godot
var motion = Vector2()
Vector 表示一个坐标, 有 x 和 y 两个方向坐标
- func _physics_process(delta):
- if Input.is_action_pressed("ui_right"):
- motion.x = 100
- if Input.is_action_pressed("ui_left"):
- motion.x = -100
- move_and_slide(motion)
- pass
通过判断键盘输入 Input.is_action_pressed("ui_right"): 来控制 player 运动方向和距离 motion.x = 100
但是只是上面代码并不能让我们的 player 根据输入运动起来, 我们还需要 move_and_slide 方法来实现. 从文档上来看这个方法是与 delta 相关. 方法传入表示线性速度的值.
- extends KinematicBody2D
- var motion = Vector2()
- func _physics_process(delta):
- motion.y += 10
- if Input.is_action_pressed("ui_right"):
- motion.x = 100
- elif Input.is_action_pressed("ui_left"):
- motion.x = -100
- else:
- motion.x = 0
- move_and_slide(motion)
- pass
然后我们可通过 motion.y += 10 给 player 添加重力效果. 这样 player 就会向下运动超出屏幕范围, 为防止 player 跌落出屏幕我们需要添加 staticBody2D 作为地面.
添加 staticBody2D
我们可以为 Wall 添加 sprite 来然后添加图片, 并调整位置在 player 下方为了就是接住 player.
为 staticBody2D 添加 sprite
接下来为 player 和 wall 分别添加 CollisionShape2D 然后添加作用范围, 这样再次启动游戏后, player 就不会在坠落出屏幕, 而是落在我们设置的地面上.
为 player 添加 CollisionShape2D 节点
通过选中 wall 后 ctrl + D 来复制更多 wall, 连接后形成地面.
布置地面
play game
来源: http://www.jianshu.com/p/39ec9ab41cd8