Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Area2D
- var velocity = Vector2.ZERO
- var speed = 300
- var sizeGame = Vector2.ZERO
- var margin = 20
- # Called when the node enters the scene tree for the first time.
- func _ready():
- OS.center_window()
- sizeGame = OS.window_size
- position = Vector2(sizeGame.x/2, sizeGame.y/2)
- # Called every frame. 'delta' is the elapsed time since the previous frame.
- func _process(delta):
- movimiento(delta)
- limites()
- animacion()
- func movimiento(delta) -> void:
- velocity = Vector2.ZERO
- velocity.x = Input.get_action_raw_strength("ui_right") - Input.get_action_raw_strength("ui_left")
- velocity.y = Input.get_action_raw_strength("ui_down") - Input.get_action_raw_strength("ui_up")
- velocity = velocity.normalized() * speed * delta if velocity.length() > 0 else Vector2.ZERO
- position += velocity
- func limites() -> void:
- position.x = clamp(position.x, 0 + margin, sizeGame.x - margin)
- position.y = clamp(position.y, 0 + margin, sizeGame.y - margin)
- func animacion() -> void:
- if velocity.x != 0:
- # get_node("AnimatedSprite") es igual a $AnimatedSprite
- get_node("AnimatedSprite").play("run")
- $AnimatedSprite.flip_h = true if velocity.x < 0 else false
- elif velocity.y != 0:
- get_node("AnimatedSprite").play("run")
- else:
- get_node("AnimatedSprite").play("idle")
Advertisement
Add Comment
Please, Sign In to add comment