Jun 192011
 

Corona for Newbies Part 4 introduces physics.

Finally, a new part in Techority’s Corona For Newbies mini series!

This covers all of the basics that you need to know about using physics in your iPhone applications; setting up, adding bodies, body types, properties, setting gravity and collision detection!

Collision detection is explained in a simple way that I believe will clear up a lot of people’s apparent confusion over how to implement it into their iPhone apps correctly.

Download Corona For Newbies Part 4

Enjoy!

And please, show appreciation if this helps you – even if you don’t want to donate Tweeting, Facebooking, etc. all helps me out!

Peach Pellen :)

PS – On a personal note, I’m sorry I haven’t been around as much lately. This is a private matter however I assure you that before long I’ll be back to giving Techority and the community the attention they deserve. (Plus I’m hoping to come up with some kind of community event we can put on; got suggestions? Let me know!)

 

Like this post? Subscribe to the monthly newsletter!

 

  79 Responses to “Corona For Newbies Part 4 – Physics”

  1. Hi Peach,

    I just downloaded your physics tutorial for CSDK newbies last week. All of your samples work fine in the simulator so no issues there. The problem is me! I`m trying to figure out the `collisions` thing. Basically I`m making a game where lavaRocks are supposed to fall with gravity until they reach the bottom border. A collision should occur and then the lavaRocks should remove themselves and the player should lose points…

    Your collision code works fine, but when I try to duplicate it at the end of my own game, the level won`t even load from the submenu.

    Here`s the code that works so far (yes, I know this file is bloated with commented out code parts, that`s because I`m using it to build a cut & paste template of code snippets that I can use for other levels in the game in order to build faster once I figure out how this works…)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    -----------------------------------------------------------------------------------------
    --
    -- level1.lua
    --
    -----------------------------------------------------------------------------------------

    local storyboard = require( "storyboard" )
    local scene = storyboard.newScene()

    -- include Corona's "widget" library
    local widget = require "widget"

    -- include Corona's "physics" library
    local physics = require "physics"
    physics.start(); physics.pause()
    physics.setGravity(0,2)
    physics.setDrawMode("hybrid")

    --- Activate MultiTouch
    system.activate ("multitouch")

    --------------------------------------------

    -- forward declarations and other locals
    local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
    --local rnd = math.random()
    local maxStars = 10
    local highScore = 000000
    local scoreActual = 000000


    ----------------------------------------------
    -- SOUND LIBRARY ----

    local soundtrack = audio.loadStream("media/ Lady Gaga - Poker Face.mp3")

    -- Sound Effects
    local winsound = audio.loadSound("tapsound.wav")
    local failsound = audio.loadSound("youwin.wav")
    ----------------------------------------------------------------------

    -- forward declarations and other locals
    local menuBtn
    local itemBtn
    ----local dodgeLeftBtn
    ----local dodgeRightBtn
    local answerBtn
    --local player



    --- CREATE LISTENER EVENTS FOR BUTT0NS -----

    -- 'onRelease' event listener for playBtn
    local function onMenuBtnRelease()
       
        --go to stageselect.lua scene
        storyboard.gotoScene( "menu", "slideLeft", 200 )
       
        return true ---- indicates successful touch
    end

    ----------------------------

    -- 'onRelease' event listener for playBtn
    local function onItemBtnRelease()
       
        --go to stageselect.lua scene
        storyboard.gotoScene( "help1", "slideLeft", 200 )
       
        return true ---- indicates successful touch
    end

    ------------------------------
    -- 'onRelease' event listener for answerBtn
    local function onAnswerBtnRelease()
       
        --go to stageselect.lua scene
        storyboard.gotoScene( "help1", "slideLeft", 200 )
       
        return true ---- indicates successful touch
    end

    ------------------------------
    -- 'onRelease' event listener for dodgeLeftBtn
    ----local function onDodgeLeftBtnRelease()
       
        --go to stageselect.lua scene
        --storyboard.gotoScene( "help1", "slideLeft", 200 )
        --dodgeBarNorm.alpha = 0
        --dodgeBarRight.alpha = 100
        --dodgeBarRight.rotation = 180
        --player.rotation = 45
       
        ----return true ---- indicates successful touch
    ----end

    ------------------------------
    -- 'onRelease' event listener for dodgeRightBtn
    ----local function onDodgeRightBtnRelease()
       
        --go to stageselect.lua scene
        --storyboard.gotoScene( "help1", "slideLeft", 200 )
       
        ----return true ---- indicates successful touch
    ----end




    ----------------------------------------------
    -- DISPLAY TEXT -----
    local wordHint_txt = display.newText("Word Hint",0,0, native.systemFontBold, 16+20)
    wordHint_txt.xScale = .45 wordHint_txt.yScale = .45
    wordHint_txt:setReferencePoint(display.TopCenterReferencePoint)
    wordHint_txt.x = screenW-245 wordHint_txt.y = screenH-315

    -- SET ALPHA
    wordHint_txt.alpha = 1


    local highScoreLabel_txt = display.newText("High Score",0,0, native.systemFont, 16+20)
    highScoreLabel_txt.xScale = .35 highScoreLabel_txt.yScale = .35
    highScoreLabel_txt:setReferencePoint(display.TopCenterReferencePoint)
    highScoreLabel_txt.x = screenW-35 highScoreLabel_txt.y = screenH-312

    local scoreTitle_txt = display.newText("Score",0,0, native.systemFont, 16+20)
    scoreTitle_txt.xScale = .35 scoreTitle_txt.yScale = .35
    scoreTitle_txt:setReferencePoint(display.TopCenterReferencePoint)
    scoreTitle_txt.x = screenW-20 scoreTitle_txt.y = screenH-273


    ----- DYNAMIC TEXT ---------

    local highScore_txt = display.newText(highScore,0,0, native.systemFontBold, 16+20)
    highScore_txt.xScale = .35 highScore_txt.yScale = .35
    highScore_txt:setReferencePoint(display.TopCenterReferencePoint)
    highScore_txt.x = screenW-25 highScore_txt.y = screenH-295

    local score_txt = display.newText(scoreActual,0,0, native.systemFontBold, 16+20)
    score_txt.xScale = .35 score_txt.yScale = .35
    score_txt:setReferencePoint(display.TopCenterReferencePoint)
    score_txt.x = screenW-25 score_txt.y = screenH-253

    ----------------------------------------------
    -----------------------------------------------------------------------------------------
    -- BEGINNING OF YOUR IMPLEMENTATION
    --
    -- NOTE: Code outside of listener functions (below) will only be executed once,
    --       unless storyboard.removeScene() is called.
    --
    -----------------------------------------------------------------------------------------

    -- Called when the scene's view does not exist:
    function scene:createScene( event )
        local group = self.view
       
       
           
    ---- INSERT BACKGROUND IMAGE ----
        local background = display.newImageRect( "gameLevel2.png", display.contentWidth, display.contentHeight )
        --background:setFillColor( 255,255,255 )
        background:setReferencePoint( display.TopLeftReferencePoint )
        background.x, background.y = 0, 0
        background:setFillColor( 255,155,255 )
       
          -- OR --
       
        ---- create a grey rectangle as the backdrop
        --local background = display.newRect( 0, 0, screenW, screenH )
        --background:setFillColor( 128 )
       
       
    ----- STATIC GRAPHICS -------

    --- DEFAULT PLAYER ---
    ----local player = display.newImageRect( "player_ss.png", 100, 150 )
        ----  player.x = 235 player.y = 200
          ----player.alpha = 1

    --- HINT BUTTON ---
    local hintButton = display.newImageRect( "button.png", 120, 30 )
          hintButton.x = 235 hintButton.y = 15
          -- SET ALPHA
          hintButton.alpha = 1
         

    --- PUZZLE LETTERS ---
    local letter_A = display.newImageRect( "letter_A_ss.png", 30, 30 )
          letter_A.x = 235 letter_A.y = 295
          --letter_A.alpha = 0
         
    local letter_S = display.newImageRect( "letter_S_ss.png", 30, 30 )
          letter_S.x = 200 letter_S.y = 295
          --letter_A.alpha = 0

    local letter_H = display.newImageRect( "letter_H_ss.png", 30, 30 )
          letter_H.x = 270 letter_H.y = 295
          --letter_A.alpha = 0
         
         

         

    --- DODGE BAR ---

          -- Normal Status --
    ----local dodgeBarNorm = display.newImageRect( "button.png", 130, 30 )
        ----  dodgeBarNorm.x = 70 dodgeBarNorm.y = 295
          --dodgeBar.alpha = 0
         
          -- Dodge Left Status (Button Press) --
    --local dodgeBarLeft = display.newImageRect( "button.png", 130, 30 )
          --dodgeBarLeft.x = 70 dodgeBarLeft.y = 295
          --dodgeBarLeft.rotation = 180
          --dodgeBar.alpha = 0

          -- Dodge Right Status (Button Press) --
    ----local dodgeBarRight = display.newImageRect( "button_Right.png", 130, 30 )
        ----  dodgeBarRight.x = 70 dodgeBarRight.y = 295
          ----dodgeBarRight.alpha = 0
       
         



    ----- DYNAMIC GRAPHICS ------


       
    ---- CREATE PHYSICAL BODIES ----
         
        -- Add Lava Rock (offscreen element)
        local lavaRock = display.newImageRect("asteroid_ss.png",90,90)
        lavaRock.x, lavaRock.y = 160, 0
        --lavaRock.x, lavaRock.y = (20, screenH-20)
        ----lavaRock.x, lavaRock.y = math.random(30, screenH-20)
       
        lavaRock.rotation = 46
       


    ---- CREATE PHYSICAL BOUNDARIES ------
    local leftwall = display.newRect(0,0,1, display.contentHeight)
    local rightwall = display.newRect(display.contentWidth,0,1, display.contentHeight)
     local floor = display.newRect(0,260, display.contentWidth, 0)
       
    ---- ADD PHYSICS TO OBJECTS ----

       -- add physics to BOUNDARIES
       physics.addBody( leftwall, "static", { friction=0.3, bounce=0.1 })
       physics.addBody( rightwall, "static", { friction=0.3, bounce=0.1 })
       physics.addBody( floor, "static", { friction=0.3, bounce=0.1 })
       
        -- add physics to the lavaRock
         physics.addBody( lavaRock, { density=0.025, friction=0.3, bounce=0.3, radius=40 } )

       



    ---- CREATE DEFAULT GRAPHICS

        --- Previous Menu Button
        --local prevBtn = display.newImageRect( "fastforward_ss.jpg", 50, 50 )
        --prevBtn.x, prevBtn.y = 50, 235
        --backBtn.rotation = 15
       
       

       
    ---- CREATE INTERACTIVE BUTTONS --------

    -- create a MenuBtn widget button (which will loads stageselect.lua on release)
        menuBtn = widget.newButton{
            --label="Play Now",
            --labelColor = { default={255}, over={128} },
            default="playButton_ss.jpg",
            over="playButtonDN_ss.jpg",
            width=40, height=40,
            onRelease = onMenuBtnRelease    -- event listener function
        }
        menuBtn.view:setReferencePoint( display.CenterReferencePoint )
        menuBtn.view.x = 30
        menuBtn.view.y = 30
       
              -- OR --
        --menuBtn.view.x = display.contentWidth*0.5
        --menuBtn.view.y = display.contentHeight - 125 
       
       
        -- create a ItemBtn widget button (which will loads stageselect.lua on release)
        itemBtn = widget.newButton{
            --label="Play Now",
            --labelColor = { default={255}, over={128} },
            default="items_ss.png",
            over="items_ss.png",
            width=100, height=100,
            onRelease = onItemBtnRelease    -- event listener function
        }
        itemBtn.view:setReferencePoint( display.CenterReferencePoint )
        itemBtn.view.x = 460
        itemBtn.view.y = 300   
       
       
         -- Answer Question Button Widgit
         answerBtn = widget.newButton{
            --label="Play Now",
            --labelColor = { default={255}, over={128} },
            default="lightBulb.png",
            over="lightBulb.png",
            width=40, height=40,
            onRelease = onAnswerBtnRelease  -- event listener function
        }
        answerBtn.view:setReferencePoint( display.CenterReferencePoint )
        answerBtn.view.x = 365
        answerBtn.view.y = 295 
       

    ---- PLAYER MOVEMENT BUTTONS ----

        -- Player DODGE RIGHT Widgit
         ----dodgeRightBtn = widget.newButton{
            --label="Play Now",
            --labelColor = { default={255}, over={128} },
            ----default="fastforward_ss.jpg",
            ----over="fastforward_ss.jpg",
            ----width=40, height=40,
            ----onRelease = onDodgeRightBtnRelease  -- event listener function
        ----}
        ----dodgeRightBtn.view:setReferencePoint( display.CenterReferencePoint )
        ----dodgeRightBtn.view.x = 110
        ----dodgeRightBtn.view.y = 295
        ----dodgeRightBtn.alpha = 0.01 

        -- Player DODGE LEFT Widgit
        ---- dodgeLeftBtn = widget.newButton{
            --label="Play Now",
            --labelColor = { default={255}, over={128} },
            ----default="fastforward_ss.jpg",
            ----over="fastforward_ss.jpg",
            ----width=40, height=40,
            ----onRelease = onDodgeLeftBtnRelease   -- event listener function
        ----}
        ----dodgeLeftBtn.view:setReferencePoint( display.CenterReferencePoint )
        ----dodgeLeftBtn.view.x = 30
        ----dodgeLeftBtn.view.y = 295
        ----dodgeLeftBtn.alpha = 0.01  
       

    ---- TOUCH EVENT LISTENERS FOR INTERACTIVE ELEMENTS ---

    ---- VELOCITY SPIN ROCKS ----
    -- function destroyRock(event)
       -- local lavaRock = event.target
        --lavaRock:applyLinearImpulse(0, -0.2, lavaRock.x, lavaRock.y)
        --lavaRock:applyLinearImpulse(0, -0.2, event.x, event.y) -- To register where I touched instead of the center all the time
        --lavaRock.alpha = 0
    --end

    ---- REMOVE ROCK FROM SCREEN EVENT ----

    function destroyRock(event)
        local lavaRock = event.target
        --lavaRock:applyLinearImpulse(0, -0.2, lavaRock.x, lavaRock.y)
        lavaRock:applyLinearImpulse(0, -0.2, event.x, event.y) -- To register where I touched instead of the center all the time
        --lavaRock.alpha = 0
    end


    ---- PLAYER DODGE LEFT FROM SCREEN EVENT ----

    ----function dodgeLeft(event)
       ---- local dodgeLeftBtn = event.target
        --lavaRock:applyLinearImpulse(0, -0.2, lavaRock.x, lavaRock.y)
        --lavaRock:applyLinearImpulse(0, -0.2, event.x, event.y) -- To register where I touched instead of the center all the time
       ---- dodgeBarNorm.alpha = 0
        ----dodgeBarRight.alpha = 100
        ----dodgeBarRight.rotation = 180
        ----player.x = 200
        ----player.rotation = -15
    ----end

    ---- PLAYER DODGE RIGHT FROM SCREEN EVENT ----

    ----function dodgeRight(event)
        ----local dodgeRightBtn = event.target
        --lavaRock:applyLinearImpulse(0, -0.2, lavaRock.x, lavaRock.y)
        --lavaRock:applyLinearImpulse(0, -0.2, event.x, event.y) -- To register where I touched instead of the center all the time
       ---- dodgeBarNorm.alpha = 0
        ----dodgeBarRight.alpha = 100
        ----dodgeBarRight.rotation = 0
        ----player.x = 270
        ----player.rotation = 15
    ----end


    ------ TOUCH EVENT HANDLERS ------
    lavaRock:addEventListener("touch", destroyRock)

    ----dodgeLeftBtn:addEventListener("touch", dodgeLeft)

    ----dodgeRightBtn:addEventListener("touch", dodgeRight)
       


       
       
    ---- GROUPS FOR Non-INTERACTIVE ELEMENTS
       
        -- all display objects must be inserted into group
        group:insert( background )
    --  group:insert( grass)
        group:insert( lavaRock )
        group:insert (hintButton)
    --  group:insert( player )
       
        --group:insert( crate )
       

     ---GROUP DISPLAY FOR WIDGITS (User Can Interact With These) ---

        group:insert( menuBtn.view )    -- you must insert .view property for widgets
        group:insert( itemBtn.view )
        group:insert( answerBtn.view )
        ----group:insert( dodgeLeftBtn.view )
        ----group:insert( dodgeRightBtn.view )

    end


    ----- COLLISIONS AREA (this is where I try to add the collision code below) --------




         
    -- Called immediately after scene has moved onscreen:
    function scene:enterScene( event )
        local group = self.view
       
        physics.start()
       
    end

    -- Called when scene is about to move offscreen:
    function scene:exitScene( event )
        local group = self.view
       
        physics.stop()
       
    end

    -- If scene's view is removed, scene:destroyScene() will be called just prior to:
    function scene:destroyScene( event )
        local group = self.view
       
        package.loaded[physics] = nil
        physics = nil
    end

    -----------------------------------------------------------------------------------------
    -- END OF YOUR IMPLEMENTATION
    -----------------------------------------------------------------------------------------

    -- "createScene" event is dispatched if scene's view does not exist
    scene:addEventListener( "createScene", scene )

    -- "enterScene" event is dispatched whenever scene transition has finished
    scene:addEventListener( "enterScene", scene )

    -- "exitScene" event is dispatched whenever before next scene's transition begins
    scene:addEventListener( "exitScene", scene )

    -- "destroyScene" event is dispatched before view is unloaded, which can be
    -- automatically unloaded in low memory situations, or explicitly via a call to
    -- storyboard.purgeScene() or storyboard.removeScene().
    scene:addEventListener( "destroyScene", scene )

    -----------------------------------------------------------------------------------------

    return scene

    Believe it or not, everything works fine up to this point. I wind up with a falling lavaRock that I can click on and make it spin. The bottom border keeps it from falling through the bottom of the screen.

    The problem comes when I add this code in the COLLISIONS AREA above.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    ---- COLLISION NAMES ----
       
        -- Adds "myName" to both lavaRock and floor
    lavaRock.myName = "lavaRock"
    floor.myName = "floor"

    ---- COLLISION CALLERS ----------

    -- Collision detection!
    lavaRock:addEventListener("collision", lavaRock)

    --function lavaRock:collision (event)
        if event.other.myName == "floor" then
        print "Vanish!"
        end
    end

    Once this code is added, my level won`t load at all and I get the following error in the terminal (which i have no clue how to fix). I have 2 `end` comments so I don`t know what it means by expected.

    ———-
    Runtime error
    error loading module ‘level1_1′ from file ‘/Users/user/Desktop/SPBA/level1_1.lua’:
    /Users/user/Desktop/SPBA/level1_1.lua:506: ” expected near ‘end’
    stack traceback:
    [C]: ?
    [C]: ?
    [C]: in function ‘require’

    —————-

    This sounds like a simple fix so I know I`m missing something, but can`t figure it out to save my life! Could you please let me know what I`m doing wring here? I`m just starting out in Corona.

    Sooooo sorry for the long post!

    ???????????

    • Holy hell that is long!

      Alright Walter, I can’t see anything obviously wrong with your code but if you want to shoot me your project file so I can run it I would be happy to take a little look and see if I can help you figure this out.

      My contact email is on my contact/hire page, OK? :)

  2. No worries; it happens :) Hope tutorials help you.

  3. No no…..actually it was my fault…sorry….it was my Mac’s problem…..Corono SDK was not responding….sorry for the comment….btw…thanks for the tutorials…..:)

  4. I’ve downloaded couple of tutorials. But i didn’t got any of those working…so sad…..:(

    • Do you mean you couldn’t get any of mine working? If so, what’s happening? I need details if it’s an issue you are having with my tutorials as it is the only way I can help you find the solution :)

  5. Hey Andrew,

    Well, I’m writing a book ;) But until then you just have to look around and find what you want to learn.

    You can check out learningcorona.com – that has a list of tutorials. A lot are not like mine but might still be useful for you moving forward.

    Peach

  6. Hey, I made it through the first four tutorials today and am excited to continue on learning, I am only a little confused on where to go from here. I really feel like I understood the first four and really liked the lesson format, but from here it looks as though we should just pick and chose what we want to learn, it’s just that there are some things I don’t exactly understand in the lessons that I looked at just now.

    So, after lesson 1-4 where now?

    Thanks a bunch,

    Andrew Friday

  7. Hey there,

    I just tested this with a copy and paste and I can tell you why it isn’t working; your quotation marks are the 66 and 99 style ones for some reason.

    myText = display.newText (“Peach hit the floor!”, 140, 30, “Arial”, 20)

    Paste that in and make sure “Peach hit the floor!” and “Arial” are showing up as strings. (Normally they appear in pink in my editor.)

    What editor are you using?

  8. Hi, peach when I replace the print with myText = display.newText (“Peach hit the floor!”, 140, 30, “Arial”, 20) the application doesn’t even load, black screen

  9. Hey Russell, sorry I didn’t see this sooner – things have been so busy lately.

    You can do peachpic:removeSelf() to remove it of if you want it to simply go invisible, peachpic.isVisible = false

    Put it in where the print/text line is and it should do the trick. (If you get an error let me know what it is.)

    Peach :)

  10. Hi Peach, is there a line of code I could put in there that would make peachpic disappear upon collission? My follow up question to that would be how to make the ground disappear upon collision with peach pic. I want to see if i can make something where I can drag 1 object to another and destroy the object i hit.

  11. Haha, no worries :)

  12. Wow that was easy I should have figured it out thanks Peach!

  13. No it wouldn’t.

    I said CHANGE the PRINT statement to that. If you change the print statement to that in the code then the text will appear on the screen on collision. Just like how the text is currently printed in the simulator. Understand?

    FYI, we don’t print to the simulator for users, we do it for us, for learning and testing and sometimes for debugging.

    But yes; CHANGE the print statement to the text line I wrote, as I said, and it will only make the text appear once the collision happens.

    Peach

  14. That will print a line of text. What I was asking is for the line of text to only be printed on the simulator when a collision occurs. For example, when the picture of peach hits the floor, then and only then will it say “Peach hit the floor!” in the simulator. So if I made a game and someone played it on a iphone they couldnt read the terminal window they wouldnt know what you were saying to them

  15. Yes, that was my intention – that is what print statements are for.

    If you wanted to put the text in the simulator you’d change that print line to;
    myText = display.newText (“Peach hit the floor!”, 140, 30, “Arial”, 20)

    or similar. If you wanted it by the picture you’d change the x and y above (140,30).

    Peach :)

  16. In the physics collision part when “Peach hits the floor” it prints out on the Corona terminal window and not the simulator. Was that your intention? If so then how would you get it to print in the simulator window next to the picture?

  17. I do have an edit button. As in, I can edit your comments ;) You just can’t with WP apparently.

    I’ll show you in the above comment.

  18. ok ok ok sorry, I screwed up again. I had so much commenting in my code that I didn’t past it all …..again (what’s relevant to the issue)…..see peachy poo if you had an edit button now, we would be peachy :)


    local boulder = display.newImage( "boulder.png" )
    game:insert( boulder )
    boulder.x = 200; boulder.y = -10
    physics.addBody( boulder, { density=1.0, friction=10, bounce=0.2, radius=36 } )

    local function moveCamera()
    if (boulder.x > 200 and boulder.x -200 and boulder.y < 1300) then
    game.y= -boulder.y + 200 --boulder.x 200 says where it shows up
    end
    end

    Runtime:addEventListener( "enterFrame", moveCamera )
    Runtime:addEventListener( "enterFrame", moveCamera1 )

    PS - Peach rawks!

  19. It seems I pasted partial code

    lol, oops.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    local boulder = display.newImage( "boulder.png" )
    game:insert( boulder )
    boulder.x = 200; boulder.y = -10
    physics.addBody( boulder, { density=1.0, friction=10, bounce=0.2, radius=36 } )


    local function moveCamera()
        if (boulder.x &gt; 200 and boulder.x  -200 and boulder.y &lt; 1300) then
            game.y= -boulder.y + 200  --boulder.x 200 says where it shows up
        end
        end
       
        --Here I put 2 even listeners on functions called
        --moveCamera, and moveCamera1
    Runtime:addEventListener( &quot;enterFrame&quot;, moveCamera )
    Runtime:addEventListener( &quot;enterFrame&quot;, moveCamera1 )

    That code is from using an image that was 2048×1408.

    That should make a little more sense.

    :)

  20. As it seems you solved your problems and are now off chilling, I shall not try to combine anything at this stage.

    Namely because as I said in my last comment I’m running on VERY little sleep ;) Off to bed now.

    But nicely handled; and thanks for posting the code, it might very well help others.

    Later G$,
    Asleep-Peach

  21. Ok It took me 4 %&$#&*@ hours to figure this out

    I figured out the “logic” I just changed one thing at a time, test, change test, change, test change, test….phew that was a bunch

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    local function moveCamera()
        if (boulder.x &gt; 200 and boulder.x &lt; 2040) then
            game.x= -boulder.x + 200  --boulder.x 200 says where it shows up
       
       
        --[[
     
            --]]
        end
    end

    --test remove if necessary
    local function moveCamera1()

    --This is how I UNDERSTAND IT (my interpretation that is)
    --if (object.y is greater than ( -200 and boulder.y  -200 and boulder.y  -200 and boulder.y &lt; 200) then
            game.y= -boulder.y + 200  --boulder.x 200 says where it shows up--]]
       
        --Here I put 2 even listeners on functions called
        --moveCamera, and moveCamera1
    Runtime:addEventListener( &quot;enterFrame&quot;, moveCamera )
    Runtime:addEventListener( &quot;enterFrame&quot;, moveCamera1 )

    Ok so after all that I realized that if you make a large map, that you set what I call a sensor (sounds good to me) on the left/right/top/bottom so that when your object is close to the edge it doesn’t scroll, instead it stops scrolling and is smooth.

    The functions, I have no idea why I had to create 2 functions, 2 listeners but hey it works.

    Peachy, if you know how to combine or nest the 2 functions i’m all ears :)

    Now, im off to hotel land for 3 days for vacation :)

    g$

  22. I was just reading (you need an edit button on here ! :) ) and I should clarify a little bit.

    On the egg breaker demo, I’m referring to only the view of the boulder.

    Ok my attempt at ascii art below. the “X” is the object I want focus on. I have a map made (i am doing a larger map WITHOUT LIME, Or SVG level editor, I want to go pure corona on this one). It’s like 1200 by 512 so the focus on the object is critical.

    Also Ms Peachy Poo, I would like to implement a view so when the object is close to an edge, the focus stops.

    Example A)
    _______
    | |
    | |
    |___x___|

    I was just reading (you need an edit button on here ! :) ) and I should clarify a little bit.

    On the egg breaker demo, I’m referring to only the view of the boulder.

    Ok my attempt at ascii art below. the “X” is the object I want focus on. I have a map made (i am doing a larger map WITHOUT LIME, Or SVG level editor, I want to go pure corona on this one). It’s like 1200 by 512 so the focus on the object is critical.

    Also Ms Peachy Poo, I would like to implement a view so when the object is close to an edge, the focus stops.

    Example B)

    Here we have an example. I made it longer, so my round thing (I haven’t decided what it is yet) is rolling around and when it gets to the edge, I want the focus to stop so that I don’t see black on the outside of my background.

    _______________
    | |
    | |
    |___x__________|

    Hopefully that makes sense, I apologize in advance if I hit submit and my ascii art doesn’t come across properly, but see if you had an EDIT BUTTON then I could fix it :)
    *hint hint* lol

    I’m excited to be at this point, but as you probably know by now, you get a design then have the features you want and every time you put one in and think YEA I GOT IT NOW, the next one stumps you…..

    g$

  23. Ok, now that I have gotten past my little debacle.

    I’m using gravity applied to the world to control the object (only X axis though, no Y) which works perfect.

    Now, I want to put focus on the object so NO MATTER WHERE ITS AT IT WILL BE CENTERED ON THE SCREEN.

    I looked at the egg breaker and it does it, and keeps the boulder there, but I was looking for when it’s in the air etc that it’s always centered.

    Any idea on how to do that?

    If you have a code snippet or something, an idea whatever i’m all ears!

    btw, said it b4 grats on 4.5 mil views you a boss now.

  24. I was still reading :P

    As far as I can tell you aren’t asking a question, just rambling incoherently about the differences between moving all physics bodies and moving just one…. I think :P

    So, ah, good! XD

    And yes – perfectly fine – you don’t have to ask to link back :P I love link love ^-^;

    Now if you will excuse me I just nearly lit my microwave on fire…. Again. (I’m not kidding.)

    Later!

    PS – Ooooh, our comments made it go over 50 so now it just shows the newest. Cool.

  25. physics.addBody( ball, { density=2.0, friction=0.5, bounce=0.4, radius=24 } )
    ^^ Yep.

    I did take the “Shape Tumbler” sample from Corona and ripped out everything except a soccer ball. I noticed the tilt controls on that are vastly different than what you have, and seems to just apply Gravity to X and Y globally instead of just on an actor (yes, I said actor, you said you from flash world, I never touched it, yet I like ACTOR so I’ll shall use it too :) )

    Ok so here is your code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    local motionx = 0
    --local motiony = 0

    local function onAccelerate( event )
    motionx = 35 * event.xGravity
    --motiony = 35 * event.yGravity
    end
    Runtime:addEventListener ("accelerometer", onAccelerate)

    local function moveball (event)
    ball.x = ball.x + motionx
    --ball.y = ball.y - motiony
    end
    Runtime:addEventListener("enterFrame", moveball)

    And here it is from the shape tumbler:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    local physics = require("physics")
    physics.start()

    physics.setScale( 60 )
    physics.setGravity( 0, 9.8 ) -- initial gravity points downwards

    system.setAccelerometerInterval( 100 ) -- set accelerometer to maximum responsiveness

    -- Build this demo for iPhone to see accelerometer-based gravity
    function onTilt( event )
        physics.setGravity( ( 9.8 * event.xGravity ), ( -9.8 * event.yGravity ) )
    end

    Runtime:addEventListener( "accelerometer", onTilt )

    From what I can tell, your code focuses on the Actor, the shapetumbler applies the gravity to x and y globally making all objects affected that can move.

    The big thing other than fluffy kitten purple carpets trying to invade my mind (just checking to see if you were still reading lol, I like doing that) is the following:

    When I use your tilt controls, a ball controlled by the tilt don’t seem to roll against other objects UNLESS the
    ball falls onto a angled ramp or something.

    When I use the shape tumbler code, it works fine. So now I am trying to find out why, I don’t know If I have to do something weird like apply force when tilt goes one way to get the ball to spin, or something but it’s quite odd.

    These tilt controls you provided are perfect for other games I was thinking about doing (on this one I took out the Y as I didn’t want it) so I think I can get them perfect for this too, I’ll just have to wrestle with it lol.

    I just want to control an object using only gravity. So if a ball goes up against a ramp, me tilting it should not make it magically fly up the ramp, instead it should just rest at the bottom and behave.

    I’ll figure it out, then I plan on making a very large post about my dealings with Corona off site with a link back to you as your site is what got me exploring this thing heavily (of course, I’ll ask you if it’s ok to link back :) )

    take care – nick G$

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.