Tuesday, March 19, 2019

4. Sprite

We attach a script to Node, where we create a Sprite node (using Sprite.new()).


The texture for the sprite is selected as the Godot icon.


The screen size, was set as 200 by 200 in project settings. We set the position as the center (100, 100). Note position is a member variable of Node2D and not Sprite class.


We set the rotation as PI/4 radians (or 45 degrees). We print out the angle using Godot's rad2deg (radian to degree) function.


We finally have to attach the Sprite node to the Node. We may use self or not, to refer to the Node, thus we could also have used add_child(), rather than self.add_child().



extends Node

var sprite = Sprite.new()

func _ready():
    sprite.texture = load("res://icon.png")
    sprite.position = Vector2(100, 100)
    sprite.rotation = PI / 4
    print("I am ", self)
    self.add_child(sprite)
    print("rotation = ", rad2deg(sprite.rotation))

The output shows the rotated image:


No comments:

Post a Comment