Two keys at once

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
napolux
Prole
Posts: 8
Joined: Wed Dec 02, 2015 7:37 pm

Two keys at once

Post by napolux »

How can I manage two key events at once? My code is this, and the ship gets stuck while moving & shooting at the same time, let's say I want to shoot WHILE moving: https://github.com/napolux/devember/blo ... s/ship.lua

Code: Select all

function Ship:update(dt)
    if love.keyboard.isDown('left','a') then
        if self.data.x >= 10 then
            self.data.x = self.data.x - (self.data.speed*dt)
        end
    elseif love.keyboard.isDown('right','d') then
        if self.data.x <= (love.graphics.getWidth() - self.data.sprite:getWidth() - 10) then
            self.data.x = self.data.x + (self.data.speed*dt)
        end
    end
    if love.keyboard.isDown('up','w') then
        if self.data.y >= 10 then
            self.data.y = self.data.y - (self.data.speed*dt)
        end

    elseif love.keyboard.isDown('down','s') then
        if self.data.y <= (love.graphics.getHeight() - self.data.sprite:getHeight() - 10) then
            self.data.y = self.data.y + (self.data.speed*dt)
        end
    end

    -- Managing shoot timing
    self.data.canShootTimer = self.data.canShootTimer - (1 * dt)
    if self.data.canShootTimer < 0 then
        self.data.canShoot = true
    end

    -- Shoot!
    if love.keyboard.isDown('rctrl', 'lctrl', 'ctrl') and self.data.canShoot then
        -- Create some lasers
        newLaser = { x = self.data.x + (self.data.sprite:getWidth()/2 - (self.data.laser:getWidth()/2)), y = self.data.y, img = self.data.laser }
        table.insert(self.data.lasers, newLaser)
        self.data.canShoot = false
        self.data.canShootTimer = self.data.canShootTimerMax
        
        -- Play sound
        laserSound:play()
    end

    -- update the positions of bullets
    for i, bullet in ipairs(self.data.lasers) do
        bullet.y = bullet.y - (LASER_SPEED * dt)

        if bullet.y < 0 then -- remove bullets when they pass off the screen
            table.remove(self.data.lasers, i)
        end
    end
end
User avatar
FullyLucid
Prole
Posts: 5
Joined: Fri Dec 04, 2015 6:36 pm
Location: Salt Lake City, Utah
Contact:

Re: Two keys at once

Post by FullyLucid »

Can you please either provide the most up to date code, or revert back to the one you have on github? The project seems to work just fine on my computer.
One rogue, one life.
napolux
Prole
Posts: 8
Joined: Wed Dec 02, 2015 7:37 pm

Re: Two keys at once

Post by napolux »

FullyLucid wrote:Can you please either provide the most up to date code, or revert back to the one you have on github? The project seems to work just fine on my computer.
That was the latest code. BTW I probably found the problem. Having CTRL as "shoot" key triggers some MacOS keyboard shortcuts while used with arrows... (i.e. change desktop)

Moved my key to z. Now it works as expected.

Thanks anyway
User avatar
FullyLucid
Prole
Posts: 5
Joined: Fri Dec 04, 2015 6:36 pm
Location: Salt Lake City, Utah
Contact:

Re: Two keys at once

Post by FullyLucid »

Yup no problem. :P Running windows here, so makes perfect sense.
One rogue, one life.
Post Reply

Who is online

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