[RESOLVED] Help on physics, please!

I have a code



– main.lua

display.setStatusBar( display.HiddenStatusBar )

local physics = require (“physics”)
physics.start()

local bg = display.newImage (“bg.png”)
local cat = display.newImage (“cat.png”,80,10)
    cat:scale  (.5, .5)
local ground = display.newImage (“hills.png”, 0, display.contentHeight-100)
local leftWall = display.newRect(-10, 0, 13, 500) – note 1                           
        leftWall.strokeWidth = 1
        leftWall:setFillColor(0, 255, 0)
        leftWall:setStrokeColor(0, 255, 0)
        
local rightWall = display.newRect(310, 0, 10, 150)
        rightWall.strokeWidth = 1
        rightWall:setFillColor(0, 255, 0)
        rightWall:setStrokeColor(0, 255, 0)
        
local rightWall2 = display.newRect(310, 140, 10, 180)
        rightWall2.strokeWidth = 1
        rightWall2:setFillColor(0, 255, 0)
        rightWall2:setStrokeColor(0, 255, 0)
        
local rightWall3 = display.newRect(310, 322, 10, 150)
        rightWall3.strokeWidth = 1
        rightWall3:setFillColor(0, 255, 0)
        rightWall3:setStrokeColor(0, 255, 0)

local topWall = display.newRect(0, 0, 350, 5)
        topWall.strokeWidth = 1
        topWall:setFillColor(0, 255, 0)
        topWall:setStrokeColor(0, 255, 0)
        
local floor = display.newRect(0, 475, 350, 5)
        floor.strokeWidth = 1
        floor:setFillColor(0, 255, 0)
        floor:setStrokeColor(0, 255, 0)
        
local myText = display.newText (“Help the cat scape”, 50,0,nil,22)

local function dragBody ( event )
    local body = event.target
    local phase = event.phase
    local stage = display.getCurrentStage ( )
 
    if “began” == phase then
        stage:setFocus ( body, event.id )
        body.isFocus = true
        body.tempJoint = physics.newJoint ( “touch”, body, event.x, event.y )
    elseif body.isFocus then
            if “moved” == phase then
                body.tempJoint:setTarget ( event.x, event.y )
            elseif “ended” == phase or “cancelled” == phase then
                stage:setFocus (body, nil)
                body.isFocus = false
                body.tempJoint:removeSelf ( )
            end
    end
    return true 
end
physics.addBody (leftWall, “static”)
physics.addBody (rightWall, “static”)
physics.addBody (rightWall3, “static”)
physics.addBody (topWall, “static”)
physics.addBody (floor, “static”)
physics.addBody (cat, “dynamic”, {density=1, friction=-0.9 , bounce = 0.3})
cat:addEventListener (“touch”, dragBody)

--------NOTE 1-------
– first number, move object from left to right    
– second number, move object from top to bottom
– third number, makes object width bigger
– fourth number, makes object height taller




And works perfect! a nice simple game.




------------------------------------------------------------------THE PROBLEM-----------------------


I want to add this game on one of my storyboard scenes.

– So, where do I place the dragBody (Event) function? – inside the enterScene?, before? Where?

–Where do I put the – physics.addBody? – at the end? after the addEventListeners? Where?

–Where do I put all the images for the walls and the cat? – in the createScene? Where?


This is my complete code, but it doesn’t work.


– --==***************************************************************************+±- –
– Music Theory Game
– By Victor M. Barba
– Copyright 2013 – All Rights Reserved

– Version 1.0
– --==***************************************************************************+±- –

local storyboard = require( “storyboard” )
local widget = require “widget”

local scene = storyboard.newScene()
local wrongSound = audio.loadSound( “wrongSound.mp3”)

local physics = require (“physics”)
physics.start()

– --==******************[FUNCTIONS TO GO TO ANOTHER SCENE]**********************+±- –

local function buttonLearnLevel1()
        storyboard.gotoScene( “home”, “crossFade”, 300 )    
    return true
