Adding function to the table element

I am really confused here.
Why I can’t add the function to the table element?
Yo can see the problem in the example below:

local clouds = {}  
  
for i=1,3 do  
 clouds[i] = display.newImage(...etc...)  
 function clouds[i]:moveCloud() self.x = self.x + (self.speed \* self.direction)  
 if self.x \> 640 or self.x \< - self.width then  
 self.x = self.x + (640 \* self.direction)  
 self.speed = math.random(1, 10)  
 end  
 end  
end  

Thanks [import]uid: 101952 topic_id: 31755 reply_id: 331755[/import]

I can’t explain it adequately, I’m afraid, but I do know that you should use this:

From:
[lua] clouds[i] = display.newImage(…etc…)
function clouds[i]:moveCloud()[/lua]

To:
[lua] local cloud = display.newImage(…etc…)
function cloud:moveCloud()

clouds[i] = cloud[/lua]

I think it is because lua does not expect an array reference within a function declaration. [import]uid: 8271 topic_id: 31755 reply_id: 126777[/import]

That’s pretty normal. Since you index inside a table with square brackets, you can’t use colon right after.
That said, you still have a couple of options.

-- inside the for loop  
 clouds[i] = display.newImage(...)  
 clouds[i]['moveCloud'] = function (self)   

or:

-- inside the for loop  
 clouds[i] = display.newImage(...)  
 clouds[i].moveClouds = function (self)  

or :

-- inside the for loop  
 clouds[i] = display.newImage(...)  
 local cloud = clouds[i]  
 function cloud:moveClouds(...)  

The last one is the same as Horace’s snippet. But you do not need the assignment clouds[i]=cloud right after, as local cloud and clouds[i] are both references to the same table.

Hope this helps. [import]uid: 142361 topic_id: 31755 reply_id: 126779[/import]

Wow!

Thank you guys. [import]uid: 101952 topic_id: 31755 reply_id: 126826[/import]

I can’t explain it adequately, I’m afraid, but I do know that you should use this:

From:
[lua] clouds[i] = display.newImage(…etc…)
function clouds[i]:moveCloud()[/lua]

To:
[lua] local cloud = display.newImage(…etc…)
function cloud:moveCloud()

clouds[i] = cloud[/lua]

I think it is because lua does not expect an array reference within a function declaration. [import]uid: 8271 topic_id: 31755 reply_id: 126777[/import]

That’s pretty normal. Since you index inside a table with square brackets, you can’t use colon right after.
That said, you still have a couple of options.

-- inside the for loop  
 clouds[i] = display.newImage(...)  
 clouds[i]['moveCloud'] = function (self)   

or:

-- inside the for loop  
 clouds[i] = display.newImage(...)  
 clouds[i].moveClouds = function (self)  

or :

-- inside the for loop  
 clouds[i] = display.newImage(...)  
 local cloud = clouds[i]  
 function cloud:moveClouds(...)  

The last one is the same as Horace’s snippet. But you do not need the assignment clouds[i]=cloud right after, as local cloud and clouds[i] are both references to the same table.

Hope this helps. [import]uid: 142361 topic_id: 31755 reply_id: 126779[/import]

Wow!

Thank you guys. [import]uid: 101952 topic_id: 31755 reply_id: 126826[/import]