![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | D.Q.Li |
I have completed an godot tutorial and have successfully produced a simple game demo.
However, when I am trying to add in my own design – a healing action that stops the player from moving and regain health after a duration of time. The engine says
“Invalid type in function ‘move_and_slide’ in base ‘KinematicBody2D (operation.gd)’. Cannot convert argument 1 from int to Vector2.”
I don’t really understand why it suddently crashed because I used the same structure as how other actions are designed.
the code is a bit long so I hope noting will help understanding it:
extends KinematicBody2D
const acceleration = 400
const maxspeed = 60
const friction = 500
const roll_speed = 120
const player_hurt_sound = preload("res://Action RPG Resources/Music and Sounds/special_sound_effect.tscn")
#set up basic variables that makve moving more realistic
#to change initial health check Player_stats
enum{
move
roll
attack
heal
}
#setup actions
var state = move
var velocity = Vector2.ZERO
var roll_vector = Vector2.DOWN
var stats = PlayerStats
var time_end = false
onready var animationPlayer = $AnimationPlayer
onready var animationtree = $AnimationTree
onready var animationstate = animationtree.get("parameters/playback")
onready var SwordHitbox = $hitboxpivot/sword_hitbox
onready var hurtbox = $hurtbox
onready var blinkAnimation = $blink
onready var timer = $Timer
func _ready():
stats.connect("no_health",self, "queue_free")
animationtree.active = true
SwordHitbox.knockback_vector = roll_vector
randomize()
#set up animation
func _physics_process(delta):
match state:
move:
move_state(delta)
roll:
roll_state(delta)
attack:
attack_state(delta)
heal:
heal_state(delta)
#set how character move
func move_state(delta):
var inputVector = Vector2.ZERO
inputVector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
inputVector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
inputVector = inputVector.normalized()
#set up how input affect character
if inputVector != Vector2.ZERO:
roll_vector = inputVector
SwordHitbox.knockback_vector = inputVector
animationtree.set("parameters/idle/blend_position", inputVector)
animationtree.set("parameters/move/blend_position", inputVector)
animationtree.set("parameters/attack/blend_position", inputVector)
animationtree.set("parameters/roll/blend_position", inputVector)
animationstate.travel("move")
velocity = velocity.move_toward(inputVector*maxspeed,acceleration*delta)
else:
animationstate.travel("idle")
velocity = velocity.move_toward(Vector2.ZERO, friction*delta)
#set how character move
move()
if Input.is_action_just_pressed("roll"):
state = roll
print("roll_Activated")
#for debug
if Input.is_action_just_pressed("attack"):
state = attack
print("attack_Activated")
#for debug
if Input.is_action_just_pressed("heal"):
state = heal
print("healing")
func roll_state(delta):
velocity = roll_vector*roll_speed
animationstate.travel("roll")
move()
print("rolling")
func heal_state(duration):
velocity = 0
timer.start(duration)
print("heal_state")
#start healing time
if(time_end):
PlayerStats.health +=1
move()
#move when healing is complete
func move():
velocity = move_and_slide(velocity)
func attack_state(delta):
velocity = Vector2.ZERO
animationstate.travel("attack")
func attact_animation_finished():
velocity = Vector2.ZERO
state = move
func roll_animation_finished():
state = move
func _on_hurtbox_area_entered(area):
stats.health -= area.damage
print("I'm hit")
hurtbox.start_invincibility(1)
hurtbox.create_hit_effect()
var playerHurtSound = player_hurt_sound.instance()
get_tree().current_scene.add_child(playerHurtSound)
func _on_hurtbox_invincibility_started():
blinkAnimation.play("start")
func _on_hurtbox_invincibility_ended():
blinkAnimation.play("stop")
func _on_Timer_timeout():
time_end = true
# tell heal state that healing is done