end

local function dragBody ( event )
    local body = event.target
    local phase = event.phase
    local stage = display.getCurrentStage ( )
    
    if “began” == phase then
        stage:setFocus ( body, event.id )
        body.isFocus = true
        body.tempJoint = physics.newJoint ( “touch”, body, event.x, event.y )
    elseif body.isFocus then
    
            if “moved” == phase then
                body.tempJoint:setTarget ( event.x, event.y )
            elseif “ended” == phase or “cancelled” == phase then
                stage:setFocus (body, nil)
                body.isFocus = false
                body.tempJoint:removeSelf ( )
            end
        
    end
    
    return true
    
end

– --==**************************[CREATE SCENE]**********************************+±- –

function scene:createScene( event )
    local group = self.view
    
    local background = display.newImage( “backgroundPlayCat.png” )
            background:scale (1.03, 1.03)
    
    buttonLearnLevel1 = widget.newButton{
        defaultFile=“buttonHome.png”,
        onRelease = buttonLearnLevel1
    }
    buttonLearnLevel1.x = 530
    buttonLearnLevel1.y = 680
    
local wall1 = display.newImage( “wall1red.png” )                           
        wall1.x = 1020
        wall1.y = 50
        
local wall2 = display.newImage( “wall2red.png” )                           
        wall2.x = 500
        wall2.y = 5
        
local wall3 = display.newImage( “wall3red.png” )                           
        wall3.x = 7
        wall3.y = 390
        
local wall4 = display.newImage( “wall4red.png” )                           
        wall4.x = 500
        wall4.y = 760
        
local wall5 = display.newImage( “wall5red.png” )                           
        wall5.x = 1020
        wall5.y = 480
        
local ladyBug = display.newImage( “ladyBug.png” )                           
        ladyBug.x = 100
        ladyBug.y = 100
        ladyBug:scale (.5, .5)
    
---------------------------------------------------------------------insert into group----
    
    group:insert ( background )
    group:insert ( buttonLearnLevel1 )
    group:insert ( wall1 )
    group:insert ( wall2 )
    group:insert ( wall3 )
    group:insert ( wall4 )
    group:insert ( wall5 )
    
end

– --==***************************[ENTER SCENE]**********************************+±- –

function scene:enterScene( event )
    local group = self.view
    

    
    
end

– --==***************************[EXIT SCENE]**********************************+±- –

function scene:exitScene( event )
    local group = self.view
    
end

– --==**************************[DESTROY SCENE]*********************************+±- –

function scene:destroyScene( event )
    local group = self.view    

    
    if buttonLearnLevel1 then
        buttonLearnLevel1:removeSelf()
        buttonLearnLevel1 = nil
    end
    
    
    
end

– --==*************************[EVENT LISTENER]*********************************+±- –

physics.addBody (ladyBug, “dynamic”, {density=1, friction=-0.9 , bounce = 0.3})

ladyBug:addEventListener (“touch”, dragBody)


scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )

return scene


I hope you can help me with this, this is all I need to finish my app.

Thanks

Victor

As I know you should put every commands about initialization in the createScene event. So physics objects should be initialize there. Then, your listeners like drugBody should be above createScene (as in your code). And finnaly, you should add listeners in enterScene event, and in exitScene you should to remove it.

function scene:enterScene( event )     local group = self.view     ladyBug:addEventListener ("touch", dragBody) end function scene:exitScene (event)    local group = self.view     ladyBug:removeEventListener ("touch", dragBody) end  

For the further information read here http://www.coronalabs.com/blog/2012/03/27/storyboard-scene-events-explained/

Thank you very much anton2111 it works!

I had to do a little study first but I got it to work!

I just have one little problem more…

I go to the gameScene the first time fine, and the game works but when I go to another scene or another and come back for the second time to the gameScene, Problems!!

File: …sers/victormbarba/Desktop/Piano For Kids/working.lua
Line: 113
Bad argument #-1 to ‘addBody’ (Proxy expected, got nil)

