0 votes

if Input.is.actionpressed("attackjab"):
animationsprite.animation = "jab"

im new to coding and im not sure what to look up.
ill be looking stuff up in the mean time.

im making a 2D fighter, and i dont see many others doing it.

Godot version v3.2.3
in Engine by (12 points)

2 Answers

+1 vote

You only posted two lines of code, but I'm guessing that you have some code that sets the animation to an idle animation or no animation at all if there's no button pressed. A simple solution to this would be to have a boolean that keeps track of whether the player is attacking or not and not play the idle/no animation if the player is attacking.

by (7,839 points)

ahh, i shouldve posted my whole code. it was bits and pieces from other tutorials. ill paste what i have now. currently after my jab animation it freezes. i have no walking animation yet.

Blockquote

extends KinematicBody2D

var movement = Vector2();
var up = Vector2(0, -1);
var speed = 200
var isAttacking = false

func _process(delta):

#button inputs
if Input.is_action_pressed("move_left") && isAttacking == false:
    movement.x -= speed;
if Input.is_action_pressed("move_right") && isAttacking == false:
    movement.x += speed;
if Input.is_action_just_pressed("attack_jab"):
    $animatedsprite.play("jab");
    isAttacking = true
movement = move_and_slide(movement, up * delta);

func onAnimatedSpriteanimationfinished():
if $Animatedsprite.animation == "jab":
isAttacking = false;

Blockquote

What do you mean by freeze? As in the character stops responding to inputs or the character stops being animated?

the character stop responding to inputs

Are you sure you're accessing the AnimatedSprite correctly? I see that you have two different capitalizations of it. Are you getting an error message?

i get an error message. it was saying something about "func onAnimationSpriteanimationfinished()" not being linked correctly. i connected that from the node tab. i saw that in tutorial. it worked for the guy that did it. he was makin a mega man like game and was adding a slash animation then returning to idle. i started over in a new project. ive been watching tutorials, but im trying to pick what i can use from each since most ppl make RPG's or platformers. im making a 2D fighter. this is my current code.

 extends KinematicBody2D

const SPEED = 100
const GRAVITY = 10
const JUMP_POWER = -150
const FLOOR = Vector2(0, -1)

var velocity = Vector2()

var on_ground = false

func physicsprocess( _delta):

if Input.is_action_pressed("ui_left"):
    velocity.x = -SPEED

elif Input.isactionpressed("move_right"):
velocity.x = SPEED
else:
velocity.x = 0

if Input.is_action_pressed("ui_up"):
    if on_ground == true:
        velocity.y = JUMP_POWER
        on_ground = false

velocity.y += GRAVITY


if is_on_floor():
    on_ground = true
else:
    on_ground = false 

velocity = move_and_slide(velocity, FLOOR)

this is working. i just have to attempt to set up the jab animation in the code.

Can you just copy and paste the error here?

going bac to my first project, somehow i made 2 scripts with realizing i did. i think thats where the issue is...

but in my debugger im seeing:
"Script inherits from type 'KinematicBody2D', do it can't be instanced in object of type 'Node2D"

and i get a yellow message:
"Missing connected method'onAnimatedSpriteanimationfinished'for signal'animation_finished'from node'main scene/player 1/AnimatedSprite' to node 'main scene/player'."

Okay well can you put the correct scripts on the correct nodes?

You have the script on a Node2D instead of a KinematicBody2D so you should probably move it to the correct place.

probably, but ive already started a duplicate project. currently having the same scenario. i can do my jab animation only when i jump.

extends KinematicBody2D

const SPEED = 100
const GRAVITY = 10
const JUMP_POWER = -150
const FLOOR = Vector2(0, -1)

var velocity = Vector2()

var on_ground = false

func physicsprocess( _delta):

if Input.is_action_pressed("ui_left"):
    velocity.x = -SPEED
    $player.play("walk")

elif Input.isactionpressed("moveright"):
velocity.x = SPEED
$player.play("walk")
else:
if on
ground == true:
$player.play("idle")
velocity.x = 0

if Input.is_action_pressed("ui_up"):
    if on_ground == true:
        velocity.y = JUMP_POWER
        on_ground = false

if Input.is_action_pressed("attack_jab"):
    $player.play("jab")



velocity.y += GRAVITY

if is_on_floor():
    on_ground = true
else:
    on_ground = false 

velocity = move_and_slide(velocity, FLOOR)

this is my current script. all animations play properly except for jab..

this is the current error message:

E 0:00:59.239   emit_signal: Error calling method from signal 'animation_finished': 'KinematicBody2D(player.gd)::_on_player_animation_finished': Method not found..

<C++ Source> core/object.cpp:1260 @ emit_signal()

Looks like you connected the signal but deleted the function that it was connected to. You need to recreate the function.

it wasnt doing anything when i applied it

Well, then remove the signal to prevent the error then.

+1 vote

Try this

animationsprite.play("jab")

Check out the docs for more info on AnimationPlayer.

https://docs.godotengine.org/en/stable/classes/class_animationplayer.html

by (130 points)
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.