GDscript:动画完成后发射子弹
GDscript: Shoot bullet after Animation Complete
我正在创建一个简单的 2d 平台游戏,
我的问题是,在我的一个动画中,bullet/Bone 需要在动画完成后创建。现在它创建骨骼并同时启动动画。
我试过将 Bone 投射物的创建放在 onanimation_complete 下,但如果我的角色面向左侧(按键给出的方向 "a"),它只会让我射击.有什么我想念的吗?
这是代码,如果有帮助的话。
谢谢!
extends KinematicBody2D
const WALK = 40
const SPRINT = 70
const GRAVITY = 10
const JUMP = -250
const FLOOR = Vector2(0, -1)
const BONE = preload("res://Bone.tscn")
var is_attacking = false
var velocity = Vector2()
var on_ground = true
func _physics_process(delta):
if Input.is_action_pressed("D"):
if is_attacking == false:
velocity.x = WALK
$Player_Animation.play("Player_Run")
$Player_Animation.flip_h = true
if sign($Position2D.position.x) == -1:
$Position2D.position.x *= -1
elif Input.is_action_pressed("A"):
if is_attacking == false:
velocity.x = -WALK
$Player_Animation.play("Player_Run")
$Player_Animation.flip_h = false
if sign($Position2D.position.x) == 1:
$Position2D.position.x *= -1
else:
velocity.x = 0
if on_ground == true && is_attacking == false:
$Player_Animation.play("Player_Idle")
if Input.is_action_just_pressed("Space"):
if is_attacking == false:
if on_ground == true:
velocity.y = JUMP
on_ground = false
if Input.is_action_just_pressed( "ui_focus_next") && is_attacking == false:
is_attacking = true
$Player_Animation.play("Player_Attack")
var bone = BONE.instance()
if sign($Position2D.position.x) == 1:
bone.set_bone_direction(-1)
else:
bone.set_bone_direction(1)
get_parent().add_child(bone)
bone.position = $Position2D.global_position
velocity.y = velocity.y + GRAVITY
if is_on_floor():
on_ground = true
else:
if is_attacking == false:
on_ground = false
if velocity.y < 0:
$Player_Animation.play("Player_Jump")
else:
$Player_Animation.play("Player_Fall")
velocity = move_and_slide(velocity, FLOOR)
func _on_Sprite_animation_finished():
is_attacking = false
可能最简单的解决方案是以计时器节点的形式添加子弹延迟。它基本上是这样工作的:同时播放动画和启动计时器。如果在播放动画时计时器达到 0,则触发射弹。您可以将计时器设置为从动画开始到您希望射弹出现的确切时间。我在其他动画中使用了这种技术并取得了巨大成功。事实上,可以在动画期间的不同点触发许多效果。
我正在创建一个简单的 2d 平台游戏, 我的问题是,在我的一个动画中,bullet/Bone 需要在动画完成后创建。现在它创建骨骼并同时启动动画。
我试过将 Bone 投射物的创建放在 onanimation_complete 下,但如果我的角色面向左侧(按键给出的方向 "a"),它只会让我射击.有什么我想念的吗?
这是代码,如果有帮助的话。 谢谢!
extends KinematicBody2D
const WALK = 40
const SPRINT = 70
const GRAVITY = 10
const JUMP = -250
const FLOOR = Vector2(0, -1)
const BONE = preload("res://Bone.tscn")
var is_attacking = false
var velocity = Vector2()
var on_ground = true
func _physics_process(delta):
if Input.is_action_pressed("D"):
if is_attacking == false:
velocity.x = WALK
$Player_Animation.play("Player_Run")
$Player_Animation.flip_h = true
if sign($Position2D.position.x) == -1:
$Position2D.position.x *= -1
elif Input.is_action_pressed("A"):
if is_attacking == false:
velocity.x = -WALK
$Player_Animation.play("Player_Run")
$Player_Animation.flip_h = false
if sign($Position2D.position.x) == 1:
$Position2D.position.x *= -1
else:
velocity.x = 0
if on_ground == true && is_attacking == false:
$Player_Animation.play("Player_Idle")
if Input.is_action_just_pressed("Space"):
if is_attacking == false:
if on_ground == true:
velocity.y = JUMP
on_ground = false
if Input.is_action_just_pressed( "ui_focus_next") && is_attacking == false:
is_attacking = true
$Player_Animation.play("Player_Attack")
var bone = BONE.instance()
if sign($Position2D.position.x) == 1:
bone.set_bone_direction(-1)
else:
bone.set_bone_direction(1)
get_parent().add_child(bone)
bone.position = $Position2D.global_position
velocity.y = velocity.y + GRAVITY
if is_on_floor():
on_ground = true
else:
if is_attacking == false:
on_ground = false
if velocity.y < 0:
$Player_Animation.play("Player_Jump")
else:
$Player_Animation.play("Player_Fall")
velocity = move_and_slide(velocity, FLOOR)
func _on_Sprite_animation_finished():
is_attacking = false
可能最简单的解决方案是以计时器节点的形式添加子弹延迟。它基本上是这样工作的:同时播放动画和启动计时器。如果在播放动画时计时器达到 0,则触发射弹。您可以将计时器设置为从动画开始到您希望射弹出现的确切时间。我在其他动画中使用了这种技术并取得了巨大成功。事实上,可以在动画期间的不同点触发许多效果。