1. extends Node2D
  2. onready var dd= $"Rect739"
  3. var vel = Vector2.RIGHT.rotated(deg2rad(-45)) * 10
  4. var g = 9.8
  5. var start_pos
  6. func _ready():
  7. start_pos = dd.position
  8. set_process(false)
  9. func _process(delta):
  10. vel.y += g * delta
  11. dd.position += vel
  12. func _on_Button_pressed():
  13. dd.position = start_pos
  14. vel = Vector2.RIGHT.rotated(deg2rad(-45)) * 10
  15. set_process(true)
  16. pass

screenshots.gif

  1. extends Node2D
  2. var speed = 10.0 # 移动速度
  3. var vel = Vector2.ZERO # 速度向量
  4. var g = 9.8 # 重力加速度
  5. var start_pos # 子弹的初始位置,用于复位
  6. onready var dd= $"dd" # 子弹
  7. func _ready():
  8. # 水平向右逆时针旋转45度,也就是斜向上45度
  9. vel = Vector2.RIGHT.rotated(deg2rad(-45)) * speed
  10. start_pos = dd.position # 记录子弹初始位置,用于复位
  11. dd.rotation_degrees = rad2deg(vel.angle()) # 设置初始朝向
  12. set_process(false) # 禁用帧处理
  13. func _process(delta):
  14. vel.y += g * delta # 每帧加上重力影响
  15. dd.rotation_degrees = rad2deg(vel.angle()) # 更新子弹朝向
  16. dd.position += vel # 移动
  17. # 按下开始按钮
  18. func _on_Button_pressed():
  19. vel = Vector2.RIGHT.rotated(deg2rad(-45)) * speed
  20. dd.position = start_pos # 子弹复位
  21. dd.rotation_degrees = rad2deg(vel.angle()) # 设置初始朝向
  22. set_process(true)
  23. pass

screenshots.gif

继续改进

image.pngimage.pngimage.png

  1. extends Node2D
  2. var speed = 50.0 # 移动速度
  3. var vel = Vector2.ZERO # 速度向量
  4. var g = 9.8 # 重力加速度
  5. var is_dd_flying = false # 子弹在飞:)
  6. onready var dd= $"dd" # 子弹
  7. onready var pos2d= $"base/body/Position2D"
  8. onready var pao_body= $"base/body"
  9. func _unhandled_input(event):
  10. if event is InputEventMouseButton:
  11. if event.button_index == BUTTON_LEFT:
  12. if event.is_pressed():
  13. if not is_dd_flying:
  14. is_dd_flying = true
  15. func _process(delta):
  16. if is_dd_flying:
  17. vel = Vector2.RIGHT.rotated(deg2rad(dd.rotation_degrees))
  18. vel.y += g * delta # 每帧加上重力影响
  19. dd.rotation_degrees = rad2deg(vel.angle()) # 更新子弹朝向
  20. dd.global_position += vel * speed# 移动
  21. else:
  22. pao_body.look_at(get_global_mouse_position())
  23. dd.global_position= pos2d.global_position # 子弹跟随炮筒的尖端位置
  24. dd.rotation_degrees = pos2d.global_rotation_degrees # 朝向与炮筒一致
  25. func _on_VisibilityNotifier2D_screen_exited():
  26. is_dd_flying = false
  27. pass

222299.gif