Jump to content
Sign in to follow this  
Philsan

Slowing sprite's animation

Recommended Posts

How may I slow a sprite's animation?

 

player1:
PLAYER DEFINITION
end

drawscreen

player1:
PLAYER DEFINITION
end

drawscreen

player1:
PLAYER DEFINITION
end

drawscreen

player1:
PLAYER DEFINITION
end

drawscreen

Share this post


Link to post
Share on other sites

You use can use a counter and use if-thens or gosubs or gotos. Here's an example using only if-thens:

 

  rem  *****************************************************
  rem  *****************************************************
  rem  * 
  rem  *  Animation Example Using Only if-thens
  rem  * 
  rem  *****************************************************
  rem  *****************************************************





  rem  *****************************************************
  rem  *
  rem  *  Create aliases for variables.
  rem  *
  rem  *****************************************************
  dim P0_Anim_Count = a
  dim P0_Flip = b
  dim rand16=z



  rem  *****************************************************
  rem  *
  rem  *  Variable descriptions.
  rem  *
  rem  *****************************************************
  rem  *
  rem  *  P0_Anim_Count -- Player 0 animation counter.
  rem  *
  rem  *  P0_Flip -- Tells the program which way player 0 should face.
  rem  *
  rem  *  rand16 -- This 16-bit random number generator gives you better random numbers.



  rem  *****************************************************
  rem  *
  rem  *  Player 0 sprite position variables.
  rem  *
  rem  *****************************************************
  x=80 : y =55



  rem  *****************************************************
  rem  *
  rem  *  Background color.
  rem  *
  rem  *****************************************************
  COLUBK = 0
  




  rem  ******************************************************
  rem  ******************************************************
  rem  *
  rem  *  Main game loop starts here.
  rem  *
  rem  ******************************************************
  rem  ******************************************************

MainLoop

  rem  *****************************************************
  rem  *
  rem  *  Player 0 sprite color.
  rem  *
  rem  *****************************************************
  COLUP0 = 202



  rem  *****************************************************
  rem  *
  rem  *  Increase Player 0 animation counter.
  rem  *
  rem  *****************************************************
  P0_Anim_Count = P0_Anim_Count + 1



  rem  *****************************************************
  rem  *
  rem  *  Player 0 sprite animation if-thens.
  rem  *
  rem  *****************************************************
  if P0_Anim_Count = 10 then player0:
  %11100111
  %11100111
  %11100111
  %11100000
  %11100000
  %11100000
  %11111111
  %11111111
end

  if P0_Anim_Count = 20 then   player0:
  %11100111
  %11100111
  %11100111
  %11100111
  %11100000
  %11100000
  %11111111
  %11111111
end

  if P0_Anim_Count = 30 then  player0:
  %11100111
  %11100111
  %11100111
  %11100111
  %11100111
  %11100000
  %11111111
  %11111111
end

  rem  *****************************************************
  rem  *  Last animation frame for Player 0. Animation counter is reset.
  rem  *****************************************************
  if P0_Anim_Count = 40 then P0_Anim_Count = 0 :  player0:
  %11100111
  %11100111
  %11100111
  %11100111
  %11100000
  %11100000
  %11111111
  %11111111
end



  rem  *****************************************************
  rem  *
  rem  *  Player 0 sprite position.
  rem  *
  rem  *****************************************************
  player0x=x : player0y=y



  rem  *****************************************************
  rem  *
  rem  *  Joystick movement of Player 0 sprite and position limitation.
  rem  *
  rem  *****************************************************
  if joy0right && x < 152 then x=x+1 :  P0_Flip = 0
  if joy0left && x > 2 then x=x-1 :  P0_Flip = 8
  if joy0up && y > 9 then y=y-1
  if joy0down && y < 88 then y=y+1



  rem  *****************************************************
  rem  *
  rem  *  Tells which way Player 0 sprite should be facing (left or right).
  rem  *
  rem  *****************************************************
  REFP0=P0_Flip



  rem  *****************************************************
  rem  *
  rem  *  Updates the screen.
  rem  *
  rem  *****************************************************
  drawscreen


  goto MainLoop

 

sprite_animation_if_then_2010y_03m_03d_2033t.bas

 

sprite_animation_if_then_2010y_03m_03d_2033t.bin

Edited by Random Terrain

Share this post


Link to post
Share on other sites

Thanks, I've also found something interesting regarding position limitation.

Share this post


Link to post
Share on other sites

I've always found it helpful to just reserve one variable for a universal "animation frame" and a one variable for a "framerate."

 

Main_Loop
  framerate = framerate + 1
  if framerate = 4 then animFrame = animFrame + 1 : framerate = 0
  if animFrame = 8 then animFrame = 0
  gosub PlayerSprite 
  drawscreen
  goto Main_Loop

 

So in the above, you have an 8-frame animation schedule that updates every fourth loop. To make game animation slower or faster, just alter "4" to the desired speed (higher numbers slow animation down, lower numbers speed it up)

 

That way, rather than having to do something like "if P0_Anim_Count = 10 then player0:" you can simply do a "on" statement for every sprite you want to animate, i.e.:

 

PlayerSprite
 (position updates, AI, player color, etc.)
 on animFrame goto P0_Frame1 P0_Frame2 P0_Frame3 P0_Frame4 P0_Frame5 P0_Frame6 P0_Frame7 P0_Frame8
 return

P0_Frame1
 player0:
 %11100111
 %11100111
 %11100111
 %11100000
 %11100000
 %11100000
 %11111111
 %11111111
end
return

(...)

 

I normally contain each "on" statement at the end of whatever general subroutine I've come up with for each sprite/entity. So if it's, for instance, an enemy hunting you down, I begin the subroutine by updating it's position, then include the A.I. nodes, then end the subroutine with the "on" statements and their pointers to the sprite definitions.

 

The important thing to remember about using this technique is to include a return after each sprite definition. After the routine reads the definition, it should return to the subroutine, which then returns to the main loop. You can do this for as many sprites (or other objects) as you want, including flickered sprites.

 

Cheers,

Jarod.

Edited by jrok

Share this post


Link to post
Share on other sites

Thank you very much Jarod!

 

No problem. You can also include the "on" statements outside of subroutines. Such as:

 

PlayerSprite
 (position updates, AI, player color, etc.)
 goto PlayerSpriteAnimation
 return

PlayerSpriteAnimation
 on animFrame goto P0_Frame1 P0_Frame2 P0_Frame3 P0_Frame4 P0_Frame5 P0_Frame6 P0_Frame7 P0_Frame8
 return

P0_Frame1
 player0:
 %11100111
 %11100111
 %11100111
 %11100000
 %11100000
 %11100000
 %11111111
 %11111111
end
return

(...)

 

I use this technique a lot in bank-switched games with lots of different animations, keeping my animations all in one place. For instance, if your animated sprite has multiple states (running, jumping, shooting, dying, etc), you can just jump to the appropriate "on" statement Label when the criteria is met in your subroutine. In any case, the useful of "on" is that you can maintain a constant animation schedule for all the elements in your game without having to keep track of a lot of different stuff.

 

Cheers,

Jarod.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...