Platform game question - regarding falling character

Hi,

I have a platform game where a character can walk left and right…
sometimes when the character walks off the platform and drops to a platform below, I want to change the sprite to a falling sprite…

my question is how can I track when he falls off the platform? I can look at the linear velocity in the y direction, and if it is more than zero change the sprite. But does that mean I have to add an event in the enterframe on the Runtime…so the function is constantly being checked? is this the best method? or can someone suggest something better? seems like a waste… to check on enterframe every single time

I am using physics as well…

Thanks [import]uid: 67619 topic_id: 25683 reply_id: 325683[/import]

Hey.

You could check the velocity every frame, but personally I’d set a flag and change it to true when the character is touching the platform and false when the collision ends. That way you wouldn’t need to check every frame as such. The collisions would already be being listened to as you’re using physics. [import]uid: 67933 topic_id: 25683 reply_id: 103864[/import]

When character first collides with ground record the character y position and state. Then check the character y position every frame. If the character falls off a platform you’ll know because

if character.y \> recorded.y and character.state == "ground" then  
 -- falling code  
 character.state = "falling"  
end   

[import]uid: 38820 topic_id: 25683 reply_id: 103887[/import]

spider_newgent

Good point will try that
Glennbjr
That would work… but I was really trying to avoid putting a function on “enterframe”

Thanks guys [import]uid: 67619 topic_id: 25683 reply_id: 103934[/import]

Been there done that. I’ve already tried what spider_newgent suggested. Months ago. It didn’t work for me but let us know how you make out or if you find a better way of doing things. [import]uid: 38820 topic_id: 25683 reply_id: 103937[/import]

If you want to do jumping in your platform game, you’ll need something like this. The following code ensures you can only jump when the character is in contact with the ground (otherwise it would be possible to jump while in mid air!). Note you need integer ncoll since multiple collisions are possible (eg wall+floor). You can then switch the sprite in the if clause below.

[lua]function collide(event)

if (event.phase==“began”) then
ncoll=ncoll+1
onground=true
elseif (event.phase==“ended”) then
ncoll=ncoll-1
if (ncoll==0) then onground=false end
end
end

:

– Jump button pressed
if (onground) then
ego:setLinearVelocity(-40,-200)
end

ego:addEventListener(“collision”,collide)[/lua]
[import]uid: 84768 topic_id: 25683 reply_id: 103954[/import]