How To Move Sprite Using Animation

This article will show you how to move a sprite using animation.

Level

Advanced
All About Animation | How To Do Multiple Animation From One Script

Details

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.

Source Code Widget

  • 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.
    Download Now"
  • #include 
    #include "SwinGame.h"
    void do_walking(sprite  sprt,key_code  key,const char * animation_name,float  dx,float  dy) 
    {
    
        if ( key_down(key )  )
        {
            sprite_start_animation(sprt ,animation_name );
            sprite_set_dx(sprt ,dx );
            sprite_set_dy(sprt ,dy );
        }
    }int main() 
    {
        sprite  my_frog;
        open_audio();
        open_graphics_window("Moving Sprite Using Animation" ,800 ,600 );
        load_resource_bundle("dance_bundle.txt" );
        my_frog = create_sprite(bitmap_named("FrogBmp" ) ,animation_script_named("WalkingScript" ) ) ;
        sprite_start_animation(my_frog ,"StandFront" );
        sprite_set_x(my_frog ,382 );
        sprite_set_y(my_frog ,274 );
        do 
        {
            clear_screen(color_white );
            draw_sprite(my_frog );
            refresh_screen(60 );
            update_sprite(my_frog );
            process_events();
            if ( sprite_animation_has_ended(my_frog )  )
            {
                sprite_set_dx(my_frog ,0 );
                sprite_set_dy(my_frog ,0 );
                do_walking(my_frog ,VK_UP ,"WalkBack" ,0 ,-0.25 );
                do_walking(my_frog ,VK_DOWN ,"WalkFront" ,0 ,+0.25 );
                do_walking(my_frog ,VK_LEFT ,"WalkLeft" ,-0.25 ,0 );
                do_walking(my_frog ,VK_RIGHT ,"WalkRight" ,+0.25 ,0 );
                do_walking(my_frog ,VK_W ,"MoonWalkBack" ,0 ,-0.25 );
                do_walking(my_frog ,VK_S ,"MoonWalkFront" ,0 ,+0.25 );
                do_walking(my_frog ,VK_A ,"MoonWalkLeft" ,-0.25 ,0 );
                do_walking(my_frog ,VK_D ,"MoonWalkRight" ,+0.25 ,0 );
            }
        }
        while ( ! (window_close_requested() ) );
        free_sprite(my_frog );
        close_audio();
        release_all_resources();
        return 0;
    }
    
    Download Now"
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon

Note that in Windows you must show a window before audio will play.

Possibilities

Suggested projects will appear here.