Trying to make a power up affect player movement...

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
hiccupface
Prole
Posts: 14
Joined: Sat Apr 25, 2015 7:55 pm

Trying to make a power up affect player movement...

Post by hiccupface »

Hi LÖVErs.

I seem to have hit a little roadblock in my game development. Basically, I'm trying to have a power up turn my player's movement into a slightly jittery mess by adding love.math.random() to it's X and Y positions.

When I manually add this to the draw code for the sprite, it works just fine, where the player is constantly jittery, as though it has the coffee shakes. An example of this:

Code: Select all

animation:draw(crusaderBlue.img, crusaderBlue.x+love.math.random(5,10), crusaderBlue.y+love.math.random(5,10), 0, crusaderBlue.scalex, crusaderBlue.scaley)
But when I try to add love.math.random to the X and Y values in the game after colliding with a power up, it only affects the X and Y value for one frame.

I've tried creating a variable named walkmode which I add to the X and Y positions. It starts as 0, and if a powerup is picked up, it is changed to the random number, but still nothing. I'm pretty sure it's because it's not constantly updating love.math.random everyframe.

Anyway, I can't seem to figure this out. Am I even making sense here? I feel the answer should be simple but my brain is starting to brick, haha.
Rickton
Party member
Posts: 128
Joined: Tue Mar 19, 2013 4:59 pm
Contact:

Re: Trying to make a power up affect player movement...

Post by Rickton »

Where/how are you adding the love.math.random to the X and Y after the powerup? It sounds like you're only adding it when the collision happens, which would explain why it's only happening for a single frame. It does need to be added (and changed, if you want it to) every frame.
Possession - Escape from the Nether Regions, my roguelike made in LÖVE for the 2013 7-Day Roguelike Challenge
And its sequel, simply called Possession , which is available on itch.io or Steam, and whose engine I've open-sourced!
hiccupface
Prole
Posts: 14
Joined: Sat Apr 25, 2015 7:55 pm

Re: Trying to make a power up affect player movement...

Post by hiccupface »

Thanks for replying, Rickton.

I went through two attempts. Firstly, if a trigger collision is detected, the player x and y positions will be affected... but only for one frame. Note, this is running in love.update().

Code: Select all

function love.update()
-- detect collision with powerUps and powerUps effects!
function getPowerUps(playerTable, dt)
        for i, newCoffee in ipairs(powerUps) do
        if CheckCollision ((collision check here)) then 
                playerTable.x = playerTable.x+love.math.random(5, 10)
                playerTable.y = playerTable.y+love.math.random(5, 10)
                table.remove(powerUps, i)
        end 
    end    
end 
My other attempt is to create the global walkmode which is added to the player x and y positions in love.draw(). It is initially assigned as 0.

Code: Select all

function love.update()
-- detect collision with powerUps and powerUps effects!
function getPowerUps(playerTable, dt)
        for i, newCoffee in ipairs(powerUps) do
        if CheckCollision ((collision check here)) then 
                walkmode = walkmode + love.math.random(5,10)
        end 
    end    
end 
User avatar
BOT-Brad
Citizen
Posts: 87
Joined: Tue Dec 02, 2014 2:17 pm
Location: England

Re: Trying to make a power up affect player movement...

Post by BOT-Brad »

Are you trying to get the random movement to trigger for a set time, say like 10 seconds once you collect the powerup? If so, something like this in your love.update should work as expected

Code: Select all

function love.update(dt)
    if shake_power_up then -- If the power up is active
        playerTable.x = playerTable.x + love.math.random(5, 10)
        playerTable.y = playerTable.y + love.math.random(5, 10) -- "jitter" the player coords around a bit
        shake_duration = shake_duration - dt -- Reduce the duration by the delta-t
        if shake_duration < 0 then shake_power_up = false end -- If the time is up, then set the shake_power_up to false
    end
end

function getPowerUps(playerTable, dt)
        for i, newCoffee in ipairs(powerUps) do
        if CheckCollision ((collision check here)) then 
                shake_power_up = true -- Set our powerup to true
                shake_duration = 10 --seconds, the duration of the powerup effect
                table.remove(powerUps, i)
        end 
    end
end
Follow me on GitHub! | Send me a friend request on PSN!
hiccupface
Prole
Posts: 14
Joined: Sat Apr 25, 2015 7:55 pm

Re: Trying to make a power up affect player movement...

Post by hiccupface »

Thanks BOT-Brad. It almost works! It doesn't make me jitter but it does is move my character to the left and down until the timer expires. Hm. Almost.
hiccupface
Prole
Posts: 14
Joined: Sat Apr 25, 2015 7:55 pm

Re: Trying to make a power up affect player movement...

Post by hiccupface »

Actually, BOT-Brad, it sort of did work.

I modified the code so the range of random numbers is (-5, 5) instead of (5, 10). This gives it a real solid jittery-ness. I owe you many hearts.

:nyu:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 61 guests