Changes

Jump to navigation Jump to search

Balls Example

84 bytes added, 04:38, 27 August 2013
m
line breaks
Line 1: Line 1:  
Simple lua code showing event handling, class creation, grabbing objects across the screen, cursor setting...<br />
 
Simple lua code showing event handling, class creation, grabbing objects across the screen, cursor setting...<br />
   −
(Code by Adrien "Adriweb" Bertrand)
+
(Code by [[User:Adriweb|Adriweb]] )
      −
<syntaxhighlight>Ball = class()
+
<syntaxhighlight>ballsTable = {}
 +
isGrabbing = false
 +
txt2 = "not moving anything"
 +
 
 +
 
 +
Ball = class()
    
function Ball:init(x,y,r,color)
 
function Ball:init(x,y,r,color)
Line 19: Line 24:  
  gc:fillArc(self.x,self.y,self.r,self.r,0,360)
 
  gc:fillArc(self.x,self.y,self.r,self.r,0,360)
 
  if self.isActive then
 
  if self.isActive then
     gc:setColorRGB(0)  
+
     gc:setColorRGB(0,0,0)  
 
     gc:drawArc(self.x,self.y,self.r,self.r,0,360)
 
     gc:drawArc(self.x,self.y,self.r,self.r,0,360)
 
  end
 
  end
Line 54: Line 59:  
     txt2 = "not moving anything"
 
     txt2 = "not moving anything"
 
     for i,ball in ipairs(ballsTable) do
 
     for i,ball in ipairs(ballsTable) do
 +
      ball.isActive = false
 +
      cursor.set("pointer")
 
       if math.abs(ball.x+ball.r/2-x) <= ball.r/1.8 and math.abs(ball.y+ball.r/2-y) <= ball.r/1.8 then
 
       if math.abs(ball.x+ball.r/2-x) <= ball.r/1.8 and math.abs(ball.y+ball.r/2-y) <= ball.r/1.8 then
 
           ball.isActive = true
 
           ball.isActive = true
 +
          cursor.set("hand pointer")
 
           trackedBall = ball
 
           trackedBall = ball
      else
+
          break
          ball.isActive = false
   
       end
 
       end
 
     end  
 
     end  
Line 73: Line 80:  
  gc:drawString("Press Enter to create a Ball object", 2, 190, "top")
 
  gc:drawString("Press Enter to create a Ball object", 2, 190, "top")
 
end
 
end
  −
ballsTable = {}
  −
grabFlag = false
  −
isGrabbing = false
  −
txt2 = "not moving anything"
      
function on.enterKey()
 
function on.enterKey()
 
  ballsTable[#ballsTable+1] = Ball(math.random(10,250), math.random(10,180), 15, {math.random(0,255),math.random(0,255),math.random(0,255)})
 
  ballsTable[#ballsTable+1] = Ball(math.random(10,250), math.random(10,180), 15, {math.random(0,255),math.random(0,255),math.random(0,255)})
 +
platform.window:invalidate()
 
end</syntaxhighlight>
 
end</syntaxhighlight>

Navigation menu