Sprite help?

BlitzPlus Forums/BlitzPlus Beginners Area/Sprite help?

Killo869(Posted 2012) [#1]
I'm a little bit familiar with basic and I'm experimenting with sprites and I can make some progress but im at a road block. Is it possible to set up a sprite chart like an array? And have values for having them move up down left and right? or is it gonna have to be separate (like I've been doing.) Any and all help would be highly appreciated =]


GfK(Posted 2012) [#2]
Try LoadAnimImage() - that's probably what you need.


Killo869(Posted 2012) [#3]
I got that far Ive been looking through the reference guide and some tutorials but that only gets me so far i got the loadanimimage() on there but its more complicated then just a single strip of sprites. This is what i got it works but every time i let go of the up/down key it disappears. I know the problem already but i don't know how to fix it and Ive been trying for a good few hours now

AppTitle "Sprite Test."
Global sprite
Global time
Global frame1
Global speed = 1


Graphics 800,600,16
SetBuffer BackBuffer()
spriteup = LoadAnimImage("anim1.bmp",19,29,0,4)
spritedown = LoadAnimImage("anim2.bmp",19,28,0,4)
spriteupx = 320
spriteupy = 240
MaskImage spriteup,255,0,255
MaskImage spritedown,255,0,255



While Not KeyDown(1)
Cls

If KeyDown(200) DrawImage spriteup,spriteupx,spriteupy,frame1
If KeyDown(200) spriteupy = spriteupy - speed

If MilliSecs() > time + 200
time = MilliSecs()
frame1 = (frame1 + 1) Mod 4
EndIf


If KeyDown(208) DrawImage spritedown,spriteupx,spriteupy,frame1
If KeyDown(208) spriteupy = spriteupy + speed

If MilliSecs() > time + 200
time = MilliSecs()
frame1 = (frame1 + 1) Mod 4
EndIf


Flip
Wend


GfK(Posted 2012) [#4]
Yeah, I see what's up with that.

Your code is ONLY drawing the sprite if either the up or down key is held. I'm not going to write the code for you, because I'm pretty sure you can do it yourself once the idea's been pointed out.

First, between EVERY Cls and Flip, you need to be drawing the sprite. You know that much.

There are two things that need to happen when a key is pressed:
1. increment the frame number
2. move the sprite

So basically something like:
If KeyDown(200)
  frame1 = (frame1 + 1) Mod 4
  spriteupy = spriteupy - speed
EndIf


One further point - you're using Millisecs() to slow down the animation speed. That's fine, but I would suggest using a Float value for the frame number, i.e.:
Global frame1#

...then later...

frame1 = frame1 + 0.1
if Floor(frame1) > 3 then frame1 = 0

...then to draw it...

DrawImage spriteup,spriteupx,spriteupy,Floor(frame1)

DrawImage uses an integer for the frame number so you don't really need to add the Floor() function call, but it might help with clarity when you're reading the code.

Just some stuff to think about!

Last edited 2012


Killo869(Posted 2012) [#5]
I finally got! before i guess i was over-thinking it a little too much and it was pretty obvious. and i also tried using the Floor(frame1) and it does make a lot more sense reading it. Thank you! And also one last thing, is it possible to have Blitz read an entire sprite chart like an array instead of individual sprite strips for each action? It just seems really inefficient the way Im doing it.


Yasha(Posted 2012) [#6]
is it possible to have Blitz read an entire sprite chart like an array instead of individual sprite strips for each action?


?

Do you mean a rectangular block of sprites instead of a single horizontal row? If so, the answer is yes: you don't have to do anything different; it will automatically detect that you have a multi-row source image if the dimensions you specify for each sprite, and the number, don't align with the width of the image. The sprites still have to be of uniform size though.

If you mean to load the images into an array data type... well that's pretty much what LoadAnimImage does behind the scenes anyway so unless you specifically want X, Y access to the frames instead of just a single-value index, this is already done for you (effectively: you don't get to see the array but there is one hidden in the anim-image).

Since the sprites are loaded into a single linear structure with a single indexing system, you could write a wrapper function that goes over the top to let you select frames "by animation" within the greater anim-image. It would have to get the information from somewhere independently, but it might help you keep your animation logic more organised.


Killo869(Posted 2012) [#7]
I got it to work with the entire sprite set. It went a lot better then the first time I tried it and I also noticed that it was smoother because it was all one image instead of 4 different sprite strips. And after successfully compiling that code, I understand what I wrote a lot better. Except for this one line however.
frame1=(frame1 + 1) mod 4*
I know that it's used to control how fast the sprite cycles through frames but idk how it does it
And the 4 had to be changed depending on which direction I had my sprite move.
And lastly thanks again for all your help. That bit is gonna be a huge help in the future if i ever need a reference


Killo869(Posted 2012) [#8]
I would post the code but I'm writing this from my phone because I lost Internet unfortunately.