Page 1 of 1

Two keys at once

Posted: Thu Dec 10, 2015 3:02 pm
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

Re: Two keys at once

Posted: Thu Dec 10, 2015 6:07 pm
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.

Re: Two keys at once

Posted: Thu Dec 10, 2015 6:12 pm
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

Re: Two keys at once

Posted: Thu Dec 10, 2015 6:23 pm
by FullyLucid
Yup no problem. :P Running windows here, so makes perfect sense.