Spawn animation keeps getting replaced with Movement animations

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 2Dfanatic

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 _physics_process(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"

spawn_biomcbody_all. Never plays and directly jumps to next if statement

if cur_wep == “holster”:
mc_anim = “idle_biomcbody_all”

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

Also when i did tried to execute the spawn_biomcbody_all 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 _physics_process() ?

:bust_in_silhouette: Reply From: Surtarso

Your code will most likely play only the 1st frame of the spawn_biomcbody_all 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

Is use a signal from animation player node

func _on_MC_AnimationPlayer_animation_finished(spawn_biomcbody_all):
	cur_wep = "holster"

2Dfanatic | 2020-11-06 13:43