0 votes

Here's my Player node code

var MAX_SPEED = 160
var ACCELERATION = 2000
var motion = Vector2.ZERO
var target = Vector2()

onready var sprPlayercannonleftb = get_node("sprbioMC_cannon_leftb").hide()
onready var sprPlayercannonrightb = get_node("sprbioMC_cannon_rightb").hide()
onready var sprbioMCcannonleftb = get_node("sprbioMC_cannon_leftb")
onready var sprbioMCcannonrightb = get_node("sprbioMC_cannon_rightb")
onready var sprbioMCbody = get_node("sprbioMC_body")
onready var mc_animplayer = get_node("MC_AnimationPlayer")

var weps = {"spwan": {damage = 0},"holster" : {damage = 0},"cannon" : {damage = 3}}
var cur_wep = "spwan"
var mc_spwan : bool = true
var animation_has_played = false
onready var mc_anim = "spawn_biomcbody_all"

func control(delta):
    if Input.is_action_pressed("ui_holster"):
        cur_wep = "holster"
        sprbioMCcannonleftb.hide()
        sprbioMCcannonrightb.hide()
        # you can also trigger animations or play sound effects here
    elif Input.is_action_pressed("ui_wep2"):
        cur_wep = "cannon"
    target = get_global_mouse_position()
    $sprbioMC_cannon_leftb.look_at(target)
    $sprbioMC_cannon_rightb.look_at(target)
    if target.x > self.position.x :
        sprbioMCcannonleftb.set_flip_v(false)
    elif target.x < self.position.x :
        sprbioMCcannonrightb.set_flip_v(true)

func _physics_process(delta):
    target = get_global_mouse_position()
    if motion.x == 0:
        if not cur_wep =="spwan":
            if not animation_has_played:
                mc_animplayer.play("spawn_biomcbody_all")
                animation_has_played = true
            else:
                cur_wep = "holster"
        if cur_wep == "holster":
            mc_anim = "idle_biomcbody_all"
        elif cur_wep == "cannon":
            if target.x > self.position.x:
                sprbioMCbody.set_flip_h(false)
                mc_anim = "idle_biomcbody_nolefta" 
                sprbioMCcannonleftb.show()
                sprbioMCcannonrightb.hide()
            elif target.x < self.position.x :
                sprbioMCbody.set_flip_h(true)
                mc_anim = "idle_biomcbody_norighta" 
                sprbioMCcannonleftb.hide()
                sprbioMCcannonrightb.show()
    else:
        mc_anim = "runf_biomcbody_all"
    if target.x > self.position.x:
        sprbioMCbody.set_flip_h(false)
        if Input.is_action_pressed("ui_right") == true and Input.is_action_pressed("ui_down") == true:
            if cur_wep == "holster":
                mc_anim = "runf_biomcbody_all"
            elif cur_wep == "cannon":
                mc_anim = "runf_biomcbody_nolefta"
                sprbioMCcannonleftb.show()
                sprbioMCcannonrightb.hide()
        elif Input.is_action_pressed("ui_left") == true or Input.is_action_pressed("ui_down") == true:
            if cur_wep == "holster":
                mc_anim = "runb_biomcbody_all"
            elif cur_wep == "cannon":
                mc_anim = "runb_biomcbody_nolefta"
                sprbioMCcannonleftb.show()
                sprbioMCcannonrightb.hide()
        elif Input.is_action_pressed("ui_left") == true and Input.is_action_pressed("ui_up") == true:
            if cur_wep == "holster":
                mc_anim = "runb_biomcbody_all"
            elif cur_wep == "cannon":
                mc_anim = "runb_biomcbody_nolefta"
                sprbioMCcannonleftb.show()
                sprbioMCcannonrightb.hide()
        elif Input.is_action_pressed("ui_right") == true or Input.is_action_pressed("ui_up") == true :
            if cur_wep == "holster":
                mc_anim = "runf_biomcbody_all"
            elif cur_wep == "cannon":
                mc_anim = "runf_biomcbody_nolefta"
                sprbioMCcannonleftb.show()
                sprbioMCcannonrightb.hide()
    elif target.x < self.position.x :
        sprbioMCbody.set_flip_h(true)
        if Input.is_action_pressed("ui_right") == true and Input.is_action_pressed("ui_up") == true:
            if cur_wep == "holster":
                mc_anim = "runb_biomcbody_all"
            elif cur_wep == "cannon":
                mc_anim = "runb_biomcbody_norighta"
                sprbioMCcannonleftb.hide()
                sprbioMCcannonrightb.show()
        elif Input.is_action_pressed("ui_left") == true and Input.is_action_pressed("ui_down") == true:
            if cur_wep == "holster":
                mc_anim = "runf_biomcbody_all"
            elif cur_wep == "cannon":
                mc_anim = "runf_biomcbody_norighta"
                sprbioMCcannonleftb.hide()
                sprbioMCcannonrightb.show()
        elif Input.is_action_pressed("ui_right") == true or Input.is_action_pressed("ui_down") == true:
            if cur_wep == "holster":
                mc_anim = "runb_biomcbody_all"
            elif cur_wep == "cannon":
                mc_anim = "runb_biomcbody_norighta"
                sprbioMCcannonleftb.hide()
                sprbioMCcannonrightb.show()
        elif Input.is_action_pressed("ui_left") == true or Input.is_action_pressed("ui_up") == true:
            if cur_wep == "holster":
                mc_anim = "runf_biomcbody_all"
            elif cur_wep == "cannon":
                mc_anim = "runf_biomcbody_norighta"
                sprbioMCcannonleftb.hide()
                sprbioMCcannonrightb.show()
    mc_animplayer.play(mc_anim)
    var axis = get_input_axis()
    if axis == Vector2.ZERO:
        apply_friction(ACCELERATION * delta)
    else:
        apply_movement(axis * ACCELERATION * delta)
    motion = move_and_slide(motion)

func get_input_axis():
    var axis = Vector2.ZERO
    axis.x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))
    axis.y = int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))
    return axis.normalized()

func apply_friction(amount):
    if motion.length() > amount:
        motion -= motion.normalized() * amount
    else:
        motion = Vector2.ZERO

func apply_movement(acceleration):
    motion += acceleration
    motion = motion.clamped(MAX_SPEED)

Under physicsprocess(delta)

f motion.x == 0:
        if not cur_wep =="spwan":
            if not animation_has_played:
                mc_animplayer.play("spawn_biomcbody_all")
                animation_has_played = true
            else:
                cur_wep = "holster"

spawnbiomcbodyall. Never plays and directly jumps to next if statement

if curwep == "holster":
mc
anim = "idlebiomcbodyall"

Is there any way i can use signals or call method from the animation player to execute
spawnbiomcbodyall animation once as player is placed in the World ?

Also when i did tried to execute the spawnbiomcbodyall teh animation repeating over and over again even though i have made it not to loop under Animation player. Is this because it's under physicsprocess() ?

in Projects by (58 points)

1 Answer

+1 vote
Best answer

Your code will most likely play only the 1st frame of the spawnbiomcbodyall animation, and some other animation will override it later

You may want to yield the animation, try this:

yield(mc_animplayer, "animation_finished")

than set the bool to true

by (172 points)
selected by

Is use a signal from animation player node

func _on_MC_AnimationPlayer_animation_finished(spawn_biomcbody_all):
    cur_wep = "holster"
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories