Help with love.mousepressed function and while do loop

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
Glampkoo
Prole
Posts: 3
Joined: Mon Feb 10, 2014 8:44 pm

Help with love.mousepressed function and while do loop

Post by Glampkoo »

I have a little problem with the love.mousepressed function and while do loop.
(it has an error too)

I'm not very experienced with Lua but I did learn some programming languages earlier so I understand most of the things easily.

First, the image doesn't appear when I click somewhere in the game.

Second the character doesn't move slowly when I click where to go (with right click).
I don't know which operator is the "not equal to" so I used !=. I can't find the proper operator to use.

Here's the code of the functions

Code: Select all

function love.load()
   ...
   mark = love.graphics.newImage("textures/mark.png")
   ...
end

Code: Select all

function love.mousepressed(MouseX, MouseY, button)
   if button == "2" then
		love.graphics.draw(mark, MouseX, MouseY)
		while x != MouseX do
			x = x + 1
			player.graphics.draw(playerUp, x, y)
		end
		while y != MouseY do
			y = y + 1
			player.graphics.draw(playerDown, x, y)
		end
   end
end
Hope you have a solution. Thanks.
Attachments
Game.love
It doesn't work
(8.81 KiB) Downloaded 91 times
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Help with love.mousepressed function and while do loop

Post by Azhukar »

This is probably what you wanted.
Attachments
Game2.love
(8 KiB) Downloaded 120 times
User avatar
JovialFeline
Prole
Posts: 28
Joined: Wed Jan 08, 2014 2:32 pm

Re: Help with love.mousepressed function and while do loop

Post by JovialFeline »

The example in love.mousepressed should've hinted at this, but 2 isn't a recognized mouse button. You can see this page for a list of values you can check. In this case, you want r.
Glampkoo wrote:I don't know which operator is the "not equal to" so I used !=. I can't find the proper operator to use.
From the Lua reference manual:
The relational operators in Lua are

== ~= < > <= >=

[...] The operator ~= is exactly the negation of equality (==).
For everything else, see what Azhukar cooked up. ;)
Glampkoo
Prole
Posts: 3
Joined: Mon Feb 10, 2014 8:44 pm

Re: Help with love.mousepressed function and while do loop

Post by Glampkoo »

Ok thanks for the updated .love file!

It gives this error. 'end' expected (to close 'if' at line 37) near '<eof>'

Updated function:

Code: Select all

function love.draw()

...
if posVar == 1 then
	love.graphics.draw(mark, MouseX, MouseY)
	posVar = 0
end
...
Attachments
main.lua
(2.22 KiB) Downloaded 84 times
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Help with love.mousepressed function and while do loop

Post by Azhukar »

Glampkoo wrote:Ok thanks for the updated .love file!

It gives this error. 'end' expected (to close 'if' at line 37) near '<eof>'
You must write "elseif" not "else if", write it without space.
Glampkoo
Prole
Posts: 3
Joined: Mon Feb 10, 2014 8:44 pm

Re: Help with love.mousepressed function and while do loop

Post by Glampkoo »

Azhukar wrote:
Glampkoo wrote:Ok thanks for the updated .love file!

It gives this error. 'end' expected (to close 'if' at line 37) near '<eof>'
You must write "elseif" not "else if", write it without space.
Ok it works now.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests