首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GDscript:完成动画后发射子弹

GDscript:完成动画后发射子弹
EN

Stack Overflow用户
提问于 2018-10-19 16:13:51
回答 1查看 522关注 0票数 0

我正在创建一个简单的2d平台游戏,我的问题是在我的动画中,子弹/骨头需要在动画完成后创建。现在,它创建骨,同时启动动画。

我试着把骨弹的创建放在onanimation_complete下,但是只有当我的角色面对左边时,它才允许我射击(按下“a”键给出的方向)。我遗漏了什么吗?

这是代码,如果有帮助的话。谢谢!

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-19 16:45:44

最简单的解决方案可能是以计时器节点的形式添加一个弹头延迟。它基本上是这样工作的:同时播放动画和启动计时器。如果动画播放时计时器达到0,则触发弹丸。您可以设置从动画开始到希望弹丸出现的确切时间量的计时器。我在其他动画中成功地使用了这个技巧。事实上,在动画过程中,可以在不同的点触发许多效果。

timer.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52896250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档