----------------------------------------------------------------------------- Something that Proxy

please help me out

---------------------------------- This is the complete code------------------

local storyboard = require( “storyboard” )
local widget = require “widget”
local scene = storyboard.newScene()

local image, text1, memTimer, wallFloor, wallTop, wallLeft, wallRightBottom, wallRightTop, wallScape
local ladyBug

local function movePlace()
        storyboard.gotoScene( “level1Q14”, “crossFade”, 500 )    
    return true
end

local function dragBody ( event )
    local body = event.target
    local phase = event.phase
    local stage = display.getCurrentStage ( )
    
    if “began” == phase then
        stage:setFocus ( body, event.id )
        body.isFocus = true
        body.tempJoint = physics.newJoint ( “touch”, body, event.x, event.y )
    elseif body.isFocus then
    
            if “moved” == phase then
                body.tempJoint:setTarget ( event.x, event.y )
            elseif “ended” == phase or “cancelled” == phase then
                stage:setFocus (body, nil)
                body.isFocus = false
                body.tempJoint:removeSelf ( )
            end
        
    end
    
    return true
    
end
--------------------------------------------------------------CREATE SCENE----------
function scene:createScene( event )
    local screenGroup = self.view
    
    local physics = require (“physics”)
    physics.start()
    
        image = display.newImage( “backgroundPlayLadyBug.png” )
        screenGroup:insert( image )
        image.x = 509
        image.y = 384

        wallFloor = display.newImage( “wallFloor.png” )
        screenGroup:insert( wallFloor )
        wallFloor.x = 512
        wallFloor.y = 759
        
        wallTop = display.newImage( “wallTop.png” )
        screenGroup:insert( wallTop )
        wallTop.x = 512
        wallTop.y = 5
        
        wallLeft = display.newImage( “wallLeft.png” )
        screenGroup:insert( wallLeft )
        wallLeft.x = 5
        wallLeft.y = 380
        
        wallRightBottom = display.newImage( “wallRightBottom.png” )
        screenGroup:insert( wallRightBottom )
        wallRightBottom.x = 1020
        wallRightBottom.y = 495
        
        wallRightTop = display.newImage( “wallRightTop.png” )
        screenGroup:insert( wallRightTop )
        wallRightTop.x = 1020
        wallRightTop.y = 90
        
        wallScape = display.newImage( “wallLadyBugScape.png” )
        screenGroup:insert( wallScape )
        wallScape.x = 1020
        wallScape.y = 212
    
        text1 = display.newText( “Help the LadyBug scape!”, 0, 0, nil, 20 )
        text1:setTextColor( 255 )
        text1:setReferencePoint( display.CenterReferencePoint )
        text1.x, text1.y = display.contentWidth * 0.5, 50
        screenGroup:insert( text1 )
        
end

--------------------------------------------------------------ENTER SCENE----------
function scene:enterScene( event )
    local group = self.view
    
        storyboard.purgeScene( “home” )
    
        buttonB = widget.newButton{
        defaultFile=“buttonHome.png”,
        onRelease = movePlace
        }
        buttonB.x = 500
        buttonB.y = 695
        group:insert ( buttonB )
                
        ladyBug = display.newImage (“ladyBugSmall.png”)
        ladyBug.x = 200
        ladyBug.y = 600

physics.addBody (wallFloor, “static”) – THIS IS LINE 113
physics.addBody (wallTop, “static”)
physics.addBody (wallLeft, “static”)
physics.addBody (wallRightBottom, “static”)
physics.addBody (wallRightTop, “static”)

physics.addBody (ladyBug, “dynamic”, {density=6, friction=-0.8 , bounce = 0.4})

ladyBug:addEventListener (“touch”, dragBody)
    
end

