Joystick module doesn't work?

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
User avatar
nikneym
Citizen
Posts: 56
Joined: Sat Mar 09, 2013 1:22 pm
Contact:

Joystick module doesn't work?

Post by nikneym »

I got issues about love joystick module. It seems i have connected joystick on pc but button-push checks doesn't work. Can you guys try this code out for me? Joystick works on other stuff but love.

Code: Select all

joysticks=love.joystick.getJoysticks();
joystick=joysticks[1];

player={
	x=100,
	y=100,
	speed=200,
};

function joystickCalistir(dt)
	if joystick:isGamepadDown("dpleft") then
		player.x=player.x-player.speed*dt;
	elseif joystick:isGamepadDown("dpright") then
		player.x=player.x+player.speed*dt;
	end
	
	if joystick:isGamepadDown("dpup") then
		player.y=player.y-player.speed*dt;
	elseif joystick:isGamepadDown("dpdown") then
		player.y=player.y-player.speed*dt;
	end
end

function love.update(dt)
	joystickCalistir(dt);
	connected=joystick:isConnected();
end

function love.draw()
	love.graphics.setBackgroundColor(255, 255, 255);

	love.graphics.setColor(255, 40, 40);

	if connected==true then
		love.graphics.print("Connected.", 100, 10);
	elseif connected==false then
		love.graphics.print("Couldn't connected.", 100, 10);
	end

	love.graphics.print(joystick:getName(), 10, 10);
	love.graphics.rectangle("fill", player.x, player.y, 32, 32);
end
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Joystick module doesn't work?

Post by zorg »

Did you call Joystick:isGamepad to see if it's recognized as an xbox360-like (probably means xinput-compatible, but i'm not sure) gamepad? If that returns false, but Joystick:isConnected is true, then you might have to resort to using the non-gamepad functions (and/or callbacks) instead; or in other words, most functions on the Joystick page that don't have "gamepad" in their names.
Last edited by zorg on Thu Oct 12, 2017 8:01 am, edited 1 time in total.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
nikneym
Citizen
Posts: 56
Joined: Sat Mar 09, 2013 1:22 pm
Contact:

Re: Joystick module doesn't work?

Post by nikneym »

thanks for the reply i'm gonna try it :)
User avatar
nikneym
Citizen
Posts: 56
Joined: Sat Mar 09, 2013 1:22 pm
Contact:

Re: Joystick module doesn't work?

Post by nikneym »

works well with this code. but i'm not sure im doing it right. :p @zorg

Edit: there is a wrong write about horizontal and vertical but it doesn't matter.

Code: Select all

joysticks=love.joystick.getJoysticks();
joystick=joysticks[1];

player={
	x=100,
	y=100,
	speed=200,
};

function joystickCalistir(dt)
	local horizontal=joystick:getAxis(2);
	local vertical=joystick:getAxis(1);
	
	if horizontal==1 then
		player.y=player.y+player.speed*dt;
	elseif horizontal==-1 then
		player.y=player.y-player.speed*dt;
	end
	
	if vertical==1 then
		player.x=player.x+player.speed*dt;
	elseif vertical==-1 then
		player.x=player.x-player.speed*dt;
	end
end

function love.update(dt)
	joystickCalistir(dt);
	connected=joystick:isConnected();
end

function love.draw()
	love.graphics.setBackgroundColor(255, 255, 255);

	love.graphics.setColor(255, 40, 40);	
	
	if connected==true then
		love.graphics.print("Connected.", 100, 10);
	elseif connected==false then
		love.graphics.print("Couldn't connected.", 100, 10);
	end
	
	if joystick:isVibrationSupported()==false then
		love.graphics.print("Your joystick doesn't support vibration.", 200, 10);
	end
	
	--love.graphics.print(joystick:getGUID(), 10, 30);
	love.graphics.print(joystick:getAxis(2), 10, 30);
	
	love.graphics.print(joystick:getName(), 10, 10);
	love.graphics.rectangle("fill", player.x, player.y, 32, 32);
end
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Joystick module doesn't work?

Post by zorg »

Looks like it could work like that :3
Though if you decide to support analog stick movement (as in movement ranging from slow to fast) then you could remove the checks in your joystickCalistir function, and just multiply player.speed with them (assuming the axes' range is between -1 and 1).
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 59 guests