0 votes

When i pressed KEY_UP play action animation of a player but cause crash if you continue pressed in this case is for running.

func ready():
set
process(true)
getnode("SpatialPlayer/AnimationPlayer").play("playerstopwithanimgun")
getnode("SpatialPlayer/AnimationPlayer").setspeed(0.2)
set
process_input(true)

func _process(delta):
pass

func input(event):
if (event.type==InputEvent.KEY and event.scancode == KEY
UP and event.pressed):
getnode("SpatialPlayer/AnimationPlayer").play("running")
getnode("SpatialPlayer/AnimationPlayer").setspeed(0.5)
else:
get
node("SpatialPlayer/AnimationPlayer").play("playerstopwithanimgun")
get
node("SpatialPlayer/AnimationPlayer").setspeed(0.2)

I need with key_UP pressed animation continue in loop without crash

Any ideia to solve this?

in Engine by (102 points)

1 Answer

0 votes

I would try something like this for your check:

if event.type==InputEvent.KEY and event.scancode == KEYUP and not event.echo:
    if event.pressed:
        getnode("SpatialPlayer/AnimationPlayer").play("running")
        getnode("SpatialPlayer/AnimationPlayer").setspeed(0.5)
    else:
        getnode("SpatialPlayer/AnimationPlayer").play("playerstopwithanimgun")
        getnode("SpatialPlayer/AnimationPlayer").setspeed(0.2)

The only code I added was "not event.echo." This will ignore continuous press events, which is actually what you want. Now, when KEYUP is pressed, the first "if" condition will happen. When KEYUP is let go, the second "if" condition will play.

As for why your program crashed, my guess is that your "if" statement runs the second condition for all events that aren't a KEYUP press. There are a lot of them. To see that, add this line to the beginning of your input function:

print(event)

You will see a ton of events written to the console. Each time, your second "if" condition happened.

by (156 points)

not work that

I just declare two variables in main scene and solve that

func ready():
set
fixedprocess(true)
# variable for check Node control of Animatios
anim
player = getnode("SpatialPlayer/AnimationPlayer")

   set_process_input(true)

func fixedprocess(delta):
if (anim != animnew):
anim
new = anim
anim_player.play(anim)

func input(event):
if (event.type==InputEvent.KEY and event.scancode == KEY
UP and event.pressed):
anim = "Running"
animplayer.setspeed(0.5)
else:
anim = "IdlewithAnimGun"
anim
player.set_speed(0.2)
.
.
.
for another keys not working
any idea?

Done!

I changed else: per elif and it worked

func input(event):
if (event.type==InputEvent.KEY and event.scancode == KEY_UP and event.pressed):
anim = "Running"
animplayer.setspeed(0.6)

elif (event.type==InputEvent.KEY and event.scancode == KEYDOWN and event.pressed):
anim = "idle
withgunanim"
animplayer.setspeed(0.2)

elif (event.type==InputEvent.KEY and event.scancode == KEY_SPACE and event.pressed):
anim = "Running"
animplayer.setspeed(0.5)

Atemption now i used else for default animation

else:
anim = "default"
animplayer.setspeed(0.2)

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 Frequently asked questions and 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 [email protected] with your username.