Page 1 of 1

How i can create an animated sprite of grid based RPG game in love2d?

Posted: Mon Sep 05, 2016 10:40 am
by Valentio
I explain the question: I am creating a game of Pokemon and i want to create the character style movement that it has.
The quads of the map tileset are 32x32, and the quads of the character tileset are 32x48 . Therefore, to move it you need to add the X or Y character 32 pixels. So far easy but things get complicated when we want to animate the sprite character. In this case we have to Professor Oak. His tileset shows how should have 4 frames for the 4 basic moves up, down, left and right.

About this problem, I decided to program it like this: every 8 pixel motion wasd, using love.keyboard.isDown, show the corresponding frame of each of their movements. The problem is shown. Each time you press one of these keys, only makes the movement of 8 pixels, and stays in the next frame. That is, to do the entire route of a quad, is tightened 4 times the same key. The question is how to make the movement of 8 pixels 4 times consecutively, so that the user cannot press any key till the movement of 32 pixels is completed, avoiding multiple errors of choice wrong frame, and preventing not grid movement. I tried to do with "for" loops, but does not draw correctly in love.draw () function, its logical.

I pass the script of the exactly problem:
funciones-pj.lua
(4.08 KiB) Downloaded 191 times
I hope that there are any function I can use to do this in an optimized way. If it does not work because of the way it is programmed, I can change it.

Greetings.

Sorry for my horrible english. The names of some functions are in spanish, sorry for that.

Re: How i can create an animated sprite of grid based RPG game in love2d?

Posted: Mon Sep 05, 2016 2:33 pm
by pgimeno
1. You can create animations that use sprite sheets using an animation library, for example anim8, https://github.com/kikito/anim8
2. Please attach the file to the post. I can't download from the link you have provided.
3. (Off-topic): If you want your game to also work for French people too, please support both WASD and ZQSD. Make Z an alias for W, and make Q an alias for A, or alternatively, use the second parameter of [wiki]love.keypressed[/wiki] available since LÖVE 0.10 (or [wiki]love.keyboard.isScancodeDown[/wiki]).

Re: How i can create an animated sprite of grid based RPG game in love2d?

Posted: Mon Sep 05, 2016 10:37 pm
by Valentio
I have uploaded the exactly script where is the problem. Thanks for sharing the library Pgimeno :)

Re: How i can create an animated sprite of grid based RPG game in love2d?

Posted: Tue Sep 06, 2016 12:39 am
by pgimeno
So you want the character to only move in full grid steps, right? Well, you can have something like a flag isMoving that is set when the character starts moving, and reset after 32 pixels. Before checking the keys you would check if your character isMoving and not react to controls in that case.

Does that make sense?

Re: How i can create an animated sprite of grid based RPG game in love2d?

Posted: Tue Sep 06, 2016 9:01 am
by Valentio
Okay, but now the problem is how i can repeat the code 4 times for moving 8x4 times, and draw 4 times the moving?

Re: How i can create an animated sprite of grid based RPG game in love2d?

Posted: Tue Sep 06, 2016 11:02 am
by pgimeno
Count from 0 to 31 and draw the sprite number counter % 8 (or counter % 8 + 1 if you count sprites starting in 1).

The % operator does that for you, for example:

Code: Select all

for i = 0, 31 do
  print(i % 8)
end
This prints 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7.

But really, an animation library may make things much easier for you.

Re: How i can create an animated sprite of grid based RPG game in love2d?

Posted: Tue Sep 06, 2016 12:33 pm
by Valentio
I make it!!
The problem was the structure of code. I have implemented two new variables (one of them is isMoving, thanks :D) and i call the function muevePj() // chMove() inside the same function. I have programmed them in such a way that the function is "recalled" four times. And now the users cant touch any key before the function ends, and the love.draw() runs perfectly :P