Add Miscari caracter
This commit is contained in:
commit
1d72cd7b53
40
Miscari caracter
Normal file
40
Miscari caracter
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
Ziua 1:
|
||||||
|
Realizarea miscarii nodului: // circular
|
||||||
|
|
||||||
|
var speed = 400 # pixel per second
|
||||||
|
var angular_speed = PI # radian per second
|
||||||
|
|
||||||
|
func _process(delta):
|
||||||
|
rotation += angular_speed * delta
|
||||||
|
var velocity = Vector2.UP.rotated(rotation) * speed
|
||||||
|
position += velocity * delta
|
||||||
|
As we already saw, the var keyword defines a new variable. If you put it at the top of the script, it defines a property of the class. Inside a function, it defines a local variable: it only exists within the function’s scope.
|
||||||
|
|
||||||
|
We define a local variable named velocity, a 2D vector representing both a direction and a speed. To make the node move forward, we start from the Vector2 class’s constant Vector2.UP, a vector pointing up, and rotate it by calling the Vector2 method rotated(). This expression, Vector2.UP.rotated(rotation), is a vector pointing forward relative to our icon. Multiplied by our speed property, it gives us a velocity we can use to move the node forward.
|
||||||
|
|
||||||
|
We add velocity * delta to the node’s position to move it. The position itself is of type Vector2, a built-in type in Godot representing a 2D vector.
|
||||||
|
|
||||||
|
|
||||||
|
Ziua 2:
|
||||||
|
Cum procesam datele oferite de la tastatura // continuare joc zi anterioara:
|
||||||
|
var speed = 400
|
||||||
|
var angular_speed = PI
|
||||||
|
|
||||||
|
func _process(delta):
|
||||||
|
var direction = 0
|
||||||
|
if Input.is_action_pressed("ui_left"):
|
||||||
|
direction = -1
|
||||||
|
if Input.is_action_pressed("ui_right"):
|
||||||
|
direction = 1
|
||||||
|
|
||||||
|
rotation += angular_speed * direction * delta
|
||||||
|
|
||||||
|
var velocity = Vector2.ZERO
|
||||||
|
if Input.is_action_pressed("ui_up"):
|
||||||
|
velocity = Vector2.UP.rotated(rotation) * speed
|
||||||
|
|
||||||
|
if Input.is_action_pressed("ui_down"):
|
||||||
|
velocity = Vector2.DOWN.rotated(rotation) * speed
|
||||||
|
|
||||||
|
position += velocity * delta
|
||||||
|
|
Loading…
Reference in New Issue
Block a user