Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Hyakkimaru |
WALK, IDLE transition to JUMP state doesn’t work .
When i press jump and im on the floor it goes to the JUMP state but for less than a second then goes to idle, but if press jump again while in the air it goes to the jump state.
extends KinematicBody2D
class_name Player
signal PlayerHealthChange
enum PLAYERSTATE{IDLE, WALK, JUMP, FALL, ATTACK, HURT, DEAD}
export (PLAYERSTATE) var currentState=PLAYERSTATE.IDLE
export (PLAYERSTATE) var previousState=null
export(Resource) var PlayerMovement
const Damage = 5
var movement = Vector2.ZERO
var hit_direction = Vector2.ZERO
var PlayerLife = 30
var is_attacking = false
var is_hit = false
var dead = false
var moving_right = true
var knockbackri = false
var left
var right
var jump
var attack
onready var remoteTransform2D: = $RemoteTransform2D
onready var AnimatedSprite: = $AnimatedSprite
onready var HitBox: = get_node("AttackHitBox/HitBox")
onready var attackHitBox: = get_node("AttackHitBox")
onready var labelState:= get_node("Label")
func _ready():
emit_signal("PlayerHealthChange", PlayerLife)
func _physics_process(delta):
HandleInput()
executeState()
apply_gravity()
apply_friction()
movement = move_and_slide(movement, Vector2.UP)
func executeState():
match currentState:
PLAYERSTATE.IDLE:
labelState.text="IDLE"
AnimatedSprite.play("idle")
movement.x = 0
pass
PLAYERSTATE.WALK:
labelState.text="WALK"
if left and is_on_floor():
movement.x=-PlayerMovement.MAX_SPEED
AnimatedSprite.set_flip_h(true)
attackHitBox.scale.x = -1
if right and is_on_floor():
movement.x=PlayerMovement.MAX_SPEED
AnimatedSprite.set_flip_h(false)
attackHitBox.scale.x = 1
if AnimatedSprite.animation !="run" and is_on_floor():
AnimatedSprite.play("run")
pass
PLAYERSTATE.JUMP:
labelState.text="JUMP"
if can_jump():
movement.y =-PlayerMovement.JUMP_SPEED
else:
AnimatedSprite.play("jump")
if left and not is_on_floor():
movement.x=-PlayerMovement.MAX_SPEED
AnimatedSprite.set_flip_h(true)
attackHitBox.scale.x = -1
if right and not is_on_floor():
movement.x=PlayerMovement.MAX_SPEED
AnimatedSprite.set_flip_h(false)
attackHitBox.scale.x = 1
pass
PLAYERSTATE.FALL:
labelState.text="FALL"
if not is_on_floor():
AnimatedSprite.play("fall")
pass
PLAYERSTATE.ATTACK:
labelState.text="ATTACK"
attack()
pass
PLAYERSTATE.HURT:
labelState.text="HURT AND KNOCKBACK"
AnimatedSprite.play("taking_hit")
if moving_right:
movement.x = 80 if knockbackri else -80
if(PlayerLife<=0):
dead=true
PLAYERSTATE.DEAD:
labelState.text="DEAD"
player_died()
changeState()
func changeState():
previousState=currentState
match currentState:
PLAYERSTATE.IDLE:
if left or right:
currentState=PLAYERSTATE.WALK
if attack:
currentState=PLAYERSTATE.ATTACK
if jump:
currentState=PLAYERSTATE.JUMP
if is_hit:
currentState=PLAYERSTATE.HURT
pass
PLAYERSTATE.WALK:
if not left and not right:
currentState=PLAYERSTATE.IDLE
if attack:
currentState=PLAYERSTATE.ATTACK
if jump:
currentState=PLAYERSTATE.JUMP
if is_hit:
currentState=PLAYERSTATE.HURT
pass
PLAYERSTATE.JUMP:
if is_on_floor():
currentState=PLAYERSTATE.IDLE
if attack:
currentState=PLAYERSTATE.ATTACK
if right and is_on_floor() or left and is_on_floor():
currentState=PLAYERSTATE.WALK
if is_hit:
currentState=PLAYERSTATE.HURT
pass
PLAYERSTATE.FALL:
pass
PLAYERSTATE.ATTACK:
if not is_attacking:
currentState=PLAYERSTATE.IDLE
pass
PLAYERSTATE.HURT:
if not is_hit:
currentState=PLAYERSTATE.IDLE
if dead:
currentState=PLAYERSTATE.DEAD
pass
PLAYERSTATE.DEAD:
pass
func HandleInput():
left = Input.is_action_pressed("left")
right = Input.is_action_pressed("right")
jump = Input.is_action_just_pressed("jump")
attack = Input.is_action_just_pressed("attack")
if right:
moving_right = true
func apply_gravity():
movement.y += PlayerMovement.GRAVITY
if movement.y>PlayerMovement.MAX_GRAVITY:
movement.y=PlayerMovement.MAX_GRAVITY
func connect_camera(camera):
var camera_path = camera.get_path()
remoteTransform2D.remote_path = camera_path
func player_died():
set_physics_process(false)
get_tree().reload_current_scene()
func apply_friction():
movement.x = move_toward(movement.x, 0, PlayerMovement.FRICTION)
func attack():
if AnimatedSprite.animation != "attack":
is_attacking = true
HitBox.disabled = false
AnimatedSprite.play("attack")
SoundPlayer.play_sound(SoundPlayer.ATTACK)
func _on_AnimatedSprite_animation_finished():
if AnimatedSprite.animation == "attack":
is_attacking=false
HitBox.disabled = true
if AnimatedSprite.animation == "taking_hit":
is_hit=false
AnimatedSprite.play("idle")
func TakeDamage(damage, right):
if not is_hit:
PlayerLife -= damage
SoundPlayer.play_sound(SoundPlayer.HURT)
knockbackri = right
emit_signal("PlayerHealthChange", PlayerLife)
is_hit = true
Global.lifeLevel = PlayerLife
func _on_AttackHitBox_body_entered(body):
if body.has_method("TakeDamage"):
body.TakeDamage(Damage)
func can_jump():
return is_on_floor()
here’s the video showing : bandicam 2023-03-08 11-01-45-536 - VEED