0 votes

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?

Godot version v3.2.3.stable.official
in Engine by (20 points)

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?

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

Yeah you're right! I've posted an answer with another error.

1 Answer

0 votes

I'm seeing two problems here:

  1. SPEED is a const which mean it can't be modified.
  2. In func _on_speed_body_entered(body) you're doing a comparison with == instead of an assignment with =

If you correct those two points, it should work better.

Just in case you missed it though, with the code you're showing right now, the player won't move unless they press something. It might be okay for you depending on your type of boost. Also, you don't have code to revert the speed back. If you didn't do it, remember to add a timer or something.

by (551 points)

Thank you, what I did was add a variable called power, which by default is set to one, then I made the SPEED be multiplied by "power", then when I collect the powerup, it changes "power" to 2 which changes the player's speed, then I added a timer so that the player is only faster for a small amount of time

Seems good! Glad it worked.

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 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 webmaster@godotengine.org with your username.

Categories