if i use the api remove.self( ) on a collision function and the object get removed how would i get it to reset to its beginning position
Hi @tejwinderthind,
I’m not clear on your intention. If you’re destroying the object, how would you reset it to the starting point? Wouldn’t it be better to just move it back to the starting point instead of destroying it?
Best regards,
Brent
how would i reset it to its starting point
There are many, many ways, and challenge to each. You’re not giving us enough context, to answer you properly, but here is one simple example.
local circle = display.newCircle( 100, 100, 10 ) circle.x0 = circle.x circle.y0 = circle.y function circle.onComplete( self ) self.x = self.x0 self.y = self.y0 end transition.to( circle, { x = 200, time = 2000, onComplete = circle } )
The circle will move from x=100 to x=200 over a period of 2 seconds, then instantly move to its starting position.
Note: You can turn my code above into a function to call any time as follows:
local circle = display.newCircle( 100, 100, 10 ) circle.x0 = circle.x circle.y0 = circle.y function circle.goHome( self ) self.x = self.x0 self.y = self.y0 end
Later…
circle:goHome()
If you have a physics body and want to remove any velocity from the the object, you could do this:
function circle.goHome( self ) self.x = self.x0 self.y = self.y0 if( self.setLinearVelocity ) then -- Won't remove inertia though... self.setLinearVelocity(0,0) self.angularVelocity = 0 end end
Not sure if there would be any unintended consequences, but you can also remove any velocity from an object by changing the body type to static:
[lua]movingObject.bodyType = “static”[/lua]
You could grab the original bodyType, set to static to stop it in its tracks, and then set the original bodyType back, such as:
[lua]local oldBody = movingObject.bodyType
movingObject.bodyType = “static”
movingObject.bodyType = oldBody
[/lua]
Jay
i want the object to go back to its starting position after a collision function
my code the transition is not working
[lua] local function onLocalCollision( self, event )
physics.stop()
transition.to( crate, { x=(w-50), y=(h-50), onComplete=onLocalcollision } )
end
crate.collision = onLocalcollision
crate:addEventListener(“collision”, crate)
– all display objects must be inserted into group
sceneGroup:insert( background )
sceneGroup:insert( grass)
sceneGroup:insert( crate )
sceneGroup:insert( physicsButton )
end[/lua]
Hi @tejwinderthind,
This code seems out of order. Why are you calling the same “onLocalCollision” function as the “onComplete” of the transition inside of that same function?
Also, I suggest that you be careful when (or if) you “stop” the physics engine. Typically the only reason to do so is if you’re absolutely 100% finished with all physics processes and you want to destroy the physics world outright. If there’s any possibility you’ll return to using physics later in the game, it’s much better to pause the physics engine for scenes you’re not using physics in.
Brent
The crate still does not transition i tried it this way for some reason the crate won’t move [lua] local function onLocalCollision( self, event )
print(“gg”)
physics.pause()
transition.moveTo( crate, { x=50, y=50, onLocalcollision = “collision” } )
end
crate.collision = onLocalcollision
crate:addEventListener(“collision”, crate)
– all display objects must be inserted into group
sceneGroup:insert( background )
sceneGroup:insert( grass)
sceneGroup:insert( crate )
sceneGroup:insert( physicsButton )
end
[/lua]
Hi @tejwinderthind,
Can I see how you’re creating the crate? Is it in the proper scope so that the “onLocalCollision()” function knows what “crate” is? Also, that “onLocalCollision” parameter that you’ve put inside the transition call is incorrect and invalid. Are you attempting to do something when the transition completes?
Best regards,
Brent
[lua] local crate = display.newImageRect( “crate.png”, 90, 90 )
crate.x, crate.y = 160, 45
crate.rotation = 0
– add physics to the crate
physics.addBody( crate, { density=1.0, friction=0.3, bounce=0. } )
[/lua]
In your “onLocalCollision()” function, add this line and tell me what it says in the console:
[lua]
print( “CRATE:”, crate )
[/lua]
Brent
CRATE: table: 0x7fad9cda4480
OK, now put the same line directly after you create the crate (local crate = ). Do the output values match exactly?
Hi @tejwinderthind,
I’m not clear on your intention. If you’re destroying the object, how would you reset it to the starting point? Wouldn’t it be better to just move it back to the starting point instead of destroying it?
Best regards,
Brent
how would i reset it to its starting point
There are many, many ways, and challenge to each. You’re not giving us enough context, to answer you properly, but here is one simple example.
local circle = display.newCircle( 100, 100, 10 ) circle.x0 = circle.x circle.y0 = circle.y function circle.onComplete( self ) self.x = self.x0 self.y = self.y0 end transition.to( circle, { x = 200, time = 2000, onComplete = circle } )
The circle will move from x=100 to x=200 over a period of 2 seconds, then instantly move to its starting position.
Note: You can turn my code above into a function to call any time as follows:
local circle = display.newCircle( 100, 100, 10 ) circle.x0 = circle.x circle.y0 = circle.y function circle.goHome( self ) self.x = self.x0 self.y = self.y0 end
Later…
circle:goHome()
If you have a physics body and want to remove any velocity from the the object, you could do this:
function circle.goHome( self ) self.x = self.x0 self.y = self.y0 if( self.setLinearVelocity ) then -- Won't remove inertia though... self.setLinearVelocity(0,0) self.angularVelocity = 0 end end
Not sure if there would be any unintended consequences, but you can also remove any velocity from an object by changing the body type to static:
[lua]movingObject.bodyType = “static”[/lua]
You could grab the original bodyType, set to static to stop it in its tracks, and then set the original bodyType back, such as:
[lua]local oldBody = movingObject.bodyType
movingObject.bodyType = “static”
movingObject.bodyType = oldBody
[/lua]
Jay