Hello ! New to the forum (as an asker) but I'd really love some help ! Touching both of my enemies (based on a similar code) on a special spot (center top or center bottom) makes them disappear ! They do not go through the "dying" phase and they just disappear from the game (a line making a += 1 on my batteries while alive confirms it). The problem is I don't see a queue_free susceptible to erase them anywhere ! I'll share my code with you lovely people still and wish upon a star some gentle and smart soul comes and save me from this 2 weeks nightmare.
Thank you so much !
Enemy 1
extends KinematicBody2D
const PLAYER = preload("res://SCENES/Player/Player.tscn")
const BULLET = preload("res://SCENES/Objects/Bullet.tscn")
const FALLDUST = preload("res://SCENES/Objects/FallDust.tscn")
var hp = 30
var speed = 150
var velocity = Vector2()
export var direction = -1
var idle = true
var attack0 = false
var attack1 = false
var attack2 = false
var attack_counter = 0
var damage = 25
var character_instance = preload("res://SCENES/Characters/Shrooms/Ellen.tscn")
var character = character_instance.instance()
func _ready():
if direction == 1:
$AnimatedSprite.flip_h = true
##Movement ??
func _physics_process(delta):
if hp > 0:
if is_on_wall():
direction *= -1
$AnimatedSprite.flip_h = not $AnimatedSprite.flip_h
velocity.y += Global.gravity
if hp > 0:
if idle :
$AnimatedSprite.play("idle")
velocity.x = (speed * direction) - (hp/4 * direction)
attack_counter += 1
if attack_counter > 200 + hp/4:
idle = false
attack0 = true
attack_counter = 0
elif attack0 :
$AnimatedSprite.play("attack0")
velocity.x = 0
attack_counter += 1
if attack_counter > 20 + hp/4:
attack_counter = 0
attack0 = false
attack1 = true
elif attack1 :
$AnimatedSprite.play("jump")
if is_on_floor():
velocity.y = -500
attack1 = false
attack2 = true
elif attack2 :
if !is_on_floor():
velocity.x = (speed * 1.5 * direction)
else :
$AnimatedSprite.play("land")
velocity.x = 0
if attack_counter < 1:
get_parent().get_parent().add_child(FALLDUST.instance())
attack_counter += 1
if attack_counter > 60:
attack2 = false
idle = true
velocity = move_and_slide(velocity, Vector2.UP)
#DEATH
if hp <= 0:
if $AnimatedSprite.scale.x <= 1.5 :
$Explosion.visible = true
$Explosion.playing = true
$CollisionShape2D.scale.y += 0.002
$AnimatedSprite.play("shocked")
$Particles2D.one_shot = true
$AnimatedSprite.scale.x += 0.001
$AnimatedSprite.scale.y += 0.001
if $AnimatedSprite.scale.x >= 1.5 :
$AnimatedSprite.play("death")
velocity = Vector2(0,0)
set_collision_layer_bit(4, false)
set_collision_mask_bit(0, false)
$sides_checker.set_collision_layer_bit(4, false)
$sides_checker.set_collision_mask_bit(0, false)
$Battery.parent_dead = true
## HURT
func ouch(var enemyposx, var bouncexforce):
$HurtTimer.start()
set_modulate(Color(1,0.3,0.3,0.7))
$AnimatedSprite.play("shocked")
func _on_HurtTimer_timeout():
set_modulate(Color(1,1,1,1))
## COLLISION PLAYER
func _on_sides_checker_body_entered(body):
var layer = body.get_collision_layer()
if layer == 1:
Global.batteries -= damage
body.ouch(position.x, 500)
##DIE AND CREATE
func _on_AnimatedSprite_animation_finished():
if $AnimatedSprite.scale.x >= 1.5:
var pos = global_position
character.global_position = pos
$"../../PNJ".add_child(character)
queue_free()
Enemy 2
extends KinematicBody2D
const PLAYER = preload("res://SCENES/Player/Player.tscn")
const BULLET = preload("res://SCENES/Objects/Bullet.tscn")
#const BADBULLET = preload("")
var hp = 50
var speed = 150
var velocity = Vector2()
export var direction = -1
var attack = 0
var attack_counter = 0
var next_attack = false
var damage = 25
##Movement ??
func _physics_process(delta):
Global.batteries += 1
if hp > 0:
attack_counter += 1
if attack == 0 :
if attack_counter < 150:
$AnimatedSprite.play("idle")
elif attack_counter > 150 + hp:
$AnimatedSprite.play("land")
next_attack = true
elif attack == 1 : #OUT AND SHOOT
$AnimatedSprite.play("out")
if attack_counter == 50 or attack_counter == 100 or attack_counter == 150 :
pass
if attack_counter > 200 + hp:
attack += 1
pass
elif attack == 2 :
$AnimatedSprite.play("jump")
next_attack = true
elif attack >= 3:
attack = 0
next_attack = false
attack_counter = 0
#DEATH
if hp <= 0:
$AnimatedSprite.play("death")
speed = 0
velocity = Vector2(0,0)
set_collision_layer_bit(4, false)
set_collision_mask_bit(0, false)
$sides_checker.set_collision_layer_bit(4, false)
$sides_checker.set_collision_mask_bit(0, false)
$Battery.parent_dead = true
func _on_sides_checker_body_entered(body):
var layer = body.get_collision_layer()
if layer == 1:
Global.batteries -= damage
body.ouch(position.x, 500)
func _on_AnimatedSprite_animation_finished():
if next_attack == true:
attack += 1
attack_counter = 0
next_attack = false
Player
extends KinematicBody2D
##POWERS
export var dark = false
export var hug = false
export var lovegun = true
export var lovesword = true
###CARACS
export var speed = 12
var velocity = Vector2(0,0)
export var gravity = 15
export var slide_speed = 25
export var jump_force = -380
export var wall_jumpx = 250
export var wall_jumpy = -330
export var bounce_force = 0.7
export var batteries = 0
export var stop_speed = 12
export var max_speed = 200
export var starting_speed = 40
var bullet_cost = Global.bullet_cost
var sword_cost = Global.sword_cost
##ANIMATIONS
var swordattacking = false
##GLOBAL
const BULLET = preload("res://SCENES/Objects/Bullet.tscn")
const SWORD = preload("res://SCENES/Objects/LoveSword.tscn")
#CAMERA
var collision_shape = 0
var size = 0
var cam = 0
func _ready():
position = Vector2(Global.wheretox, Global.wheretoy)
## POWERS START
if dark == false:
$Light2D.enabled = false
if lovegun == false:
$SpriteGun.visible = false
if lovesword == false:
$SpriteSword.visible = false
#CAMERA MOVEMEMENT
func _on_RoomDetector_area_entered(area: Area2D) -> void:
collision_shape = area.get_node("CollisionShape2D")
size = collision_shape.shape.extents*2
cam = $Camera2D
func _physics_process(delta):
##MORE CAMERA MOVEMENT
if sign($Position2D.position.x) == -1 :
if cam.global_position.x > collision_shape.global_position.x - size.x/2 - cam.offset.x :
if cam.offset.x >= -74 :
cam.offset.x -= cam.smoothing_speed * (1-(cam.offset.x/-74))
else : pass
else :
if cam.global_position.x < collision_shape.global_position.x + size.x/2 + cam.offset.x :
if cam.offset.x <= 74:
cam.offset.x += cam.smoothing_speed * (1-(cam.offset.x/74))
cam.limit_top = collision_shape.global_position.y - size.y/2
cam.limit_left = collision_shape.global_position.x - size.x/2 - cam.offset.x
cam.limit_bottom = collision_shape.global_position.y + size.y/2
cam.limit_right = collision_shape.global_position.x + size.x/2 - cam.offset.x
if Global.talking == false:
#MOVING
if Input.is_action_pressed("ui_right"):
velocity.x += speed
$Sprite.flip_h = false
$SpriteGun.flip_h = false
$SpriteSword.flip_h = false
if not next_to_wall():
if sign($Position2D.position.x) == -1:
$Position2D.position.x *= -1;
if Input.is_action_pressed("ui_left"):
velocity.x -= speed
$Sprite.flip_h = true
$SpriteGun.flip_h = true
$SpriteSword.flip_h = true
if not next_to_wall():
if sign($Position2D.position.x) == 1:
$Position2D.position.x *= -1;
if !Input.is_action_pressed("ui_left") and !Input.is_action_pressed("ui_right") or Input.is_action_pressed("ui_left") and Input.is_action_pressed("ui_right"):
if velocity.x > stop_speed and velocity.x != 0:
velocity.x -= stop_speed;
elif velocity.x < -stop_speed:
velocity.x += stop_speed
else:
velocity.x = 0;
if Input.is_action_just_pressed("ui_select"):
if is_on_floor():
velocity.y = jump_force
if next_to_right_wall():
velocity.y += wall_jumpy
velocity.x -= wall_jumpx
if next_to_left_wall():
velocity.y += wall_jumpy
velocity.x += wall_jumpx
if not next_to_wall():
velocity.y += gravity
elif next_to_wall() and velocity.x == 0 :
velocity.y = slide_speed;
velocity = move_and_slide(velocity, Vector2.UP)
if velocity.x > max_speed:
velocity.x -= speed;
if velocity.x < -max_speed:
velocity.x += speed;
#SHOOTING
if Input.is_action_just_pressed("ui_changeweapon"):
Global.weapon_selected += 1
if Global.weapon_selected > Global.weapons_available:
Global.weapon_selected = 1
if lovegun == true and Global.weapon_selected == 2:
if Input.is_action_just_pressed("ui_shoot"):
if Global.batteries > 0:
Global.batteries -= bullet_cost
var bullet = BULLET.instance()
get_parent().add_child(bullet)
if Input.is_action_pressed("ui_up") and !next_to_wall():
bullet.set_bullet_directiony(-1)
bullet.position = global_position
elif Input.is_action_pressed("ui_down")and !next_to_wall() and !is_on_floor():
bullet.set_bullet_directiony(1)
bullet.position = global_position
elif Input.is_action_pressed("ui_down")and !next_to_wall():
bullet.set_bullet_directionx(1)
bullet.position = $Position2D.global_position
elif sign($Position2D.position.x) == 1:
bullet.set_bullet_directionx(1)
bullet.position = $Position2D.global_position
else:
bullet.set_bullet_directionx(-1)
bullet.position = $Position2D.global_position
else: pass
elif lovesword == true and Global.weapon_selected == 1 and swordattacking == false:
if Input.is_action_just_pressed("ui_shoot"):
if Global.batteries > 0:
swordattacking = true
Global.batteries -= sword_cost
var sword = SWORD.instance()
get_parent().add_child(sword)
if Input.is_action_pressed("ui_up") and !next_to_wall():
sword.set_bullet_directiony(-1)
sword.position.x = global_position.x
sword.position.y = global_position.y - 16
elif Input.is_action_pressed("ui_down")and !next_to_wall() and !is_on_floor():
sword.set_bullet_directiony(1)
sword.position = global_position
elif Input.is_action_pressed("ui_down")and !next_to_wall():
sword.set_bullet_directionx(1)
sword.position = $Position2D.global_position
elif sign($Position2D.position.x) == 1:
sword.set_bullet_directionx(1)
sword.position = $Position2D.global_position
else:
sword.set_bullet_directionx(-1)
sword.position = $Position2D.global_position
elif lovesword == true : velocity.x /= 2
if batteries > 999:
batteries = 999
# ANIMATION
##WEAPONS
if Global.weapon_selected == 1:
$SpriteSword.visible = true
else :
$SpriteSword.visible = false
if Global.weapon_selected == 2:
$SpriteGun.visible = true
else :
$SpriteGun.visible = false
if next_to_wall():
$Sprite.play("WallSlide")
$SpriteGun.play("WallSlide")
$SpriteSword.play("WallSlide")
if next_to_left_wall():
$Sprite.flip_h = false
$SpriteGun.flip_h = false
if sign($Position2D.position.x) == -1:
$Position2D.position.x *= -1;
if next_to_right_wall():
$Sprite.flip_h = true
$SpriteGun.flip_h = true
if sign($Position2D.position.x) == 1:
$Position2D.position.x *= -1;
elif swordattacking == true:
$Sprite.play("SwordAttack")
$SpriteSword.play("SwordAttack")
elif not is_on_floor() and Input.is_action_pressed("ui_up"):
$Sprite.play("JumpUp")
$SpriteGun.play("JumpUp")
$SpriteSword.play("JumpUp")
elif not is_on_floor() and Input.is_action_pressed("ui_down"):
$Sprite.play("JumpDown")
$SpriteGun.play("JumpDown")
$SpriteSword.play("JumpDown")
elif not is_on_floor():
$Sprite.play("Jump")
$SpriteGun.play("Jump")
$SpriteSword.play("Jump")
elif velocity.x < -stop_speed and Input.is_action_pressed("ui_up") or velocity.x > stop_speed and Input.is_action_pressed("ui_up"):
$Sprite.play("RunningUp")
$SpriteGun.play("RunningUp")
$SpriteSword.play("RunningUp")
elif velocity.x < -stop_speed or velocity.x > stop_speed:
$Sprite.play("Running")
$SpriteGun.play("Running")
$SpriteSword.play("Running")
elif Input.is_action_pressed("ui_up"):
$Sprite.play("LookUp")
$SpriteGun.play("LookUp")
$SpriteSword.play("LookUp")
elif Input.is_action_pressed("ui_down"):
$Sprite.play("LookDown")
$SpriteGun.play("LookDown")
$SpriteSword.play("LookDown")
else:
$Sprite.play("Idle")
$SpriteGun.play("Idle")
$SpriteSword.play("Idle")
## WALLJUMP
func next_to_wall():
if hug == true:
if not is_on_floor():
return next_to_right_wall() or next_to_left_wall()
func next_to_right_wall():
if not is_on_floor() and velocity.y >= 0:
return $RightWall.is_colliding()
if sign($Position2D.position.x) == 1:
$Position2D.position.x *= -1;
func next_to_left_wall():
if not is_on_floor() and velocity.y >= 0:
return $LeftWall.is_colliding()
if sign($Position2D.position.x) == -1:
$Position2D.position.x *= -1;
func _on_deathzone_body_entered(body):
get_tree().change_scene("res://Level1.tscn")
func bounce():
velocity.y = jump_force * bounce_force
func ouch(var enemyposx, var bouncexforce):
$HurtTimer.start()
set_modulate(Color(1,0.3,0.3,0.7))
velocity.y = jump_force * bounce_force
if position.x < enemyposx:
velocity.x = -bouncexforce
elif position.x > enemyposx:
velocity.x = bouncexforce
Input.action_release("ui_left")
Input.action_release("ui_right")
func _on_HurtTimer_timeout():
set_modulate(Color(1,1,1,1))
func _on_Sprite_animation_finished() -> void:
if swordattacking == true :
swordattacking = false