Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | mrspaceblock |
Hello, I can’t get a powerup in my game to work, I know I am doing something wrong, but I also don’t know what I have to do to get it working
Here is my code:
Player:
extends KinematicBody2D
var velocity = Vector2(0,0)
var crystals = 0
const SPEED = 100
const GRAVITY = 15
const JUMPFORCE = -300
func _physics_process(delta):
if Input.is_action_pressed("right"):
velocity.x = SPEED
$Sprite.play("Walk")
$Sprite.flip_h = false
elif Input.is_action_pressed("left"):
velocity.x = -SPEED
$Sprite.play("Walk")
$Sprite.flip_h = true
else:
$Sprite.play("Idle")
if not is_on_floor():
$Sprite.play("Air")
velocity.y = velocity.y + GRAVITY
func _on_speed_body_entered(body):
SPEED == 150
The Powerup:
extends Area2D
signal powerup_collected
func _on_crystal_body_entered(body):
$AnimationPlayer.play("bounce") #plays the bounce animation
emit_signal("coin_collected") #emits the coin_collected signal
set_collision_mask_bit(0,false) #sets the player collision mask to false so that we can't collide with it after we collect it
set_collision_mask_bit(0,false)
$SoundCollect.play() #plays the collection sound
func _on_AnimationPlayer_animation_finished(anim_name):
queue_free() #deletes itself after the bouncing animation finishes
Could someone please explain to me how do I get it to work?
You should provide a bit more context. What is the expected behavior and what’s happening. Also, how are the nodes related, how does the player knows he got a speed boost. Do you receive the signal from the coin?
MrEliptik | 2021-03-23 18:50
I’m trying to make make the player faster when he collects the object
I made a function so that when the player enters the body of the powerup, it changes his speed from 100 to 150, but I think it doesn’t work because SPEED is a const
the name of the powerup in game is just called speed, but SPEED (with all caps) is the constant for the velocity of the player
mrspaceblock | 2021-03-23 19:00
Yeah you’re right! I’ve posted an answer with another error.
MrEliptik | 2021-03-23 20:02