Procházet zdrojové kódy

Add movement control to sprite

DricomDragon před 5 roky
rodič
revize
575f5cc7b6
2 změnil soubory, kde provedl 32 přidání a 7 odebrání
  1. 23 0
      Player.gd
  2. 9 7
      Player.tscn

+ 23 - 0
Player.gd

@@ -0,0 +1,23 @@
+extends Area2D
+
+export var speed = 400 # pixel / sec
+var screen_size
+
+func _ready():
+	screen_size = get_viewport_rect().size
+
+func _process(delta):
+    var velocity = Vector2()
+    if Input.is_action_pressed("ui_right"):
+        velocity.x += 1
+    if Input.is_action_pressed("ui_left"):
+        velocity.x -= 1
+    if Input.is_action_pressed("ui_down"):
+        velocity.y += 1
+    if Input.is_action_pressed("ui_up"):
+        velocity.y -= 1
+    if velocity.length() > 0:
+        velocity = velocity.normalized() * speed
+        $AnimatedSprite.play()
+    else:
+        $AnimatedSprite.stop()

+ 9 - 7
Player.tscn

@@ -1,18 +1,19 @@
-[gd_scene load_steps=7 format=2]
+[gd_scene load_steps=8 format=2]
 
-[ext_resource path="res://dodge_assets/art/playerGrey_walk1.png" type="Texture" id=1]
-[ext_resource path="res://dodge_assets/art/playerGrey_walk2.png" type="Texture" id=2]
-[ext_resource path="res://dodge_assets/art/playerGrey_up1.png" type="Texture" id=3]
-[ext_resource path="res://dodge_assets/art/playerGrey_up2.png" type="Texture" id=4]
+[ext_resource path="res://Player.gd" type="Script" id=1]
+[ext_resource path="res://dodge_assets/art/playerGrey_walk1.png" type="Texture" id=2]
+[ext_resource path="res://dodge_assets/art/playerGrey_walk2.png" type="Texture" id=3]
+[ext_resource path="res://dodge_assets/art/playerGrey_up1.png" type="Texture" id=4]
+[ext_resource path="res://dodge_assets/art/playerGrey_up2.png" type="Texture" id=5]
 
 [sub_resource type="SpriteFrames" id=1]
 animations = [ {
-"frames": [ ExtResource( 1 ), ExtResource( 2 ) ],
+"frames": [ ExtResource( 2 ), ExtResource( 3 ) ],
 "loop": true,
 "name": "right",
 "speed": 5.0
 }, {
-"frames": [ ExtResource( 3 ), ExtResource( 4 ) ],
+"frames": [ ExtResource( 4 ), ExtResource( 5 ) ],
 "loop": true,
 "name": "up",
 "speed": 5.0
@@ -23,6 +24,7 @@ radius = 26.1109
 height = 14.0326
 
 [node name="Player" type="Area2D"]
+script = ExtResource( 1 )
 __meta__ = {
 "_edit_group_": true
 }