timer.performWithDelay Event Problems [RESLOVED]

Hello,
In my game when the apple reaches the ground it is supposed to self:removeSelf. It’s working fine but it doesn’t look very realistic because it disappears before it’s gone through the ground.

If you need to see more code then let me know.

Thank,
Danny

My code is this…

[code]
timer.performWithDelay(1000, self:removeSelf())

[import]uid: 59140 topic_id: 13320 reply_id: 313320[/import]

I may not understand completely, but you could extend the time. If this is not good, make the apple actually make contact with a “sensor” object that is actually “below” the ground. Hope this helps! [import]uid: 21331 topic_id: 13320 reply_id: 48912[/import]

Thanks for the response! Your idea of the sensor could work for the app but I also have an object moving around the screen and if it makes contact with an apple then it has to subtract points so I can’t make a “line” to tell wether it has been hit or not because of the moving object. I did try making it a longer timer but that didn’t work.

Any other ideas?
[import]uid: 59140 topic_id: 13320 reply_id: 48913[/import]

try setting a property named type for your objects.
and if you put type for Apple object as “Apple” you can use the below code
[lua]local groundLine = display.newLine(10, 300, 300, 300)
groundLine.width = 2
groundLine:setColor( 255, 0, 0)

local function removeObject(event)
if event.other.type == “apple” the
event.other:removeSelf()
end
end

physics.addBody( groundLine, “static”)
groundLine:addEventListener(“collision”,removeObject)[/lua] [import]uid: 71210 topic_id: 13320 reply_id: 48924[/import]

Hey!
The only problem is my command in a function handler. I’m just really not sure how to make this work. All I want to do is delay the disappearance of the apple after hitting the ground. All my other systems are working correctly so I don’t want to change them up too much. Here is the code…

[code]
local function onLocalCollision( self,event )
if self.name == “apple” and event.other.name == “pig” then
timer.performWithDelay( 1000, spawnApple, 1 )
timer.performWithDelay( 10000, self:removeSelf())
score = score + 1
scoreText.text = score
end
if self.name == “apple” and event.other.name == “line” then
timer.performWithDelay( 1000, spawnApple, 1 )
timer.performWithDelay(1000, self:removeSelf())
end
end

apple.collision = onLocalCollision
apple:addEventListener( “collision”, apple ) [import]uid: 59140 topic_id: 13320 reply_id: 48927[/import]

you can use
[lua]timer.performWithDelay(2000, function() event.other:removeSelf() end) [/lua]
but if the object bounce when it hits the line then there is chance that it will collide again with the line and will return an error at the above line
then you can use something like this
[lua]if event.other.hit == nil then
event.other.hit = 1
timer.performWithDelay(2000, function() event.other:removeSelf() end)
end[/lua] [import]uid: 71210 topic_id: 13320 reply_id: 48929[/import]

Good news: It works
Bad news: It bounces back up (with both codes that you provided) before disappearing. Also, it would only work once. Please don’t think I’m ungrateful for the code because I am grateful.

I thought it would keep going through the pig but it doesn’t seem too. Do you know of a way to have it go through the pig? It’s not a huge part of my game I just thought it might make it look a little bit more realistic. [import]uid: 59140 topic_id: 13320 reply_id: 48931[/import]

are you saying that the apple is bouncing on the pig ? (poor pig… :0 )

I dint’t understand what you meant by “keep going through the pig” .
can you explain a little more about the result that you are expecting ?
[import]uid: 71210 topic_id: 13320 reply_id: 48932[/import]

He’s such an abused pig :slight_smile:

What’s happening is this… The apple is falling down the screen towards the pig when it reaches the top of the pig (his head) it hits the top of the pig and disappears. But my problem it’s hitting the top of his head not the bucket he is holding

So the result I’m hoping for is this… The apple is falling down the screen towards the pig when it reaches the top of the pig (his head) it keeps going till it’s just over his bucket (which isn’t far from his head) and disappears so that it looks like it is hitting his bucket rather than his head.

