Change Image after Object stops moving

Well, I have been playing with Corona all weekend and I love it!! I’m hoping there are some corona experts out there who can help me before I pull out all my hair =)

I have a couple questions:

  1. How do I track the velocity of an object? I want to perform an action on my objects when they are not moving. I am not sure how to set up a listener for when objects stop.

Note: My object is a movieclip, with physics applied to it. When it stops moving across the screen I want to stop the animation. No point in having my little man walking when he’s not going anywhere =)

  1. Also, how can I increase the animation speed based on the velocity of the object? If my man is bouncing around quickly then his little legs should be animating faster =)

  2. How do you keep objects with physics applied to them constantly moving? I don’t want to apply a force to them because that increases their speed. I just want them to bounce around the screen at a slow pace. Similar to the fishies sample game BUT I applied physics to my object so they bounce off each other.

Thanks in advance for your help!

Jaz

[import]uid: 76665 topic_id: 12584 reply_id: 312584[/import]

Hey Jaz,

  1. http://developer.anscamobile.com/content/game-edition-physics-bodies#body:getLinearVelocity - That talks about getting the linear velocity of an object, you can use it to perform an event only when it’s not moving any more.

  2. http://developer.anscamobile.com/reference/sprite-sheets - You’d start here, then you’d have a function to change what was playing (or at what speed) when appropriate. You could pre-prepare three different speeds (for example) to make it easy to switch between them.

  3. To keep the speed the same, you’d use the first step, only if the speed was lower than X you’d set it back to X.

Peach :slight_smile: [import]uid: 52491 topic_id: 12584 reply_id: 46079[/import]

Thank you Peach for pointing me int he right direction. Unfortunately I’m still a newbie and have more silly questions =)

  1. To find velocity I saw:
    vx, vy = myBody:getLinearVelocity()

I just am not sure how I put that into a listener on an object.

[code]
–Create the object
function newBall()
ball = movieclip.newAnim({ “img1.png”, “img2.png”, “img3.png”, “img4.png” })
myAnim:play()
ball.x = display.contentWidth*0.5
ball.y = -100
physics.addBody( ball, { density=0.9, friction=0.3, bounce=0} )

–Apply the listener
ball:addEventListener( “enterFrame”, ballstopped() )

end

–Listener function
function ballstopped(event)
local vx,vy = event.target:getLinearVelocity()
local m = math.sqrt((vx*vx)+(vy*vy))
if m —Consider this object as stopped
end
[/code]
Thanks again!
Jaz [import]uid: 76665 topic_id: 12584 reply_id: 46089[/import]

jazzerup - I believe you’ll use the enterframe event listener and then test for the velocity within it. There is no event that triggers when it is stopped that I know of… BTW you can also set up custom events… however the dispatch event will probably still happen within the enterframe event.

[code]
ball.enterFrame = new function()

local vx,vy = event.target:getLinearVelocity()

if vx == 0 and vy == 0 then
—Consider this object as stopped
end

end
ball:addEventListener( “enterFrame”, ball)

[/code] [import]uid: 29520 topic_id: 12584 reply_id: 46091[/import]

—UPDATED!! Sorry, I was wrong, the code is not working. Anyone have any ideas on how I can fix my code below so I know when my ball stops moving? Thank you =)

[code]
–Who cares how much battery they have left, let’s hide it
display.setStatusBar( display.HiddenStatusBar )

–Cannot forget those external files and make sure they are in the folder with your main.lua
local movieclip = require(“movieclip”);
local physics = require(“physics”);

–If the ball is going to bounce off the walls you will need some physics
physics.start()
–No gravity means it will not drop to the floor.
physics.setGravity(0,0)

–Create the walls
local wallThickness = 10
– Left wall
local wall = display.newRect( 0, 0, wallThickness, display.contentHeight )
physics.addBody(wall, “static”, {friction=0, bounce = 1})
– Top wall
wall = display.newRect(0,0, display.contentWidth, wallThickness)
physics.addBody(wall, “static”, {friction=0, bounce = 1})
– Right wall
wall = display.newRect(display.contentWidth - wallThickness, 0, wallThickness, display.contentHeight)
physics.addBody(wall, “static”, {friction=0, bounce = 1})
– Bottom wall
wall = display.newRect(0, display.contentHeight - wallThickness, display.contentWidth, wallThickness)
physics.addBody(wall, “static”, {friction=0, bounce = 1})
–Create the ball
function newBall()
ball = movieclip.newAnim({ “ball_blue.png”, “ball_green.png”, “ball_red.png”, “ball_pink.png” })
ball:play{ startFrame=1, endFrame=1, loop=0, remove=true }
ball.x = display.contentWidth/2
ball.y = display.contentHeight/2
physics.addBody( ball, { density=0.9, friction=0.3, bounce=0} )
ball:setLinearVelocity(30,50)

end

–To test if our velocity listener is working we need to stop the ball
function stopball()
print(“ball stopped Function run”)
ball:setLinearVelocity(0,0)
end

–Our Velocity Tracker to see when the ball has stopped
enterFrame = new function VelocityTracker()
local vx,vy = event.target:getLinearVelocity()
if vx == 0 and vy == 0 then
print(“Yes, the ball stopped!! Win!”)
end
end

–Let’s create our ball now
newBall()
–add the listener
ball:addEventListener( “enterFrame”, ball)

–After 3000 miliseconds we will stop our ball
local ballstop = timer.performWithDelay( 3000, stopball, 1 )

–VIOLA!!! It works!!!
[/code] [import]uid: 76665 topic_id: 12584 reply_id: 46167[/import]

one piece of advice I can give is, samples are your friends. You absolutely need to familiarize yourself with the documentation, but when you need to see it in real world use, nothing beats the samples. I have to give credit to Ansca on how good the bundled samples are. Add that to the user submitted code and you should easily be able to figure out little issues like this.
use this after you create the walls and it should work.

[lua]–Create the ball
function newBall()
ball = movieclip.newAnim({ “ball_blue.png”, “ball_green.png”, “ball_red.png”, “ball_pink.png” })
ball:play{ startFrame=1, endFrame=1, loop=0, remove=true }
ball.x = display.contentWidth/2
ball.y = display.contentHeight/2
physics.addBody( ball, { density=0.9, friction=0.3, bounce=0} )
ball:setLinearVelocity(30,50)
–Our Velocity Tracker to see when the ball has stopped
function ball:enterFrame(event)
local vx,vy = self:getLinearVelocity()
if vx == 0 and vy == 0 then
print(“Yes, the ball stopped!! Win!”)
end
end
–add the listener
Runtime:addEventListener( “enterFrame”, ball)
end

–To test if our velocity listener is working we need to stop the ball
function stopball()
print(“ball stopped Function run”)
ball:setLinearVelocity(0,0)
end

–Let’s create our ball now
newBall()

–After 3000 miliseconds we will stop our ball
local ballstop = timer.performWithDelay( 3000, stopball, 1 )[/lua] [import]uid: 9187 topic_id: 12584 reply_id: 49328[/import]