Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | hiruy |
extends KinematicBody2D
const type = "Player"
signal health_updated(health)
signal killed()
const UP = Vector2(0,-1)
const GRAVITY = 30
const MAXFALLSPEED = 5500
const MAXSPEED = 800
const jumpforce = 1675
const accel = 20
export var max_health = 10
onready var health = max_health setget _set_health
onready var invulnerability_timer = $invulnerabilityTimer
var motion = Vector2()
var facing_right = true
var isAttacking = false;
var is_dead = false
func _physics_process(delta):
if is_dead == false:
motion.y += GRAVITY
if motion.y > MAXFALLSPEED:
motion.y = MAXFALLSPEED
if facing_right == true:
$Sprite.scale.x = 10
else:
$Sprite.scale.x = -10
motion.x = clamp(motion.x,-MAXSPEED,MAXSPEED)
if Input.is_action_pressed("right") && isAttacking == false:
motion.x += accel
facing_right = true
get_node("attackarea").position = Vector2(25, 1)
$AnimationPlayer.play("run")
elif Input.is_action_pressed("left") && isAttacking == false:
motion.x -= accel
get_node("attackarea").position = Vector2(-225, 1)
facing_right = false
$AnimationPlayer.play("run")
else:
motion.x = 0;
if isAttacking == false:
$AnimationPlayer.play("idle")
if is_on_floor():
if Input.is_action_just_pressed("jump"):
motion.y = -jumpforce
if !is_on_floor():
if motion.y < 0:
$AnimationPlayer.play("jump")
elif motion.y > 0:
$AnimationPlayer.play("fall")
if Input.is_action_just_pressed("attack"):
$AnimationPlayer.play("attack")
isAttacking = true;
$attackarea/attackcolision.disabled = false;
motion = move_and_slide(motion, UP * delta)
func _set_health(value):
if value > 0:
emit_signal("health_updated", value)
else:
dead()
var prev_health = health
health = clamp(value, 1, max_health)
if health != prev_health:
emit_signal("health_updated", health)
func damage(amount):
if invulnerability_timer.is_stopped():
invulnerability_timer.start()
_set_health(health - amount)
$AnimationPlayer.play("damage")
$AnimationPlayer.queue("flash")
func kill():
pass
func dead():
kill()
is_dead = true
Vector2(0, 0)
$AnimationPlayer.play("dead")
emit_signal("killed")
$Timer.start()
get_tree().reload_current_scene()
func heal(amount):
if is_dead == false:
health += amount
health = min(health, max_health)
emit_signal("health_updated", health)
print("%s got healed by %s points. Health: %s/%s" % [get_name(), amount, health, max_health])
func _on_invulnerabilityTimer_timeout():
$AnimationPlayer.play("rest")
func _on_AnimationPlayer_animation_finished(attack):
$attackarea/attackcolision.disabled = true;
isAttacking = false;
func _on_Timer_timeout():
get_tree().reload_current_scene()
func _on_Area2D_body_entered(body):
damage(1)
print(health)
“Isn’t working” is way too vague for anyone to help. Please, help us to help you. For starters:
- Describe the problem in detail
- How do you expect it to work?
- What exactly isn’t working as expected?
- Do you get any errors? If so, please past the entire error message.
- When posting code, format it for the forum (using the
{ }
button in the editors toolbar). Otherwise, it’s nearly impossible to read.
Just dumping a wall of unformatted code in a post and saying “this isn’t working” won’t get you to a solution. If you expect someone to spend the (likely considerable) time it’ll take to assist you, at least take the time provide the necessary details.
jgodfrey | 2022-07-29 16:59
I agree with Jgodfrey
godot_dev_ | 2022-07-29 17:05
-my problem is there are invulnerability and damage animations when I take damage I want them to activate.
-the animation isn’t activating
-no I don’t have any errors
-thanks for your advice i didn’t know that
hiruy | 2022-07-29 17:05
if there is some thing you want to know pls tell me
hiruy | 2022-07-29 17:07
Please edit your post and format the code for the forum. To do that…
- Edit the original post
- Select the code
- Press the
{ }
button in the forum’s mini-toolbar. - Look at the
Preview
section (below the forum edit box) to verify the code’s format - Save the edited message.
jgodfrey | 2022-07-29 17:13
thanks this helped
hiruy | 2022-07-29 17:16
Maybe its about this code
func _on_AnimationPlayer_animation_finished(attack):
it Should be like
func _on_AnimationPlayer_animation_finished(anim_name):
And Whats The Problem BTW?
Beatrix | 2022-07-29 17:44
the ANIMATION ISNT PLAYING WHEN I TAKE DAMAGE
hiruy | 2022-07-29 17:51
but do you take damage or the you take damage but animation is not playing?
Beatrix | 2022-07-29 17:55
I take damage but the animation isnt playing
hiruy | 2022-07-29 17:56
Well You Start A Timer On The Damage Function
invulnerability_timer.start()
when that timer end this function runs
func _on_invulnerabilityTimer_timeout():
$AnimationPlayer.play("rest")
so the animation will change to rest right?
Beatrix | 2022-07-29 18:01
yes i want it to change to rest
hiruy | 2022-07-29 18:03
Here Change The
func _on_AnimationPlayer_animation_finished(attack):
to
func _on_AnimationPlayer_animation_finished(anim_name):
if anim_name == "attack":
$attackarea/attackcolision.disabled = true;
isAttacking = false;
if anim_name == "damage":
$Animation.play("rest")
remove this
if invulnerability_timer.is_stopped():
invulnerability_timer.start()
Beatrix | 2022-07-29 18:08
excuse me this didn’t work if you want more info about the animation it is an animation that flashes and turns the character red when token damage the rest animation just turns things back to normal it something has gone wrong
hiruy | 2022-07-29 18:18
jus t upload the project ill try my best to fix it
Beatrix | 2022-07-29 18:23
ok here you go GitHub - hiruy56/game: this is my 2d game
hiruy | 2022-07-29 18:33
great give me an 10-30 minute
Beatrix | 2022-07-29 18:35