I'm currently working on a game where I want to use a gamepad (in this case an Xbox 360 controller) where I have a square that I want to move using the triggers on the controller. But I have a problem understanding how to use "Joystick".
How I want it to work is that I want the input to happen in a function such as "love.joystickaxis(joystick, axis, value)" and the movement update to happen in "love.update(dt)". What I have right now is that I have variables according to directions that are false in the beginning:
Code: Select all
local isLeftHeld
local isRightHeld
local isUpHeld
local isDownHeld
isLeftHeld = false
isRightHeld = false
isUpHeld = false
isDownHeld = false
Code: Select all
function love.joystickaxis(joystick, axis, value)
local Joysticks = love.joystick.getJoysticks()
for i, joystick in ipairs(Joysticks) do
if joystick:isGamepad() then
local RIGHT = joystick:getGamepadAxis('triggerright')
local LEFT = joystick:getGamepadAxis('triggerleft')
if RIGHT then
print('banana boat going right')
isRightHeld = true
end
if LEFT then
print('banana boat going left')
end
end
end
end
Code: Select all
if isLeftHeld == true then
Tank.Theta = Tank.Theta - Tank.rotationSpeed * dt
elseif isRightHeld == true then
Tank.Theta = Tank.Theta + Tank.rotationSpeed * dt
end
I have isolated that the problem lies in my "if isLeftHeld == true" is the problem by moving the movement code into the "love.joystickaxis" and then it works kinda okay but I don't want the movement to happen in that function.
Why do I get a bunch of prints in the beginning and what am I doing wrong?
Full code
Code: Select all
local isLeftHeld
local isRightHeld
local isUpHeld
local isDownHeld
local isForwardHeld
isLeftHeld = false
isRightHeld = false
isUpHeld = false
isDownHeld = false
isForwardHeld = false
function love.load()
r = 0
Tank = {}
-- Basically a new sprite (48x80)
Tank.Body = love.graphics.newImage("Graphics/tempPlayer.png")
-- Tank Body's position on X/Y
Tank.posX = love.graphics:getWidth() * 0.5
Tank.posY = love.graphics:getHeight() * 0.5
-- Tank Radians (or Rotation)
Tank.Radians = 0
-- Scales up (or down) the size of sprite on X/Y
Tank.scaleFactorX = 1
Tank.scaleFactorY = 1
-- Changes origin from where the Sprite is drawn
Tank.originOffsetX = 0
Tank.originOffsetY = 0
-- Tank's Width/Height
Tank.Width = Tank.Body:getWidth()
Tank.Height = Tank.Body:getHeight()
-- Tank Speed/Velocity
Tank.rotationSpeed = 5
Tank.Velocity = 0
-- Tank Direction
Tank.Direction = 1
Tank.currentDirection = Tank.Direction
Tank.Theta = 4.71 -- Angle / Angle area
end
function love.update(dt)
r = r + 1 * dt
p1Joystick = nil
-- m = math
local mCos = math.cos
local mSin = math.sin
local pos = {}
pos.X = Tank.posX
pos.Y = Tank.posY
pos.Theta = Tank.Theta
if isLeftHeld == true then
Tank.Theta = Tank.Theta - Tank.rotationSpeed * dt
elseif isRightHeld == true then
Tank.Theta = Tank.Theta + Tank.rotationSpeed * dt
end
if isForwardHeld == true then
Tank.Velocity = Tank.Velocity + 5 * dt
end
Tank.posX = Tank.posX + mCos(Tank.Theta) * Tank.Velocity
Tank.posY = Tank.posY + mSin(Tank.Theta) * Tank.Velocity
end
function love.draw()
love.graphics.print("HELLO, WORLD!", 400, 300, r)
love.graphics.draw(Tank.Body,
Tank.posX,
Tank.posY,
Tank.Theta,
Tank.scaleFactorX,
Tank.scaleFactorY,
Tank.originOffsetX,
Tank.originOffsetY)
end
function love.joystickaxis(joystick, axis, value)
local Joysticks = love.joystick.getJoysticks()
for i, joystick in ipairs(Joysticks) do
if joystick:isGamepad() then
local RIGHT = joystick:getGamepadAxis('triggerright')
local LEFT = joystick:getGamepadAxis('triggerleft')
if RIGHT then
print('banana boat going right')
isRightHeld = true
--Tank.Theta = Tank.Theta - Tank.rotationSpeed
end
if LEFT then
print('banana boat going left')
end
end
end
end
function love.gamepadpressed(joystick, button)
-- if joystick:isGamepadDown('triggerleft') then
-- isLeftHeld = true
-- end
end
function love.gamepadreleased(joystick, button)
end
function love.keypressed(key)
if key == 'left' then
isLeftHeld = true
end
if key == 'right' then
isRightHeld = true
end
--[[
if love.keyboard.isDown('left', 'right') then
isForwardHeld = true
end
]]
end
function love.keyreleased(key)
if key == 'left' then
isLeftHeld = false
end
if key == 'right' then
isRightHeld = false
end
end