GLBasic forum

Main forum => GLBasic - en => Topic started by: david on 2007-Nov-24

Title: 2d Animations
Post by: david on 2007-Nov-24
I followed the tutorial for the ninja/shuriken game and saw how to make animations with the person moving. How do I make more than 1 animation? It shows how to flip from sprite 0 to 1, but how do I switch from sprite 5 to 1, or 5 to 4 to 3 to 1?

(I have demo version)

Please help me.

Thanks
Title: 2d Animations
Post by: Kitty Hello on 2007-Nov-24
ani = MOD(GETTIMERALL()/100, number_of_frames)
DRAWSPRITE first_animation_id + ani, x,y
Title: 2d Animations
Post by: bigsofty on 2007-Nov-25
Simple example... just add sprites! ;)

Code (glbasic) Select
ALLOWESCAPE TRUE

WHILE 1=1

IF KEY(203) // Change animation on left or right key pressed or idle if none
DIMDATA ani[], 1, 2, 3 // Run left frames (Forwards example)
ELSEIF KEY(205)
DIMDATA ani[], 6, 5, 4 // Run Right frames (Backwards example)
ELSE
  DIMDATA ani[], 10 // Idle
ENDIF


frame_index=INTEGER(GETTIMERALL()/1000) // lower the 1000 for faster animation...
current_frame=ani[MOD(frame_index,LEN(ani[]))] // Get the frame from ani[] array

PRINT "Current_index="+current_frame,100, 100 // drawsprite current_frame, 100,100

SHOWSCREEN

WEND
Title: 2d Animations
Post by: Schranz0r on 2007-Nov-25
hey bigsofty that's handy :D
Title: 2d Animations
Post by: bigsofty on 2007-Nov-25
ty :)
Title: 2d Animations
Post by: david on 2007-Nov-27
Sorry Bigsofty, I'm not very good at glbasic, where do you add on the sprites?
Title: 2d Animations
Post by: Kitty Hello on 2007-Nov-27
uhm... LOADSPRITE? ...maybe?
Title: 2d Animations
Post by: david on 2007-Nov-27
Code (glbasic) Select
LOADSPRITE "ninjathrow1.bmp", 4
LOADSPRITE "ninjathrow2.bmp", 5
LOADSPRITE "ninjathrow3.bmp", 6
LOADBMP "shrine.bmp"




ALLOWESCAPE TRUE

WHILE 1=1

IF KEY(203)    // Change animation on left or right key pressed or idle if none
    DIMDATA ani[], 1, 2, 3 // Run left frames (Forwards example)
ELSEIF KEY(205)
    DIMDATA ani[], 6, 5, 4 // Run Right frames (Backwards example)
ELSE
      DIMDATA ani[], 10 // Idle
ENDIF


frame_index=INTEGER(GETTIMERALL()/1000) // lower the 1000 for faster animation...
current_frame=ani[MOD(frame_index,LEN(ani[]))] // Get the frame from ani[] array

PRINT "Current_index="+current_frame,100, 100 // drawsprite current_frame, 100,100


SHOWSCREEN

WEND
But it doesn't work. What am I doing wrong?
Title: 2d Animations
Post by: Kitty Hello on 2007-Nov-27
Make sure you have the images for 1,2,3 and for 10 loaded as well? The rest seems pretty good to me.
Title: 2d Animations
Post by: bigsofty on 2007-Nov-27
And change this...
Code (glbasic) Select
PRINT "Current_index="+current_frame,100, 100 // drawsprite current_frame, 100,100to this...
Code (glbasic) Select
PRINT "DRAWSPRITE current_frame, 100,100Load frame images, select a direction key, select a frame of animation, display frame... thats all the code does.
Title: 2d Animations
Post by: david on 2007-Nov-28
Thank you so much it worked!!!
Title: 2d Animations
Post by: bigsofty on 2007-Nov-28
Your welcome, remember programming is all about building blocks... this is your reference 'sprite animation' example. Keep the URL in a list and use it for reference when starting a new program.

This list could consist of, for example ...

KeyBoard/Mouse handling
Highscore tables
Modular programming
Animation
Start Game Menu
Character movement
Tilemaps
Collision Detection
Sound and Music
Files


Tick each one off, write a little program to test it, keep a reference of how you got it working. Eventually you will have the skills to put it all together in a larger game... overall keep it fun and ask a question if you get stuck! ;)