How To Create Multiple Animations From One Script

This article will show you how to create multiple animations from one script.

Level

Advanced
All About Animation

Details

In your animation script you need to mention different frames for different animations with an identifier for those animations. Please read about animation to know how you can create a script with multiple animation frames. After you create the identifier for different animations you can follow the steps below:

  1. In your game loop prompt the users which key they have to press to get the animation they want.
  2. Draw Sprite, Refresh Screen and update the sprite (Update Sprite).
  3. Next process all the events (Process Events) to get what the user input is.
  4. Now depending on the user's command-
  • Start animating (Sprite Start Animation) your previously created sprite. Here you also have to mention the name of the animation that you want to play by calling the identifier of that animation. For the code below it will start the animation called explosion if the user press the key "E".
  • It will start the animation called implosion if the user press the key "I"
  • It will start the animation called explosion_loop if the user press the key "L"

The code below shows you step by step process to get multiple animations from the same bitmap file and one animation script.

Source Code Widget

  • program HowToCreateMultipleAnimationsFromOneScript;
    uses SwinGame,sgTypes;
    
    procedure Main();
    var
        explosion : Sprite;
    begin
        OpenAudio();
        OpenGraphicsWindow('Create Multiple Animations' ,200 ,200 );
        LoadResourceBundle('explosion_bundle.txt' );
        explosion := CreateSprite(BitmapNamed('explosionBmp' )  ,AnimationScriptNamed('explosionScrpt' )  );
        SpriteSetX(explosion ,64 );
        SpriteSetY(explosion ,64 );
        repeat
            ClearScreen(ColorWhite );
            DrawText('[E]xplosion' ,ColorBlack ,0 ,0 );
            DrawText('[I]mplosion' ,ColorBlack ,0 ,10 );
            DrawText('[L]oop' ,ColorBlack ,0 ,20 );
            DrawSprite(explosion );
            RefreshScreen(60 );
            UpdateSprite(explosion );
            ProcessEvents();
            if  KeyTyped(vk_e )    then
                SpriteStartAnimation(explosion ,'explosion' );
            if  KeyTyped(vk_i )    then
                SpriteStartAnimation(explosion ,'implosion' );
            if  KeyTyped(vk_l )    then
                SpriteStartAnimation(explosion ,'explosion_loop' );
        
        until WindowCloseRequested();
        FreeSprite(explosion );
        CloseAudio();
        ReleaseAllResources();
    end;
    begin
        Main();
    end.
    Download Now"
  • #include 
    #include "SwinGame.h"
    int main() 
    {
        sprite  explosion;
        open_audio();
        open_graphics_window("Create Multiple Animations" ,200 ,200 );
        load_resource_bundle("explosion_bundle.txt" );
        explosion = create_sprite(bitmap_named("explosionBmp" ) ,animation_script_named("explosionScrpt" ) ) ;
        sprite_set_x(explosion ,64 );
        sprite_set_y(explosion ,64 );
        do 
        {
            clear_screen(color_white );
            draw_text("[E]xplosion" ,color_black ,0 ,0 );
            draw_text("[I]mplosion" ,color_black ,0 ,10 );
            draw_text("[L]oop" ,color_black ,0 ,20 );
            draw_sprite(explosion );
            refresh_screen(60 );
            update_sprite(explosion );
            process_events();
            if ( key_typed(VK_E )  )
                sprite_start_animation(explosion ,"explosion" );
            if ( key_typed(VK_I )  )
                sprite_start_animation(explosion ,"implosion" );
            if ( key_typed(VK_L )  )
                sprite_start_animation(explosion ,"explosion_loop" );
        }
        while ( ! (window_close_requested() ) );
        free_sprite(explosion );
        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.