Is this how I'd use classes?

myBall = Class(100, 5)

function myBall:init(num1, num2)
self.Hitpoints = num1
self.Speed = num2
end

print(myBall.Hitpoints)

And when is myBall:draw() executed? And I’ve also seen class:touched()

@Coder1500 You’re off to a good start, but you still have a lot to learn. myBall:draw() and myBall:touched() would be executed when you call them from the functions draw() and touched().

These may help

https://coolcodea.wordpress.com/2014/10/01/169-why-tables-and-classes-are-so-useful/

https://coolcodea.wordpress.com/2013/06/14/84-a-practical-example-showing-the-value-of-classes/

@Coder1500 Here’s a corrected version.

function setup()
    ball = myBall(100, 5)
end

function draw()
    background(40, 40, 50)
    ball:draw()
end

function touched(t)
    if t.state==BEGAN then
        ball:touched(t)
    end
end

myBall=class()

function myBall:init(num1, num2)
    self.Hitpoints = num1
    self.Speed = num2
end

function myBall:draw()
        
end

function myBall:touched(t)
    print(self.Hitpoints)
    print(self.Speed)
end

Thanks guys. Hey Dave, I have a question. Why did you define a variable in the setup function and then turn it into a class later on? Is this required to do? or… Idk. Some explanation here would be most appreciated!

@Coder1500 - you need to read up about Lua classes. There are plenty of explanations on the net.

In the index to my blog (see posts above) you’ll also find a link to an ebook I wrote on Lua for Codea, which covers this.

@Coder1500 I didn’t turn a variable into a class. I created a class that’s called myBall using the statement myBall=class(). Following that I created functions for that class. The statement ball=myBall(100,5) creates an instance of the class using the variable ball. You then use ball to access the class functions. You can create as many instances of the class as you want and each one will be unique to itself. You can get a better explanation by reading Ignatz tutorials.

@dave1707 because we already speak about classes, I seen some classes

CLASS = class(ANOTHER_CLASS)

Where can I read about this?

@TokOut that’s how Codea’s class system implements “Class Inheritance”. Ignatz has some good blog posts on that, if you google his site for “class inheritance” you’ll find them.

Thanks all!