Are you using the physics library? If so, you could use getLinearVelocity to get the character’s y-velocity (http://docs.coronalabs.com/api/type/Body/getLinearVelocity.html).
- Andrew
Thanks. I’ll give it a try.
I’ll let you know if it worked.
Ok.
So, yeah I use the “setLinearVelocity” to make the character jump.
When the character jumps, Linear Velocity is set to (0, -600).
Could you help me put together the function that will trigger the sprites to animate?
AKA if character’s Linear Velocity y value is more than 0 then play sprites
or something like that.
Well, I won’t help you write it, but I’m happy to give some suggestions. :-)
You need a way of detecting when the linearVelocity passes the threshold, so they question you need to ask yourself is: how will my code know when that happens? Probably what you would do is use an enterFrame listener to check each frame what the velocity is. You could also have a flag on your character that indicates which sprite sequence is playing. If the velocity is not consistent with the animation you want, then you would switch to the other animation.
- Andrew
Ok thanks for these tips.
I’ll go back to typing and try to make it work…
I don’t know what could I use to detect if linearVelocity is bigger than selected value. I’ve never seen any statements like that.
After all this is the first game that I’m building and something like that thing above is just too big leap forward for me right now.
I think I’m gonna look for other ways to trigger that sprite to play…
Now I’m trying to play the sprite using the tap event.
The code looks like this
function jump:tap(event)
if(event.numTaps >= 1) then
char2sprite:setSequence(“jumping”)
char2sprite:play()
end
end
Runtime:addEventListener(“tap”, jump)
Jump is an image that I have inserted. Jumping is the sprite set containing one sprite- the jumping sprite.
The function that makes the character jump is somewhere else.
The only purpose of this code is playing that one single sprite.
However there is a problem.
The sprite will play, but not only when you tap the jump button/ image. Instead, it will play that sprite if you click ANYWHERE on the screen.
I don’t know what is the problem, but most likely the problem is in the code above.
Anyone knows what causes that problem?
You have added the touch listener to the Runtime, which means the whole screen will react to the touch event.
Add the listener to your jump button object instead.
Oh thanks for clearing that up for me. Makes sense.
I never knew what exactly Runtime does. I just put it there because I knew it had to be there for function to work.
I will change my code in a minute…
Might I suggest you download some example code and figure out how it works, play around with it, make it do different things before you dive right in and code something from scratch. That’s the way I first learnt Corona and Lua two years ago, playing around with the Ghosts vs Monsters game, not sure that will still work with the newer Corona builds though.
Something like this perhaps:
http://developer.coronalabs.com/code/side-scroller-game
Also, here’s some VERY untidy, uncommented and poorly structured code I was messing about with to implement some of Sonic’s basic moves - running, jumping, spinning, skidding, crouching, looking up. However it does work and might be useful to learn from. The actual ‘game’ code is in app.lua.
Sure, some examples would be really nice.
Thanks for those links I’m sure I’ll find use of them.
It’s similar to what I’m trying to make. A sidescroller like one of those that Nintendo made for GameBoy Advance. Something like Metroid, Megaman, or Warioland.
I don’t think I’ll do any more coding today, cause it’s midnight right now in my country and when I code late at night I just keep making stupid mistakes. So I’ll take a look at those links.
I’ll prolly code tommorow and I’ll post future feedback and questions about jumping and falling here.
Just a quick question. I apologise if it is a little off-topic.
What is the maximum allowed number of frames on the sprite sheet?
EDIT: tried it out myself. Turns out that one sprite sheet can’t have more than 25 frames on it. Kinda lame. I guess I will have to make more sheets and find a way to interact with those as well.
It has been almost 3 days now. Still having problems playing the falling sprite when the character’s y is dropping…
Could anyone help me out with this? I can’t find a tutorial or anything.
I must find a way to play the falling sprite when the character’s y value is dropping.
Try something like this:
[lua]
– put this after you create char2sprite
char2sprite.status = “standing”
— put this above all the storyboard functions, below your variable declarations
local gameLoop = function ()
local vx, vy = char2sprite:getLinearVelocity()
if vy > 0 and char2sprite.status ~= “falling” then
char2sprite.status = “falling”
char2sprite:setSequence(“falling”)
char2sprite:play()
end
end
Runtime:addEventListener(“enterFrame”,gameLoop) – put this in enterScene
[/lua]
Thanks for this!
I’ll try it out right away.
Why not just use previous X/Y values?
Like so:
[lua]
char2sprite.prevX, char2sprite.prevY = char2sprite.x, char2sprite.y
local function checkDirection()
if char2sprite.prevX ~= char2sprite.x then
if char2sprite.x > char2sprite.prevX then
print(“Moving right”)
elseif char2sprite.x < char2sprite.prevX then
print(“Moving left”)
end
end
if char2sprite.prevY ~= char2sprite.y then
if char2sprite.y > char2sprite.prevY then
print(“Moving down”)
elseif char2sprite.y < char2sprite.prevY then
print(“Moving up”)
end
end
char2sprite.prevX, char2sprite.prevY = char2sprite.x, char2sprite.y
end
[/lua]
- Caleb
OK, tried out both ways, that’s why it took me so long to reply.
In my case the second one worked out better (the one that Caleb P typed).
Because I am not YET using the Storyboard- might consider adding it.
I have never thought that something like prev.y or prev.x exist. Never seen those before.
I was stuck on this problem for days and it made my day when I saw your replies. Helped me out a bunch!
I really appreciate both of you helping me and I think that this problem is solved.
Thanks guys!
Just to clarify, prevX and prevY actually don’t exist - I just created them (on line 2 of the code I posted) and updated them each time the checkDirection() function was called.
Glad I could help!
- Caleb
Ah, nice.
It would take me quite some time to come up with such code.
It also gave me more ideas on how I could achieve diffirent things.