can someone explain difference between . and :

I am new to corona sdk, can someone explain the difference
between . and :

I am a bit confused when to use . and when to use :
i.e is the . only used for attributes and : with methods ? or can they both be used interchangeably?

thanks [import]uid: 61084 topic_id: 11658 reply_id: 311658[/import]

The : is just a shortcut for including the calling table as the first parameter, so:

object:removeSelf()  

Is the same as:

object.removeSelf(object)  

-Angelo

[import]uid: 12822 topic_id: 11658 reply_id: 42385[/import]

Short Answer:

self:myFunction()

is the short form of

myClass.myFunction(self)
Long Answer:

When dealing with objects, you need something to tell you which object your are referring to (A pointer). In languages like C++ that is called ‘this’. In Lua, it is called ‘self’. Lets say you had a Box Class, and a couple of methods. (Lua is flexible, and you can code in different styles, this is just to convey the idea.)

Class Box = {}

function Box.create(x,y)
local self = {}
self.x = x
self.y = y
.
.
.

return self
end

function Box.update(self, deltaX, deltaY)
self.x = self.x + deltaX
self.y = self.y + deltaY
end

To create 2 boxes you would:

box1 = Box.create()
box2 = Box.create()

now to update, you can

Box.update(box1,1,0)
Box.update(box2,0,1)
or
box1:update(1,0)
box2:update(0,1)

It is just a syntax shorthand, (Lua calls it syntactic sugar) the pointer is still being passed, you just do not see it.
[import]uid: 41667 topic_id: 11658 reply_id: 42425[/import]