Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon

Godot 3 Tutorial–2D Graphics, Sprites And Animation

 

This video covers 2D graphics in Godot 3.  We start by looking at the 2D editor tools then move on to create a Sprite, including covering the process of importing textures into the Godot engine.  We then look at scripting Sprite objects, covering tasks like pivot points, translating, rotating and scaling.  We then move on to AtlasTextue, a single texture with multiple images.  We then create a script illustrating how to use a sprite sheet, including creating a timer to flip between animation frames.  Finally we look at using the AnimatedSprite node, including code to control switching animations.

 

The Video

Assets and Code Samples

BasicSprite.gd

extends Sprite


func _ready():
  #draw in the middle of the screen
  position = Vector2(get_viewport().size.x/2,get_viewport().size.y/2)
  
  #scale to 30%
  scale = Vector2(0.3,0.3)
  
  #rotate by 90degrees
  rotate(deg2rad(90))
  
  set_process(false)
  
func _process(delta):
  # each frame rotate by 90deg/sec
  rotation = self.rotation + deg2rad(90.0 * delta)
  
  # move left by 100 pixels / sec
  # once you go off left side of screen, jump to the right
  translate(Vector2(-100*delta,0))
  if(position.x < 0):
    position = Vector2(get_viewport().size.x, get_viewport().size.y/2)

 

SpriteAtlas.gd

extends Sprite

var timer

func _ready():
  timer = Timer.new()
  timer.connect("timeout",self,"tick")
  add_child(timer)
  timer.wait_time = 0.2
  timer.start()
  

func tick():
    if self.frame < 9:
      self.frame = self.frame + 1
    else:
      self.frame = 0

 

AnimatedSprite.gd

extends AnimatedSprite

func _ready():
  connect("animation_finished",self, "_on_AnimatedSprite_animation_finished")

func _on_AnimatedSprite_animation_finished():
  if self.animation == "run":
    animation = "jumpattack"
  else:
    animation = "run"

 

The assets used in this example can be downloaded here.

 

Back to Tutorial Series Homepage





blog comments powered by Disqus


See More Tutorials on DevGa.me!

Month List