you should set physics for your pig like this
[lua]physics.addBody( pig, “static”,{ isSensor = true})[/lua] [import]uid: 71210 topic_id: 13320 reply_id: 49116[/import]
Ok so there’s progress but like always a problem. The apples are now going down in front of the pig but they’re passing without disappearing and when they pass through the pig the collisions register a couple of times so the score is about 3 after one apple has passed. The only one that does disappear (when I don’t get the error) is the first one but the ones after that never disappear.
This is the error i’m getting…
Runtime error
…kilkenny/Desktop/Today’s Tutorials/acc_ball/main.lua:98: attempt to index field ‘hit’ (a number value)
stack traceback:
[C]: ?
…kilkenny/Desktop/Today’s Tutorials/acc_ball/main.lua:98: in function ‘_listener’
?: in function <?:446>
?: in function <?:215>
and this is the code…
[code]
local function onLocalCollision( self,event )
if self.name == “apple” and event.other.name == “pig” then
timer.performWithDelay( 1000, spawnApple, 1 )
if event.other.hit == nil then
event.other.hit = 1
timer.performWithDelay(200, function() event.other.hit:removeSelf() end)
end
scoreText.text = score
score = score + 1
end
if self.name == “apple” and event.other.name == “line” then
timer.performWithDelay( 1000, spawnApple, 1 )
if event.other.hit == nil then
event.other.hit = 1
timer.performWithDelay(100, function() self:removeSelf() end)
end
end
end
apple.collision = onLocalCollision
apple:addEventListener( “collision”, apple ) [import]uid: 59140 topic_id: 13320 reply_id: 49665[/import]
I din’t understand this code
[lua]if event.other.hit == nil then
event.other.hit = 1
timer.performWithDelay(200, function() self:removeSelf() end)
end[/lua]
is it to solve the multiple collision issue ?
you wanted to remove the apple after collision right ?
so shouldn’t it be.
[lua]timer.performWithDelay(200, function() self:removeSelf() end) [/lua]
try changing your code to
[lua]local function onLocalCollision( self,event )
if self.name == “apple” and event.other.name == “pig” then
timer.performWithDelay( 1000, spawnApple, 1 )
if event.other.hit == nil then
event.other.hit = 1
timer.performWithDelay(200, function() self:removeSelf() end)
scoreText.text = score
score = score + 1
end
elseif self.name == “apple” and event.other.name == “line” then
timer.performWithDelay( 1000, spawnApple, 1 )
if event.other.hit == nil then
event.other.hit = 1
timer.performWithDelay(100, function() self:removeSelf() end)
end
end
end
apple.collision = onLocalCollision
apple:addEventListener( “collision”, apple )[/lua] [import]uid: 71210 topic_id: 13320 reply_id: 49676[/import]
Ok here’s what I’ve found…
When I got rid of the “multiple collisions” issue the apple started disappearing every time it came into contact with the pig. The only two problems left are (1) It keeps adding 2-3 points when it hits the pig instead of 1 point and (2) I get an error when I got rid of the “multiple collisions” code.
Runtime error
…kilkenny/Desktop/Today’s Tutorials/acc_ball/main.lua:104: ERROR: Attempt to remove an object that’s already been removed from the stage or whose parent/ancestor group has already been removed.
stack traceback:
[C]: ?
[C]: in function ‘removeSelf’
…kilkenny/Desktop/Today’s Tutorials/acc_ball/main.lua:104: in function ‘_listener’
?: in function <?:446>
?: in function <?:215>
Runtime error
…kilkenny/Desktop/Today’s Tutorials/acc_ball/main.lua:104: attempt to call method ‘removeSelf’ (a nil value)
stack traceback:
[C]: in function ‘removeSelf’
…kilkenny/Desktop/Today’s Tutorials/acc_ball/main.lua:104: in function ‘_listener’
?: in function <?:446>
?: in function <?:215>
Runtime error
…kilkenny/Desktop/Today’s Tutorials/acc_ball/main.lua:104: attempt to call method ‘removeSelf’ (a nil value)
stack traceback:
[C]: in function ‘removeSelf’
…kilkenny/Desktop/Today’s Tutorials/acc_ball/main.lua:104: in function ‘_listener’
?: in function <?:446>
?: in function <?:215>
And this is what my code looks like now
[code]
local function onLocalCollision( self,event )
if self.name == “apple” and event.other.name == “pig” then
timer.performWithDelay( 1000, spawnApple, 1 )
scoreText.text = score
score = score + 1
timer.performWithDelay(100, function() self:removeSelf() end)
end
if self.name == “apple” and event.other.name == “line” then
timer.performWithDelay( 1000, spawnApple, 1 )
timer.performWithDelay(100, function() self:removeSelf() end)
end
end
apple.collision = onLocalCollision
apple:addEventListener( “collision”, apple )
[import]uid: 59140 topic_id: 13320 reply_id: 49814[/import]
if you do not want more than one collision, then simply remove eventListener for that collision upon collision
something like that:
[lua]function onCollision(event)
if event.other.name == “something” then
print(“something collides with some other thing”)
otherthing:removeEventListener(“collision”, onCollision)
end[/lua] [import]uid: 16142 topic_id: 13320 reply_id: 49817[/import]
try this code and let us know how it works
[lua]local function onLocalCollision( self,event )
if self.name == “apple” then
– To avoid further collissions
self:removeEventListener( “collision”, apple )
timer.performWithDelay( 1000, spawnApple, 1 )
if event.other.name == “pig” then
timer.performWithDelay(200, function() self:removeSelf() end)
scoreText.text = score
score = score + 1
elseif event.other.name == “line” then
timer.performWithDelay(100, function() self:removeSelf() end)
end
end
end
apple.collision = onLocalCollision
apple:addEventListener( “collision”, apple )[/lua] [import]uid: 71210 topic_id: 13320 reply_id: 49836[/import]
THANK YOU, THANK YOU, THANK YOU! It works perfectly and looks great. Thanks for sticking with me I deeply appreciate it! Let me know if you want a promo code to my game when it comes out and I’ll definitely be trying to help on the forms too. [import]uid: 59140 topic_id: 13320 reply_id: 49942[/import]
EDIT: I had a problem but I found a solution right after posting the question. It’s nice to be able to solve something
[import]uid: 59140 topic_id: 13320 reply_id: 51181[/import]
thats nice… do give us some promo code when ur game is out.
[import]uid: 71210 topic_id: 13320 reply_id: 51254[/import]