Does this make more sense? I know I tend to think that everybody knows what i’m thinking, which isn’t true :stuck_out_tongue: [import]uid: 59140 topic_id: 13320 reply_id: 48937[/import]

so you want the apple to actually hit the pig but to dissapear only after a few second which will give the effect like its going in to his bucket… right ? and now the apple is bouncing on top of the abused pig. right ?
hmmm ok…let me think about it… [import]uid: 71210 topic_id: 13320 reply_id: 48941[/import]

Did you try making the isSensor property of pig physics body to true ?
[lua] physics.addBody( pig, “dynamic”,{ isSensor = true})[/lua] [import]uid: 71210 topic_id: 13320 reply_id: 48943[/import]

YES that is exactly what I want to happen!
No I hadn’t put the isSensor in my code. A couple of questions I have is when I use the “dynamic” it knocks the pig over (further abusing him) can I use kinematic instead? And how does the isSensor help me, how do I code it so that it works as you are thinking? Or is it as easy as what you previously posted, just telling the program that it’s a sensor. [import]uid: 59140 topic_id: 13320 reply_id: 48950[/import]

i tried to simulate your issue
see the below code
[lua]local gameGroup = display.newGroup()
local physics = require(“physics”)
physics.start()

function newObject()
local rand = math.random( 110 )
obj = display.newText(rand,0,0,nil,20)
obj.x = 10 + math.random( 200 )
obj.y = -100
obj.hit = 0
physics.addBody( obj, { density=0.5, friction=0.3, bounce=0} )
end
local dropobjects = timer.performWithDelay( 1000, newObject, 100 )

local groundLine = display.newLine(10, 300, 320, 300)
groundLine.width = 5
groundLine:setColor( 255, 0, 0)

local function removeObject(event)
if event.other.hit == nil then
event.other.hit = 1
–uncomment the below line to remove object on collision
–timer.performWithDelay(100, function() event.other:removeSelf() end)
end
end

physics.addBody( groundLine, “static”)

–use the below code to remove object once it pass the line
–physics.addBody( groundLine, “static”,{ isSensor = true})

groundLine:addEventListener(“collision”,removeObject)[/lua]
can you see something strange when u execute it ?
the objects are actually bouncing on the left side of the line but not on the right side ? do you have any idea why that happens ? [import]uid: 71210 topic_id: 13320 reply_id: 48951[/import]

I’m not on my computer right now (using my iPod) as it’s 1am in Ohio :stuck_out_tongue: but I can’t see anything that would cause that. The only things I could think of changing would be the density to 3.0 cause thats what I have and make the ground line kinematic not static. [import]uid: 59140 topic_id: 13320 reply_id: 48952[/import]

Yes its as easy as setting isSensor property to true.
kinematic din’t allow the object to pass through. kinematic is something same as static but only difference being kinematic object is affected by velocity but static objects are not. [import]uid: 71210 topic_id: 13320 reply_id: 48954[/import]

Something is easy in coding? What’s going on here :smiley:
Thanks for the help I’ll give your ideas a try in the morning and I’ll let you know how it goes. [import]uid: 59140 topic_id: 13320 reply_id: 48955[/import]

Good morning! I tried out your code in my app and attached it to the pig. When I ran it the pig fell right through the bottom and when I set it to dynamic, it fell over after an apple hit it. I entered the code {isSensor = false} to the ground line, the apple stuck to it before disappearing.

I’m going to be gone for a couple of days so if you don’t get a response that’s why. Let me know what you think and have a great weekend! [import]uid: 59140 topic_id: 13320 reply_id: 49004[/import]

No idea why my iPad posting the same post twise…and I can’t find a delete button also… So just editing it . [import]uid: 71210 topic_id: 13320 reply_id: 49009[/import]

Pig fell off ? You abused it a lot. Seems like it can’t stand it anymore. Sorry I posted the wrong code.
Your pig should be static or kinematic and isSensor should be true.
[import]uid: 71210 topic_id: 13320 reply_id: 49008[/import]

The thing is I did make it kinematic/static but it just bounced right off. [import]uid: 59140 topic_id: 13320 reply_id: 49010[/import]