problem with collision in storyboard.api

I can’t get my onCollision function to call through the ‘floor1’ collision event listener. I can get it to call through the ‘present1’ collision event listener, but I want the floor1 to have it since multiple objects will be hitting it. Thanks in advance

Also I could only forward declare onCollision function as a table, can someone explain why that is?

[lua]–scene1–


– scenetemplate.lua


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

physics = require(‘physics’)
physics.start()
physics.setGravity(0,9.3)
–physics.setDrawMode(‘hybrid’)

local p = 0
local bg1, floor1, present1, dropButton

local phys1 = {friction = .7, bounce = .1, density = .3}
local h = display.contentHeight
local w = display.contentWidth
local level1Presents
local onTouch
local createDropButton
local dropPresents
local onCollision = {}



– NOTE:

– Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.



– BEGINNING OF YOUR IMPLEMENTATION

– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view

bg1 = display.newRect(0, 0, w, h)
bg1:setFillColor(200,0,0)
group:insert(bg1)

floor1 = display.newRect(0, h-1, w, 1)
floor1.name = ‘floor1’
physics.addBody(floor1, “static”,{density = 9, friction = .7, bounce = .2})
floor1:addEventListener(‘collision’, onCollision)
group:insert(floor1)


– CREATE display objects and add them to ‘group’ here.
– Example use-case: Restore ‘group’ from previously saved state.


end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
print( “1: enterScene event” )

local function level1Presents()
p = p+1
print §
if (p ==1) then
present1 = display.newRect(0,0, 50, 50)
physics.addBody(present1, ‘kinematic’, phys1)
present1.x = w/2
present1.y = h/7
present1.name = ‘present1’
present1:addEventListener(‘touch’, onTouch)
present1:addEventListener(‘collision’, onCollision)
group:insert(present1)
elseif (p == 2) then

storyboard.gotoScene(‘scene2’)
end

end

function onTouch(event)

t = event.target
t.name = ‘t’
local phase = event.phase

if event.phase == “began” then
display.getCurrentStage():setFocus(t)
t.isFocus = true
t.x0= event.x - t.x

elseif t.isFocus then
if event.phase == “moved” then
t.x = event.x - t.x0
elseif event.phase == ‘ended’ or ‘cancelled’ then
display.getCurrentStage():setFocus(nil)
t.isFocus = false
createDropButton()
t:removeEventListener(‘touch’, onTouch)
end
end
return true
end

function createDropButton()
dropButton = display.newRect(w-100,h/4, 80, 80)
dropButton:addEventListener(‘tap’, dropPresent)
end

function dropPresent()
t.bodyType = ‘dynamic’
timer.performWithDelay(3000, level1Presents, 1)
display.remove(dropButton)
–dropButton:removeEventListener
end

function onCollision(event)
if event.other.name ==‘present1’ then
floor1:setFillColor(255, 0, 0)
print ‘collision’
end
end

level1Presents()

– INSERT code here (e.g. start timers, load audio, start listeners, etc.)


end

– Called when scene is about to move offscreen:
function scene:exitScene( event )

print( “1: exitScene event” )
–local group = self.view
present1:removeEventListener(‘touch’, onTouch)

–dropButton:removeEventListener(‘tap’, dropPresent)

– INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)


end

– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
–local group = self.view
print( “((destroying scene 1’s view))” )


– INSERT code here (e.g. remove listeners, widgets, save state, etc.)


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 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

[import]uid: 75779 topic_id: 19828 reply_id: 319828[/import]

Hey there, try this;

[lua]----------------------------------------------------------------------------------

– scenetemplate.lua


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

physics = require(‘physics’)
physics.start()
physics.setGravity(0,9.3)
–physics.setDrawMode(‘hybrid’)

local p = 0
local bg1, floor1, present1, dropButton

local phys1 = {friction = .7, bounce = .1, density = .3}
local h = display.contentHeight
local w = display.contentWidth

