How to "jump" after few Quad of stripe

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
tagiyevv
Prole
Posts: 3
Joined: Thu May 16, 2013 9:41 pm

How to "jump" after few Quad of stripe

Post by tagiyevv »

Hello everyone

I would like to share my problem about the platformer that I have been creating (is this the word to use?). So, here is:

When pressed the "up" key the "player" should jump, which there is not any problem with that. But what I want to do is to set a time delay that the "player" to wait for 5 frames of stripes and then jump, meanwhile, as it should, the animation to continue. I added the " *.love ". Sorry that the code is kind of messy.

Thanks in advance
walkertest.love
(829.38 KiB) Downloaded 109 times
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: How to "jump" after few Quad of stripe

Post by veethree »

What do you mean by stripes?

To set a time delay you can do something like this in love.update

Code: Select all

t = t + dt
	if t > delay then
		--Do things
	end
t being a variable for the current time and delay for the amount of time to wait.
tagiyevv
Prole
Posts: 3
Joined: Thu May 16, 2013 9:41 pm

Re: How to "jump" after few Quad of stripe

Post by tagiyevv »

veethree wrote:What do you mean by stripes?

To set a time delay you can do something like this in love.update

Code: Select all

t = t + dt
	if t > delay then
		--Do things
	end
t being a variable for the current time and delay for the amount of time to wait.
Thanks for the reply. By "stripes" I meant quads of the image file used for jump animation.
And I ques, if use the delay like you suggest, the animation won't start until the condition satisfied.
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: How to "jump" after few Quad of stripe

Post by veethree »

tagiyevv wrote:
veethree wrote:What do you mean by stripes?

To set a time delay you can do something like this in love.update

Code: Select all

t = t + dt
	if t > delay then
		--Do things
	end
t being a variable for the current time and delay for the amount of time to wait.
Thanks for the reply. By "stripes" I meant quads of the image file used for jump animation.
And I ques, if use the delay like you suggest, the animation won't start until the condition satisfied.
I have little experience with animations, But if there's a way to detect which frame of the animation is currently being drawn you could start the animation when the jump key is pressed, then wait until the 5th frame is being drawn and then jump. This would pretty much be a simple if statement. If the jump key is in love.keypressed you could have a boolean that's false by default and set it to true when the jump key is pressed, Then in love.update you could check if the boolean is true, then check if the frame is 5, if it is then do the jumping thing.
tagiyevv
Prole
Posts: 3
Joined: Thu May 16, 2013 9:41 pm

Re: How to "jump" after few Quad of stripe

Post by tagiyevv »

veethree wrote: I have little experience with animations, But if there's a way to detect which frame of the animation is currently being drawn you could start the animation when the jump key is pressed, then wait until the 5th frame is being drawn and then jump. This would pretty much be a simple if statement. If the jump key is in love.keypressed you could have a boolean that's false by default and set it to true when the jump key is pressed, Then in love.update you could check if the boolean is true, then check if the frame is 5, if it is then do the jumping thing.
I have been doing exactly what you suggest, but somehow, it doesn't seem to work :shock:. I will try again and keep those who are interested posted. Thanks again.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: How to "jump" after few Quad of stripe

Post by kikito »

If you are willing to use an external library, anim8 can help you in controlling the frames, and cron.lua.

The code would look similar to this:

Code: Select all

local anim8 = require 'anim8'
local cron = require 'cron'

...

-- This assumes that your image is called image, and your frames are 32x32
local g32 = anim8.newGrid(g32(32,32, image:getWidth(), image:getHeight())

function createPlayer()
  -- This assumes that the running animation is the 5 first quads of the first row, 
  -- and jumping is the second row, with 3 frames.
  local runningAnim = anim8.newAnimation(g32('1-5',1), 0.1)
  local jumpingAnim = anim8.newAnimation(g32('1-3',2), 0.1)
end

function drawPlayer(x,y)
  player.anim:draw(image, x, y)
end

function startJumping()
  player.anim = runningAnim
  cron.after(0.5, function() -- runningAnim is 5 frames of 0.1 seconds each = 5*0.1 = 0.5
    player.anim = jumpingAnim
    -- or other stuff that you want to do, like starting moving up
  end)
end
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests