Difference between revisions of "Balls Example"

From Inspired-Lua Wiki
Jump to navigation Jump to search
(Created page with " Simple lua code showing event handling, class creation, grabbing objects across the screen, cursor setting... (Code by Adrien "Adriweb" Bertrand) <syntaxhighlight&gt...")
 
Line 1: Line 1:
&nbsp;Simple lua code showing event handling, class creation, grabbing objects across the screen, cursor setting...
+
Simple lua code showing event handling, class creation, grabbing objects across the screen, cursor setting...<br />
  
 
(Code by Adrien "Adriweb" Bertrand)
 
(Code by Adrien "Adriweb" Bertrand)
  
  
 
+
<syntaxhighlight>Ball = class()
&lt;syntaxhighlight&gt;Ball = class()
 
  
 
function Ball:init(x,y,r,color)<br> self.x = x<br> self.y = y<br> self.r = r<br> self.color = color<br> self.isActive = false<br> self.id = #ballsTable+1<br>end
 
function Ball:init(x,y,r,color)<br> self.x = x<br> self.y = y<br> self.r = r<br> self.color = color<br> self.isActive = false<br> self.id = #ballsTable+1<br>end
Line 23: Line 22:
 
function on.paint(gc)<br> for _,ball in ipairs(ballsTable) do<br> ball:paint(gc)<br> end<br> gc:setColorRGB(0)<br> gc:drawString("grabbing : " .. tostring(isGrabbing),2,0,"top")<br> gc:drawString(tostring(txt2),2,20,"top")<br> gc:drawString("Press Enter to create a Ball object", 2, 190, "top")<br>end
 
function on.paint(gc)<br> for _,ball in ipairs(ballsTable) do<br> ball:paint(gc)<br> end<br> gc:setColorRGB(0)<br> gc:drawString("grabbing : " .. tostring(isGrabbing),2,0,"top")<br> gc:drawString(tostring(txt2),2,20,"top")<br> gc:drawString("Press Enter to create a Ball object", 2, 190, "top")<br>end
  
ballsTable = {}<br>grabFlag = false<br>isGrabbing = false<br>txt2 = "not moving anything"<br> <br>function on.enterKey()<br> 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)})<br>end<br>&lt;/syntaxhighlight&gt;<br>
+
ballsTable = {}<br>grabFlag = false<br>isGrabbing = false<br>txt2 = "not moving anything"<br> <br>function on.enterKey()<br> 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)})<br>end<br></syntaxhighlight>

Revision as of 12:19, 17 June 2012

Simple lua code showing event handling, class creation, grabbing objects across the screen, cursor setting...

(Code by Adrien "Adriweb" Bertrand)


Ball = class()

function Ball:init(x,y,r,color)<br> self.x = x<br> self.y = y<br> self.r = r<br> self.color = color<br> self.isActive = false<br> self.id = #ballsTable+1<br>end

function Ball:paint(gc)<br> gc:setColorRGB(unpack(self.color))<br> gc:fillArc(self.x,self.y,self.r,self.r,0,360)<br> if self.isActive then<br> gc:setColorRGB(0)<br> gc:drawArc(self.x,self.y,self.r,self.r,0,360)<br> end<br>end

function Ball:move(x,y)<br> self.x = x<br> self.y = y<br> platform.window:invalidate()<br>end

<br>function on.grabDown(x,y)<br> isGrabbing = not isGrabbing<br> platform.window:invalidate()<br>end

function on.mouseUp(x,y)<br> isGrabbing = false<br> platform.window:invalidate()<br>end

function on.mouseDown(x,y)<br> isGrabbing = true<br> platform.window:invalidate()<br>end

function on.mouseMove(x,y)<br> if isGrabbing and (trackedBall and trackedBall.isActive) then<br> txt2 = "moving ball #"..trackedBall.id<br> trackedBall:move(x-trackedBall.r/2,y-trackedBall.r/2)<br> else<br> txt2 = "not moving anything"<br> for i,ball in ipairs(ballsTable) do<br> if math.abs(ball.x+ball.r/2-x) &lt;= ball.r/1.8 and math.abs(ball.y+ball.r/2-y) &lt;= ball.r/1.8 then<br> ball.isActive = true<br> trackedBall = ball<br> else<br> ball.isActive = false<br> end<br> end<br> end<br>end

function on.paint(gc)<br> for _,ball in ipairs(ballsTable) do<br> ball:paint(gc)<br> end<br> gc:setColorRGB(0)<br> gc:drawString("grabbing : " .. tostring(isGrabbing),2,0,"top")<br> gc:drawString(tostring(txt2),2,20,"top")<br> gc:drawString("Press Enter to create a Ball object", 2, 190, "top")<br>end

ballsTable = {}<br>grabFlag = false<br>isGrabbing = false<br>txt2 = "not moving anything"<br> <br>function on.enterKey()<br> 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)})<br>end<br>