Spawn objects on touch

Hello,

I’m having an issue. I am trying to get objects to spawn when another object is touched based on the position of the user’s finger. After the first object is touched, I would like it to be removed, leaving only the spawned objects. How do I do this? Below is the code I set up for the original object.

[blockcode] local removeLarge = function( event )
local t = event.target
local phase = event.phase

if (health_up ~= true) then
if “began” == phase then
t:removeSelf() – destroy object
starting_blocks = starting_blocks + 1
_score = _score + SCORE_LRG
score.text = _score
createSplitblock()
winLose()
end
end

– Stop further propagation of touch event
return true
end
[/blockcode]

Here is the code I set up for the spawned objects
[blockcode]
function createSplitblock( event )

local phase = event.phase

if “ended” == phase then
local splitblock = display.newImageRect(“images/s_boulder.png”, 35, 35);
localGroup:insert(splitblock)
splitblock.x = event.x
splitblock.y = event.y
physics.addBody(splitblock, {density = 0.3, friction = 0.3})
splitblock:setLinearVelocity(0,1)
splitblock.name = “small block”

splitblock:addEventListener(“touch”, removeSmall)
splitblock:addEventListener(“collision”, smallblockCollision )

end
return true
end
[/blockcode]

I am receiving the following error

Runtime error
/Users/Username/Desktop/Corona/level1.lua:270: attempt to index local ‘event’ (a nil value)
stack traceback:
[C]: ?
/Users/Username/Desktop/Corona/level1.lua:270: in function ‘createSplitblock’
I believe it’s referring to the second function I posted. Can anyone point me in the right direction or tell me a better way to do this? Any help would be greatly appreciated.

Thanks,
[import]uid: 47722 topic_id: 13053 reply_id: 313053[/import]

you are calling the second function with no parameters, but you have defined the function with a parameter event and are trying to get the event.phase from it

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13053 reply_id: 47914[/import]

Thanks for the reply. I’m a little confused. Do I call createSplitblock(event) in the first set of code on line 11 or createSplitblock(event.phase)? [import]uid: 47722 topic_id: 13053 reply_id: 47915[/import]

@macman223,
event is a table that is passed as a parameter. phase is an entry in the table.

so you will call it as createSplitBlock(event)

The thing that I am confused on is will it work? Think about it…

Here is some pseudocode for you to think about

[lua]if TOUCH_BEGAN
call FUNCTION2(TOUCH_BEGAN_EVENT)
end
FUNCTION2(event)
if event.phase == TOUCH_ENDED
–Do whatever
end
END[/lua]

the code in your first function is triggered only when the event.phase is “Began” and you pass the same block to the second function, and then you check for the event.phase if it is ended, which will never evaluate to true as it came from the condition where it was BEGAN.

hope you do understand what I am explaining to you here.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13053 reply_id: 47917[/import]

I removed the if “ended” == phase in the second set of code on line 3 and it worked a little better., but now i get this error

Runtime error
assertion failed!
stack traceback:
[C]: ?
[C]: in function ‘assert’
?: in function ‘getOrCreateTable’

I think it’s referring to lines 14 and 15 in the second set of code. Hmm…I wonder why. The listeners look okay. Are they in the wrong place? [import]uid: 47722 topic_id: 13053 reply_id: 47918[/import]

I figured it out. Thanks for your advice…it helped me go in the right direction [import]uid: 47722 topic_id: 13053 reply_id: 47923[/import]