local level1Presents
local onTouch
local createDropButton
local dropPresents
local onCollision = {}



– NOTE:

– Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.



– BEGINNING OF YOUR IMPLEMENTATION

– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view

bg1 = display.newRect(0, 0, w, h)
bg1:setFillColor(200,0,0)
group:insert(bg1)

floor1 = display.newRect(0, h-1, w, 1)
floor1.name = ‘floor1’
physics.addBody(floor1, “static”,{density = 9, friction = .7, bounce = .2})
floor1:addEventListener(‘collision’, floor1)
group:insert(floor1)

function floor1:collision ()
print “test”
end


– CREATE display objects and add them to ‘group’ here.
– Example use-case: Restore ‘group’ from previously saved state.


end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
print( “1: enterScene event” )

local function level1Presents()
p = p+1
print §
if (p ==1) then
present1 = display.newRect(0,0, 50, 50)
physics.addBody(present1, ‘kinematic’, phys1)
present1.x = w/2
present1.y = h/7
present1.name = ‘present1’
present1:addEventListener(‘touch’, onTouch)
present1:addEventListener(‘collision’, onCollision)
group:insert(present1)
elseif (p == 2) then
storyboard.gotoScene(‘scene2’)
end

end

function onTouch(event)

t = event.target
t.name = ‘t’
local phase = event.phase

if event.phase == “began” then
display.getCurrentStage():setFocus(t)
t.isFocus = true
t.x0= event.x - t.x

elseif t.isFocus then
if event.phase == “moved” then
t.x = event.x - t.x0
elseif event.phase == ‘ended’ or ‘cancelled’ then
display.getCurrentStage():setFocus(nil)
t.isFocus = false
createDropButton()
t:removeEventListener(‘touch’, onTouch)
end
end
return true
end

function createDropButton()
dropButton = display.newRect(w-100,h/4, 80, 80)
dropButton:addEventListener(‘tap’, dropPresent)
end

function dropPresent()
t.bodyType = ‘dynamic’
timer.performWithDelay(3000, level1Presents, 1)
display.remove(dropButton)
–dropButton:removeEventListener
end

function onCollision(event)
if event.other.name ==‘present1’ then
floor1:setFillColor(255, 0, 0)
print ‘collision’
end
end

level1Presents()

– INSERT code here (e.g. start timers, load audio, start listeners, etc.)


end

– Called when scene is about to move offscreen:
function scene:exitScene( event )

print( “1: exitScene event” )
–local group = self.view
present1:removeEventListener(‘touch’, onTouch)

–dropButton:removeEventListener(‘tap’, dropPresent)

– INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)


end

– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
–local group = self.view
print( “((destroying scene 1’s view))” )


– INSERT code here (e.g. remove listeners, widgets, save state, etc.)


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 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[/lua]

That should print test :slight_smile: [import]uid: 52491 topic_id: 19828 reply_id: 76953[/import]

Thanks Peach, I see you called the floor1 ‘collision’ event listener on itself. What line can I insert into the collision function that would restart the scene from the beginning. kind of like a game over and restart the scene. thanks [import]uid: 75779 topic_id: 19828 reply_id: 77134[/import]

You’d do it in the line where I print “test”.

I’m not actually sure how you’d call it from within the scene (storyboard isn’t something I’ve had a chance to play with yet) but what I normally do when using tools like this or Director is to navigate to a gameover scene then back to the level - that way it is reset without any fussing about with extra code.

Make sense?

Peach :slight_smile: [import]uid: 52491 topic_id: 19828 reply_id: 77208[/import]

Yes in the line we put ‘test’ I tried a few things. I tried storyboard.gotoScene(‘gameover’) and made a gameover scene. It didn’t fire. I couldn’t fire any storyboard.gotoScene events from the floor1:collision function. I tried delaying it with a timer.performWithDelay(no dice) I will do more research into how to do this maybe repost in a few days. Tnx! [import]uid: 75779 topic_id: 19828 reply_id: 77507[/import]