0 votes

Hi, I am new to the Godot game engine and I am trying to create a simple platformer game with some spikes in which his life will be reduced if the character hits on it. I created a separate area 2d scene for the spike and imported it into the main scene where the player(It is the first level). I signaled it with the player script and reduced the health variable which if it reached zero the player dies. I don't have any problem with it. But I want to add another feature, whenever the player hits the spike, the sprite associated with it should change from a normal sprite to a sprite that contains blood in it. I implemented it with code, but the problem is that even if he hits one of the spikes, all the spikes change to the sprite with blood in it. I want to modify each of the spike's sprites individually which are the instances of the main sprite scene. Please let me know how to do that

This is my player script (Contains everything from his movement, crouching and health, etc). Also, he is a kinematic body 2d

    extends KinematicBody2D

    var isCrouching
    var isCealing
    var velocity = Vector2.ZERO
    var isdead = false

    export (int) var speed = 1200
    export (int) var jump_speed = 1800
    export (int) var gravity = 4000
    export (float) var fallMultiplayer = 0.25
    export (int) var crouchSpeedReduce = 300
export (int) var health = 5000
export (int) var damage = 200

onready var parent = get_parent()
onready var crouchCheck = $crouchCheck
onready var playersprite = $player/Sprite
onready var crouchsprite = $crouch/crouchsprite
onready var colloider = $player 
onready var crouch= $crouch
onready var deathsprite = $player/Sprite2
onready var damager = get_node("../damager")

func _movement(delta):
    velocity.x = 0
    if Input.is_action_pressed("Move_right") && !isCrouching:
        velocity.x += speed
    elif Input.is_action_pressed("Move_right") && isCrouching:
        velocity.x += (speed - crouchSpeedReduce)
    if Input.is_action_pressed("move_left") && !isCrouching:
        velocity.x -= speed
    elif Input.is_action_pressed("move_left") && isCrouching:
        velocity.x -= (speed - crouchSpeedReduce)

    velocity.y += gravity * delta

func _crouch():
    if crouch.disabled == false:
        isCrouching = true
        isCrouching = false

    if Input.is_action_pressed("Crouch"):
    elif isCrouching == true && isCealing == true :

    if crouchCheck.is_colliding() == true:
        isCealing = true
        isCealing = false

func _jump(delta):
    if !Input.is_action_pressed("Jump") && !isCrouching:
        velocity.y += gravity * delta 
        velocity.y += (gravity * fallMultiplayer) * delta

    velocity = move_and_slide(velocity, Vector2.UP)

    if Input.is_action_just_pressed("Jump") && !isCrouching:
        if is_on_floor():
            velocity.y -= jump_speed

func _crouchon ():
    crouch.disabled = false
    colloider.disabled = true
    playersprite.visible = false
    crouchsprite.visible = true
func _crouchoff ():
    crouch.disabled = true
    colloider.disabled = false
    playersprite.visible = true
    crouchsprite.visible = false    

#Actual Update function

#func _spriteChange():
#   if health < 5000:
#       damager.get_node("Spike").visible = false
#       damager.get_node("death_spike").visible = true
#   else:
#       damager.get_node("Spike").visible = true
#       damager.get_node("death_spike").visible = false

func _physics_process(delta):
    if !isdead:
        deathsprite.visible = true
        playersprite.visible = false
 #  _spriteChange()
    if health <= 0:
        isdead = true

 func _on_KinematicBody2D_body_entered(body: Node) -> void:
    health -= damage

And this is the code for the sprite

extends Area2D

onready var spike = $Spike
onready var death_spike = $death_spike

#func _physics_process(delta):
#   if data.isdamaged:
#       spike.visible = false
#       death_spike.visible = true

func _on_collisioncheck_area_entered(area):
        spike.visible = false
        death_spike.visible = true
Godot version 3.3.2
in Engine by (12 points)

1 Answer

+1 vote

The instances of the spike suppose to be individuals during the game. They should not jam with each other. Otherwise, there are some shared variables (or some signal) that triggered the problem.

by (341 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.