--------------------------------------------------------------EXIT SCENE----------
function scene:exitScene()
    --cat:removeEventListener (“touch”, dragBody)
    ladyBug:removeEventListener (“touch”, dragBody)
    
    --display.remove ( cat )
    display.remove ( wallFloor )
    display.remove ( wallScape )
    display.remove ( ladyBug )
    display.remove ( wallTop )
    display.remove ( wallLeft )
    display.remove ( wallRightBottom )
    display.remove ( wallRightTop )

end

--------------------------------------------------------------DESTROY SCENE----------
function scene:destroyScene( event )
    if buttonB then
            buttonB:removeSelf()
            buttonB = nil
        end
end

– END OF YOUR IMPLEMENTATION

scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )

return scene

Victor
 

You have to change movePlace() function. It is a little tricky and hard to understand. I spend about a day to figure it out. So, you need to purgee scene before you call gotoScene() function - and your movePlace() should be next:

local function movePlace()     storyboard.purgeScene ("")--The name of scene you want to leave.     storyboard.gotoScene( "level1Q14", "crossFade", 500 )         return true end

I hope it will help.

I did not really get that purgeScene yet!

but I found another way to make it work. instead of a .png image I use the

clearFloor = display.newRect(2,770,1020,5) --visible (1,1,5,768)
        --clearFloor.strokeWidth = 1
        clearFloor:setFillColor(0, 0, 255)
        clearFloor:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearFloor )
        physics.addBody (clearFloor, “static”)

And when I go to the same scene againg it works!

I don’t really understand why, but IT WORKS!, so now my app is finished.

Yeeeeeeeeeee!!!

Thanks for all your help, I’m learning a lot

but I still need a lot, lot, lot, lot more… (I know)

Victor

As I know you should put every commands about initialization in the createScene event. So physics objects should be initialize there. Then, your listeners like drugBody should be above createScene (as in your code). And finnaly, you should add listeners in enterScene event, and in exitScene you should to remove it.

function scene:enterScene( event )     local group = self.view     ladyBug:addEventListener ("touch", dragBody) end function scene:exitScene (event)    local group = self.view     ladyBug:removeEventListener ("touch", dragBody) end  

For the further information read here http://www.coronalabs.com/blog/2012/03/27/storyboard-scene-events-explained/

Thank you very much anton2111 it works!

I had to do a little study first but I got it to work!

I just have one little problem more…

I go to the gameScene the first time fine, and the game works but when I go to another scene or another and come back for the second time to the gameScene, Problems!!

File: …sers/victormbarba/Desktop/Piano For Kids/working.lua
Line: 113
Bad argument #-1 to ‘addBody’ (Proxy expected, got nil)

----------------------------------------------------------------------------- Something that Proxy

please help me out

---------------------------------- This is the complete code------------------

local storyboard = require( “storyboard” )
local widget = require “widget”
local scene = storyboard.newScene()

local image, text1, memTimer, wallFloor, wallTop, wallLeft, wallRightBottom, wallRightTop, wallScape
local ladyBug

local function movePlace()
        storyboard.gotoScene( “level1Q14”, “crossFade”, 500 )    
    return true
end

local function dragBody ( event )
    local body = event.target
    local phase = event.phase
    local stage = display.getCurrentStage ( )
    
    if “began” == phase then
        stage:setFocus ( body, event.id )
        body.isFocus = true
        body.tempJoint = physics.newJoint ( “touch”, body, event.x, event.y )
    elseif body.isFocus then
    
            if “moved” == phase then
                body.tempJoint:setTarget ( event.x, event.y )
            elseif “ended” == phase or “cancelled” == phase then
                stage:setFocus (body, nil)
                body.isFocus = false
                body.tempJoint:removeSelf ( )
            end
        
    end
    
    return true
    
