Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon


3. June 2015

 

In this tutorial we are going to look at Sprite Animation in Godot Engine, specifically on using the AnimatedSprite class.  We are going to import and create a node that has multiple frames of animation, then look at some code to flip between frames.  In the immediately following tutorial, we will then cover a much better animation method using AnimationPlayer.

 

As always, there is an HD Video version of this tutorial available right here or embedded below.

 

Alright, let’s just right in with AnimatedSprite.

 

Sprite Animation

AnimatedSprite is a handy Node2D derived class that enables you to have a node with multiple SpriteFrames.  In plain English, this class enables us to have a sprite with multiple frames of animation. 

 

Speaking of frames of animation, this is the sequence of png images I am going to use for this example:

image

 

You can download the zip file containing these images here, or of course you can use whatever images you want.

 

Now we simply want to import them to our using the standard Import->2D Texture method.  Be aware, you can multi select in the Importer, so you can import the entire sequence in one go.  Assuming you’ve done it right, your FileSystem should look somewhat like:

image

 

Now add an AnimatedSprite node to your scene like so:

image

 

Now we add the frames to our AnimatedSprite by selecting Frame->New SpriteFrames

image

 

Now drop it down again and select Edit:

image

 

The 2D editor will now be replaced with the SpriteFrames editor.  Click the open icon:

image

 

Shift select all of the sprite frames and select OK

image

 

All of your sprites should now appear in the editor:

image

 

Now let’s add some code to flip through the frames of our AnimatedSprite.  Attach a script to the AnimatedSprite node, then use the following code:


extends AnimatedSprite

var tempElapsed = 0

func _ready():
   set_process(true)
   
func _process(delta):
   tempElapsed = tempElapsed + delta
   
   if(tempElapsed > 0.1):
      if(get_frame() == self.get_sprite_frames().get_frame_count()-1):
         set_frame(0)
      else:
         self.set_frame(get_frame() + 1)
      
      tempElapsed = 0
   
   print(str(get_frame() + 1))

The logic is pretty simple.  In our process tick we increment a variable tempElapsed, until 1/10th of a second has elapsed, at which point we move on to the next frame.  If we are at the last frame of our available animation, we then go back to the very first frame.

 

When you run it, you should see:

walking

 

Pretty cool!  However, instead of advancing the frame using code there is a much better approach to animation, that we will see in the next tutorial.  Stay tuned.

 

The Video

Programming , , ,

blog comments powered by Disqus

Month List

Popular Comments

Unity Release Patch 5.4.2p3
Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon


3. November 2016

 

Another day, another Unity game engine patch, this time patch 5.4.2p3.  As is typical of Unity patches, it contains no new functionality, just the regular collection of fixes and improvements.  From the release notes:

 

Improvements
  • iOS: Exposed EditorUserBuildSettings.iOSBuildConfigType in public API.
  • User profile markers in scripts now show in platform profilers even when the Unity profiler is not running.
Fixes
  • (832837) - Animation: Fixed a crash when receiving null property modification in animation recording.
  • (835544) - Animation: Re-enabling playback/recording in play mode.
  • (844785, 841067) - AssetBundles: Fixed a deadlock when decompressing lzma bundle when stream had incompressible data.
  • (774264, 820297) - CacheServer: Fixed creation of directories and an issue while sending file and socket was closed.
  • (819150) - Collab: Fixed a crash with collab toolbar.
  • (826244) - Graphics: Texture2D.ReadPixels no longer reads from the wrong location on iOS/Metal when reading a section of an image.
  • (842538) - IL2CPP: Correctly marshal an out array of structures which uses the LPStruct marshaling directive.
  • (840534) - Mono: Prevent GetHostEntry from throwing a SocketException on Windows when the machine is not connected to any network.
  • (827122, 804744) - Multiplayer: Fixed a crash with StartAsClient.
  • (790431, 785347, 771860, 836170, 827884) - Multiplayer: Fixed the issues concerning disconnect and error handling.
  • (761588, 820982, 773323) - Multiplayer: Fixed weaver error handling of not supported types.
  • (756572, 737241, 775248) - Multiplayer: Fixed weaver generation of Serialize and Deserialize functions.
  • (765145, 840314, 710625, 730779) - Shaders: Fixed some cases where surface shader writing to o.Occlusion would not sample the texture with correct UVs.
  • (833647) - UnityWebRequest: Fixed redirect to a relative URL.
  • (none) - VR: Fixed issue with Oculus causing Controller Input to fail for bumpers.
  • (838737) - Windows Store: Fixed incorrect instanceID for component when cloning object with component contained in a struct inside list.

 

The patch is available for download here.

GameDev News

blog comments powered by Disqus

Month List

Popular Comments