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