program HowToMoveSpriteUsingAnimation; uses SwinGame,sgTypes; procedure DoWalking(sprt : Sprite ;key : KeyCode ;animationName : String ;dx : Single ;dy : Single ); begin if KeyDown(key ) then begin SpriteStartAnimation(sprt ,animationName ); SpriteSetDX(sprt ,dx ); SpriteSetDY(sprt ,dy ); end; end;procedure Main(); var myFrog : Sprite; begin OpenAudio(); OpenGraphicsWindow('Moving Sprite Using Animation' ,800 ,600 ); LoadResourceBundle('dance_bundle.txt' ); myFrog := CreateSprite(BitmapNamed('FrogBmp' ) ,AnimationScriptNamed('WalkingScript' ) ); SpriteStartAnimation(myFrog ,'StandFront' ); SpriteSetX(myFrog ,382 ); SpriteSetY(myFrog ,274 ); repeat ClearScreen(ColorWhite ); DrawSprite(myFrog ); RefreshScreen(60 ); UpdateSprite(myFrog ); ProcessEvents(); if SpriteAnimationHasEnded(myFrog ) then begin SpriteSetDX(myFrog ,0 ); SpriteSetDY(myFrog ,0 ); DoWalking(myFrog ,vk_UP ,'WalkBack' ,0 ,-0.25 ); DoWalking(myFrog ,vk_DOWN ,'WalkFront' ,0 ,+0.25 ); DoWalking(myFrog ,vk_LEFT ,'WalkLeft' ,-0.25 ,0 ); DoWalking(myFrog ,vk_RIGHT ,'WalkRight' ,+0.25 ,0 ); DoWalking(myFrog ,vk_w ,'MoonWalkBack' ,0 ,-0.25 ); DoWalking(myFrog ,vk_s ,'MoonWalkFront' ,0 ,+0.25 ); DoWalking(myFrog ,vk_a ,'MoonWalkLeft' ,-0.25 ,0 ); DoWalking(myFrog ,vk_d ,'MoonWalkRight' ,+0.25 ,0 ); end; until WindowCloseRequested(); FreeSprite(myFrog ); CloseAudio(); ReleaseAllResources(); end; begin Main(); end.
How To Move Sprite Using Animation
This article will show you how to move a sprite using animation.
It is important in many occasions to move a character from one place to another place. In those cases a character should use animation so the user has the feeling that a character is moving and not dragging itself. At the same time the character should change its location depending on the user, or the key they are using to move the character. For this instance it lets you move left, right, up and down directions. Moreover if the user holds down the "A" or the "D" key, the character will do the "moon dance". To do all these things only one image with 16 bitmap cells, one bundle script and one animation script is used.
The core idea of this program is in the game loop, after processing all the events (Process Events), check if the previous animation, that was running, has ended (Sprite Animation Has Ended) or not. If it has not been ended then the game loop will continue and update (Update Sprite) and draw (Draw Sprite) the previous animation.
If the previous animation has ended then at first it will set the dx any dy (Sprite Set Dx, Sprite Set Dy) of the sprite as 0 so the sprite will not move anymore. depending on the user input, a procedure (Do Walking) will be called. This procedure will get the sprite, take which key was pressed, which animation it needs to play and the new dx and dy of the sprite so it knows at which direction and at which speed it needs to move the sprite.
The code below shows a small program that demonstrates all these steps in making a sprite that will move with the animation.
Note that in Windows you must show a window before audio will play.
Suggested projects will appear here.