Jump to content




[Lua] Detection of click algorithm bug


  • You cannot reply to this topic
8 replies to this topic

#1 bjornir90

  • Members
  • 378 posts
  • LocationFrance

Posted 27 November 2012 - 05:31 AM

Hello everyone !
I have tried to do a mouse/graphic API but I still blocked at the click detection algorithm. Here the code I use :
function buttonClick(x, y, a, B)/>/> 
 clic, x1, y1 = os.pullEvent("mouse_click") 
 for b=b, 1 do 
  if x1<= a-b then 
   for a=a, 1 do 
    if y1 <= b-a then 
     test = "true" 
    else 
      a = a-1 
     end 
    end 
   else 
    b = b-1 
   end 
  end 
 if test == "true" then 
  for y=y, 1 do 
   if x1 >= x-y then 
    for x=x, 1 do 
     if y1 >= y-x then 
      clicked = "true" 
      return clicked 
     else 
      x = x+1 
     end 
    end 
   else 
    y = y+1 
   end 
  end 
 end 
When I click on the button, it doesn't detect anything.

#2 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 27 November 2012 - 05:40 AM

when you do "for var=number over max,max do"
it will not run anything after do

#3 bjornir90

  • Members
  • 378 posts
  • LocationFrance

Posted 27 November 2012 - 05:42 AM

View PostPixelToast, on 27 November 2012 - 05:40 AM, said:

when you do "for var=number over max,max do"
it will not run anything after do
Why ? It's not the use of the for loops ?

#4 bjornir90

  • Members
  • 378 posts
  • LocationFrance

Posted 27 November 2012 - 05:54 AM

Well I found the error thanks you!

#5 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 27 November 2012 - 06:51 AM

View Postbjornir90, on 27 November 2012 - 05:42 AM, said:

View PostPixelToast, on 27 November 2012 - 05:40 AM, said:

when you do "for var=number over max,max do" it will not run anything after do
Why ? It's not the use of the for loops ?
yea, for loops wont run entirely if the min is greater than the max
same thing for string.sub
string.sub("hello",3,2) will return a empty string

#6 bjornir90

  • Members
  • 378 posts
  • LocationFrance

Posted 27 November 2012 - 07:05 AM

View PostPixelToast, on 27 November 2012 - 06:51 AM, said:

View Postbjornir90, on 27 November 2012 - 05:42 AM, said:

View PostPixelToast, on 27 November 2012 - 05:40 AM, said:

when you do "for var=number over max,max do" it will not run anything after do
Why ? It's not the use of the for loops ?
yea, for loops wont run entirely if the min is greater than the max
same thing for string.sub
string.sub("hello",3,2) will return a empty string
Yeah, how can I have even try to do this ? I'm so idiot !

#7 GopherAtl

  • Members
  • 888 posts

Posted 27 November 2012 - 08:05 AM

if you actually wanted to iterate the other way, say from 10 down to 1, you could use the optional third value for for, which is the step


--countdown

for i=10,1,-1 do

  print(i.."...")

end

print("boom")



You may have found this already, but it should be noted that where your for loops reuse variable names that exist outside the loops, the behavior may not be what you would expect, ex..



local i=1

for i=i,10 do --this WILL loop from 1 to 10...

  ...

end

--i will have reverted, as the loop was using a new, local variable "i" which ceased to exist when you exited the loop

print(i) -- will print "1"!



Also note, modifying the loop variable inside the loop has NO effect at all. This code will print all the numbers from 1 to 10, the i=i+1 inside the loop will not affect the next pass through the loop's initial value of i

for i=1,10 do

  print(i)

  i=i+1

end




#8 bjornir90

  • Members
  • 378 posts
  • LocationFrance

Posted 27 November 2012 - 08:30 AM

View PostGopherAtl, on 27 November 2012 - 08:05 AM, said:

if you actually wanted to iterate the other way, say from 10 down to 1, you could use the optional third value for for, which is the step


--countdown

for i=10,1,-1 do

  print(i.."...")

end

print("boom")



You may have found this already, but it should be noted that where your for loops reuse variable names that exist outside the loops, the behavior may not be what you would expect, ex..



local i=1

for i=i,10 do --this WILL loop from 1 to 10...

  ...

end

--i will have reverted, as the loop was using a new, local variable "i" which ceased to exist when you exited the loop

print(i) -- will print "1"!



Also note, modifying the loop variable inside the loop has NO effect at all. This code will print all the numbers from 1 to 10, the i=i+1 inside the loop will not affect the next pass through the loop's initial value of i

for i=1,10 do

  print(i)

  i=i+1

end

Thanks you alot ! I did not even heard about the third variable in a for loop XD And about the i=i+1 thing it's because I need this value updated in the rest of my program. (I didn't post the code entirely because you can find it in the API subforum.)

#9 GopherAtl

  • Members
  • 888 posts

Posted 27 November 2012 - 08:55 AM

That's my point, looping with a variable x makes any other variable x outside the loop completely inaccessible to the code inside the loop. You'd need to use different variable names for the loop for this to work.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users