extends Node2D
onready var dd= $"Rect739"
var vel = Vector2.RIGHT.rotated(deg2rad(-45)) * 10
var g = 9.8
var start_pos
func _ready():
start_pos = dd.position
set_process(false)
func _process(delta):
vel.y += g * delta
dd.position += vel
func _on_Button_pressed():
dd.position = start_pos
vel = Vector2.RIGHT.rotated(deg2rad(-45)) * 10
set_process(true)
pass
extends Node2D
var speed = 10.0 # 移动速度
var vel = Vector2.ZERO # 速度向量
var g = 9.8 # 重力加速度
var start_pos # 子弹的初始位置,用于复位
onready var dd= $"dd" # 子弹
func _ready():
# 水平向右逆时针旋转45度,也就是斜向上45度
vel = Vector2.RIGHT.rotated(deg2rad(-45)) * speed
start_pos = dd.position # 记录子弹初始位置,用于复位
dd.rotation_degrees = rad2deg(vel.angle()) # 设置初始朝向
set_process(false) # 禁用帧处理
func _process(delta):
vel.y += g * delta # 每帧加上重力影响
dd.rotation_degrees = rad2deg(vel.angle()) # 更新子弹朝向
dd.position += vel # 移动
# 按下开始按钮
func _on_Button_pressed():
vel = Vector2.RIGHT.rotated(deg2rad(-45)) * speed
dd.position = start_pos # 子弹复位
dd.rotation_degrees = rad2deg(vel.angle()) # 设置初始朝向
set_process(true)
pass
继续改进
extends Node2D
var speed = 50.0 # 移动速度
var vel = Vector2.ZERO # 速度向量
var g = 9.8 # 重力加速度
var is_dd_flying = false # 子弹在飞:)
onready var dd= $"dd" # 子弹
onready var pos2d= $"base/body/Position2D"
onready var pao_body= $"base/body"
func _unhandled_input(event):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT:
if event.is_pressed():
if not is_dd_flying:
is_dd_flying = true
func _process(delta):
if is_dd_flying:
vel = Vector2.RIGHT.rotated(deg2rad(dd.rotation_degrees))
vel.y += g * delta # 每帧加上重力影响
dd.rotation_degrees = rad2deg(vel.angle()) # 更新子弹朝向
dd.global_position += vel * speed# 移动
else:
pao_body.look_at(get_global_mouse_position())
dd.global_position= pos2d.global_position # 子弹跟随炮筒的尖端位置
dd.rotation_degrees = pos2d.global_rotation_degrees # 朝向与炮筒一致
func _on_VisibilityNotifier2D_screen_exited():
is_dd_flying = false
pass