I’m not quite sure what you’re trying to do here. Is there a reason you’re not calling an instance, like object1:draw()? Are you trying to create some kind of generic wrapper class ala EJB or java persistence API?
This is a class, with some methods and properties:
Ball = class()
function Ball:init(x, y, size)
self.x = x
self.y = y
self.size = size
end
function Ball:draw()
fill(255,0,0,255)
noStroke()
ellipseMode(CENTER)
ellipse(self.x, self.y, self.size, self.size)
end
function Ball.bounce()
-- just an example method, this doesn't actually do anything useful
self.y = self.y + 50
end
And this is how I would then use that class in my main file:
function setup()
myBall = Ball(50, 50, 100) -- create a new Ball called 'myBall', at position 50, 50 and with size 100
anotherBall = Ball(100, 100, 75) -- create another Ball called 'anotherBall'
end
function draw()
myBall:draw() -- draw each ball
anotherBall:draw()
end
function touched(touch)
myBall:bounce() -- if the user touches the screen, bounce myBall
end
Wow @Simeon and @frosty , this Works. Thank you! BTW, @Simeon , the New Update is Really awesome. Bought both the Backgrounds and the Editor theme to help you developing!
@frosty - thanks for that example - very helpful!
When I wrote my comp entry used no classes and just very basic functions and lots of nested “if” statements - but I knew classes would have made it much easier but was at a loss as to how to use them. This example has shown me how to define the class and create objects from it - many thanks! now to have a go…
One question for you guys: I guess I could store numerous balls and their respective data in a table and then draw them by cycling through the table with the ipairs function?
If so, how do I store numerous attributes in the table specific to each ball e.g. Ball 1, xpos, ypos, size? And if I had a table which I add data to through the program, how do I use the table.insert function to add the above attributes to the table? (I’ve used it to add a single value, but not multiple values related to a particular object e.g ball 1).
Hope this post makes sense?! Thanks
@Badger You can simply create multiple ball objects, and store them all in a table. Because the balls are objects, they all take care of storing their own attributes. So, if you had a Ball class, like in my earlier post, you could do something like:
function setup()
local ball1 = Ball(50, 50, 100) -- create 3 balls
local ball2 = Ball(300, 300, 100)
local ball3 = Ball(550, 450, 100)
balls = { ball1, ball2, ball3 } -- create a new table containing them
watch("return #balls") -- display the number of balls in the Balls table
end
function draw()
background(41, 41, 41, 255)
for index, ball in ipairs(balls) do
ball.y = ball.y + 1
ball:draw()
-- check if the ball goes offscreen
if ball.y > HEIGHT + (ball.size/2) then
table.remove(balls, index) -- remove the ball at the current index
end
end
end
function touched(touch)
if touch.state == BEGAN then
-- if the user touches the screen, add a new ball at the touch position
table.insert(balls, Ball(touch.x, touch.y, 100))
end
end
I should maybe point out that in the draw() method there, ‘ball’ is different to ‘Ball’ (notice the capital). ‘Ball’ is the Ball class, and is used to create new objects. In the draw method, ‘ball’ is a specific ball object - as we loop through each item in the balls table, ‘ball’ refers to each one in turn.
I’m not sure if I’ve just made that more complicated…