end
--------------------------------------------------------------CREATE SCENE----------
function scene:createScene( event )
    local screenGroup = self.view
    
    local physics = require (“physics”)
    physics.start()
    
        image = display.newImage( “backgroundPlayLadyBug.png” )
        screenGroup:insert( image )
        image.x = 509
        image.y = 384

        wallFloor = display.newImage( “wallFloor.png” )
        screenGroup:insert( wallFloor )
        wallFloor.x = 512
        wallFloor.y = 759
        
        wallTop = display.newImage( “wallTop.png” )
        screenGroup:insert( wallTop )
        wallTop.x = 512
        wallTop.y = 5
        
        wallLeft = display.newImage( “wallLeft.png” )
        screenGroup:insert( wallLeft )
        wallLeft.x = 5
        wallLeft.y = 380
        
        wallRightBottom = display.newImage( “wallRightBottom.png” )
        screenGroup:insert( wallRightBottom )
        wallRightBottom.x = 1020
        wallRightBottom.y = 495
        
        wallRightTop = display.newImage( “wallRightTop.png” )
        screenGroup:insert( wallRightTop )
        wallRightTop.x = 1020
        wallRightTop.y = 90
        
        wallScape = display.newImage( “wallLadyBugScape.png” )
        screenGroup:insert( wallScape )
        wallScape.x = 1020
        wallScape.y = 212
    
        text1 = display.newText( “Help the LadyBug scape!”, 0, 0, nil, 20 )
        text1:setTextColor( 255 )
        text1:setReferencePoint( display.CenterReferencePoint )
        text1.x, text1.y = display.contentWidth * 0.5, 50
        screenGroup:insert( text1 )
        
end

--------------------------------------------------------------ENTER SCENE----------
function scene:enterScene( event )
    local group = self.view
    
        storyboard.purgeScene( “home” )
    
        buttonB = widget.newButton{
        defaultFile=“buttonHome.png”,
        onRelease = movePlace
        }
        buttonB.x = 500
        buttonB.y = 695
        group:insert ( buttonB )
                
        ladyBug = display.newImage (“ladyBugSmall.png”)
        ladyBug.x = 200
        ladyBug.y = 600

physics.addBody (wallFloor, “static”) – THIS IS LINE 113
physics.addBody (wallTop, “static”)
physics.addBody (wallLeft, “static”)
physics.addBody (wallRightBottom, “static”)
physics.addBody (wallRightTop, “static”)

physics.addBody (ladyBug, “dynamic”, {density=6, friction=-0.8 , bounce = 0.4})

ladyBug:addEventListener (“touch”, dragBody)
    
end

--------------------------------------------------------------EXIT SCENE----------
function scene:exitScene()
    --cat:removeEventListener (“touch”, dragBody)
    ladyBug:removeEventListener (“touch”, dragBody)
    
    --display.remove ( cat )
    display.remove ( wallFloor )
    display.remove ( wallScape )
    display.remove ( ladyBug )
    display.remove ( wallTop )
    display.remove ( wallLeft )
    display.remove ( wallRightBottom )
    display.remove ( wallRightTop )

end

--------------------------------------------------------------DESTROY SCENE----------
function scene:destroyScene( event )
    if buttonB then
            buttonB:removeSelf()
            buttonB = nil
        end
end

– END OF YOUR IMPLEMENTATION

scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )

return scene

Victor
 

You have to change movePlace() function. It is a little tricky and hard to understand. I spend about a day to figure it out. So, you need to purgee scene before you call gotoScene() function - and your movePlace() should be next:

local function movePlace()     storyboard.purgeScene ("")--The name of scene you want to leave.     storyboard.gotoScene( "level1Q14", "crossFade", 500 )         return true end

I hope it will help.

I did not really get that purgeScene yet!

but I found another way to make it work. instead of a .png image I use the

clearFloor = display.newRect(2,770,1020,5) --visible (1,1,5,768)
        --clearFloor.strokeWidth = 1
        clearFloor:setFillColor(0, 0, 255)
        clearFloor:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearFloor )
        physics.addBody (clearFloor, “static”)

And when I go to the same scene againg it works!

I don’t really understand why, but IT WORKS!, so now my app is finished.

Yeeeeeeeeeee!!!

Thanks for all your help, I’m learning a lot

but I still need a lot, lot, lot, lot more… (I know)

Victor