Sprite Animation Problem

BlitzMax Forums/BlitzMax Beginners Area/Sprite Animation Problem

shepherd(Posted 2016) [#1]
Hello. I just recently started with BlitzMax. Anyways my problem is that my Image/sprite is not animating when I press any of the 4 arrow keys. Here is my code section regarding the animation and key press,




Brucey(Posted 2016) [#2]
i, j, k and l are always zero - since they are declared as locals in your UpdatePlayer() method.

Perhaps you would be better calling them something more useful and making them Fields in your Type.


shepherd(Posted 2016) [#3]
ok I will try making them as fields in my type. I tried with global but still no animation.It's like it always in the first frame(0).


Derron(Posted 2016) [#4]
If you declare them global, do this _outside_ of the method "UpdatePlayer".

Each time you call UpdatePlayer() now, you reset the variables i, j, k and l to 0 before you check for "down" etc.


@fields
"i" and "j" seem to be "frames"-numbers. As you cannot walk down and up in the same time, you might think of making them "share" the same variable.
Then you reset the frame to "0" as soon as you start moving down from another direction (so if the direction changes - reset, if the direction was already the same, skip reset).

If you do so, your type might just contain "Field frame:int".
Having this, allows for multiple units having their individual frame number instead of sharing a big single global.



bye
Ron


Brucey(Posted 2016) [#5]
Globals are bad. Let's use something else here :-)


Derron(Posted 2016) [#6]
I already edited my above post.

Your type might look this way:

type TUnit
  Field direction:int
  Field frame:int
  Field sprite:TImage
  Field x:int, y:int 
...
End Type


These "direction", "frame" are individual variables for each instance of the type TUnit (eg the player or a mob). They are persistent for the life of the instance.


bye
Ron


therevills(Posted 2016) [#7]
What is it at the moment with people using really bad variable naming!?


shepherd(Posted 2016) [#8]
Thanks all. I already added the frame variable to my type as suggested by brucey and all of you. The Animation is working now but the animation plays only after I release the respective arrow key. I want the animation to play during the key press only. Once I release the arrow key i want the animation to stop. Right now it's the opposite.


markcw(Posted 2016) [#9]
What is it at the moment with people using really bad variable naming!?

What do you mean? Everyone is at different levels of skill.


Derron(Posted 2016) [#10]
instead of
If key... direction = x

You do
Local newdirection=0 'reset in all cases
If keyDown(...) then newdirection=2
If keyDown(other key) then newdirection=3

If direction <> newdirection
 'Reset frames
 'Adjust speed...
 direction = newdirection
Endif


This changes direction as soon as none of the interesting keys is down.

Bye
Ron


shepherd(Posted 2016) [#11]
@Ron

Thanks,but I already figured it out.Anyways, Do you have any suggestions regarding how to change the speed of animation. I am using CreateTimer and WaitTimer but it just feels so laggy when I use them at low values. Like if I set CreateTimer to 12 or less than 12 value the player moves slow like there is lag. And If i give 15 or more value the animation speeds up a lot. So any Idea or suggestions regarding on how to control the animation speed?


dw817(Posted 2016) [#12]
What do you mean? Everyone is at different levels of skill.


Munch, you get a medal for that great statement of understanding you made.



Do we all type the same way ? No. Me ? I tilt the external keyboard at a hard 45-degree angle to the left. Does that make me a bad typist ? Not at 135wpm with high accuracy it doesn't. :)


therevills(Posted 2016) [#13]
What do you mean? Everyone is at different levels of skill.


What I mean is that it is really really bad coding practice to name your variables using single letters. I just pointed out the fact that lately I've seen some really bad naming for variables.

It doesnt take any skill to name your variables to something meaningful. These days you dont need to have single letter variables, PCs have so much more room and you will find it so much easier to understand your own logic if you name them correctly.

Yes I am a professional software developer by trade, I perform code reviews for peers nearly every day and I fail the review if they are not up to standard or have poor naming standards.


dw817(Posted 2016) [#14]
Well now, Therevills, I think "I" is a perfectly good variable for a FOR/NEXT loop. And if you need one inside, there is always "J"

I suppose someone could do:
For Index=0 to EndValue
  For InnerIndex=0 to SecondaryEndValue
But the way I see it, I and J are pronouns. You remember that good old Schoolhouse Rock song ?




Derron(Posted 2016) [#15]
I don't think he was talking about temporary index variables but "properties".
So instead of "frameNumber" you use i,j,k,...

The more complex code gets, the more an "understandable" variablename helps. This also counts for comples function calls:

DrawStretchedSprite(sprite, x, y, w, h, frame)
is maybe easier to recognize than
DrawStretchedsprite(s,x,y,i,j,k)


bye
Ron


dw817(Posted 2016) [#16]
Hi Ron. I do this ! (mostly, sort of, when I can remember) :)