im wondering how can i remove a function when an image is touched/clicked? something like “remove:self()” but i dont want it to remove itself when its pressed, i want it to remove another function/image when its pressed, thanks! [import]uid: 10827 topic_id: 6644 reply_id: 306644[/import]
I have the same question. I need to somehow remove the function below after an event happens and I already have the if/then setup, I just need the remove function code.
local function moveSprite()
[import]uid: 32061 topic_id: 6644 reply_id: 23226[/import]
You don’t “remove” functions, you just don’t call them. Functions don’t do anything until you call them.
This is a pretty fundamental thing to be confused about, so we need to see some code to understand what you’re asking about. [import]uid: 12108 topic_id: 6644 reply_id: 23259[/import]
I recently added the moveSprite function and it has caused bugs. I assume it is because I am removing the object that it is trying to move. I thought if I added the removeEventListener it would fix the problem but it hasn’t.
[code]
local function moveSprite()
tank.x = tank.x + 1
tank.y = tank.y + 0
end
local function onPostCollision ( event )
if ( event.force > 40.0 ) then
tank:play{ startFrame=3, endFrame=11, loop=2, remove=true }
tank:scale( 2, 2 )
audio.play( bombSound )
Runtime:removeEventListener( “enterFrame”, moveSprite )
self:removeEventListener( “postCollision”, self )
end
end
tank:addEventListener( “postCollision”, onPostCollision )
Runtime:addEventListener( “enterFrame”, moveSprite )
[/code] [import]uid: 32061 topic_id: 6644 reply_id: 23264[/import]
that should be
[lua]tank:removeEventListener(“postCollision”, onPostCollision)[/lua]
shouldnt it? (or better would probably be to use event.target)
also i’m sure you’re usign a mixture of global and local event listeners. is it working as it should?
https://developer.anscamobile.com/content/game-edition-collision-detection
[lua]local function onCollision( event )
print(event.object1, event.object2)
end
Runtime:addEventListener( “collision”, onCollision )[/lua]
and
[lua]local function onLocalCollision( self, event )
print(self, event.other)
end
crate1.collision = onLocalCollision
crate1:addEventListener( “collision”, crate1 )
crate2.collision = onLocalCollision
crate2:addEventListener( “collision”, crate2 )[/lua]
[import]uid: 6645 topic_id: 6644 reply_id: 23268[/import]
I fixed it. The problem was the explosion event listener.
tank:removeEventListener( “postCollision”, onPostCollision )
I had tank instead of onPostCollision so that wasn’t being removed properly. [import]uid: 32061 topic_id: 6644 reply_id: 23270[/import]
While I thought I fixed it, I noticed there were still errors. I’m getting the error message “attempting to compare number to nil”.
In the moveSprite function, is there an if then I can put in there saying if tank.x = nil, then dont worry about it?
[code]
local function moveSprite()
tank.x = tank.x + 1
end
local function onPostCollision ( event )
if ( event.force > 40.0 ) then
tank:play{ startFrame=3, endFrame=11, loop=2, remove=true }
tank:scale( 2, 2 )
audio.play( bombSound )
Runtime:removeEventListener( “enterFrame”, moveSprite )
tank:removeEventListener( “postCollision”, onPostCollision )
end
end
tank:addEventListener( “postCollision”, onPostCollision )
Runtime:addEventListener( “enterFrame”, moveSprite )
[lua]local function moveSprite()
if(tank.x~=nil) then
tank.x = tank.x + 1
end
end[/lua]
but there’s no reason tank.x should be nil unless
- you haven’t added it yet (in which case you wouldn’t want to add the moveSprite runtime listener yet)
or
- you’ve removed it (in which case you should have removed the moveSprite listener first)
j [import]uid: 6645 topic_id: 6644 reply_id: 23798[/import]
The moveSprite is making the tank move until it blows up in which case remove=true… so the moveSprite is then trying to move an object that is nil. So how can I resolve this? [import]uid: 32061 topic_id: 6644 reply_id: 23822[/import]
well theoretically when you do that collision, you also look like you remove the moveSprite runtime listener, so that shouldn’t be happening. [import]uid: 6645 topic_id: 6644 reply_id: 23834[/import]
thats what i thought too but i’m still getting an error. [import]uid: 32061 topic_id: 6644 reply_id: 23865[/import]
you’ll have to post more code. or if it’s too big, a zip on mediafire or wherever [import]uid: 6645 topic_id: 6644 reply_id: 23887[/import]