Using states with classes?

Is it possible to use states on classes, so that I can use self in the class function? I can’t seem to make it work.

I guess the psuedo code below explains it a bit better, even though it does not work this way.

-- Set state in the class
self.state = self:move 

-- Call state the main draw function

you might need to call ball:state(), ie use a colon not a dot

Oops, error in the psuedo code, doh. In my actual code I used : when calling.

I just found the error though, it was indeed because of the : and . but not where I called the function but where I set the function I used : instead of .

I guess now I know when to use which, although I’m not exactly clear on the underlying difference between : and .

When you write B = ball(), Codea returns the “address” of the ball it has created and stores it in B. It is just a number, nothing more.

When you then write B:state(), the colon tells Codea to send the address in B as the first parameter, so B:state() is EXACTLY the same as ball.state(B). That is all the colon does, ie pass the address of a particular ball as the first parameter.

Try this


function A:init(a)

function A:mult(b)
    return self.a * b

function setup()
    c = A(5)
    print(c:get(2)) --prints 10
    d = A(7)
    print(d:get(3)) --prints 21

    print(A.get(c,2)) --prints 10
    print(A.get(d,5) --prints 35