Help with Enemy Class - noob question.

Hello everyone. First of all I want to thanks in advance for the help you are giving me. I searched a lot to try to fix my problem, so I decided that maybe I should post it here.

I have been creating a 2d scroller and the controllers for the main character are working pretty well.

My problem lies on the enemy class.
When I create an enemy, it stays as a child of the back ground (it follows background.x when I call the function monster:move())

But whenever I create a second or third enemy, only the first enemy to be created follows the background, and the others remain static on the screen.

I think the problem is because of the enemyImage, which is the same for all the enemies I create, right?
Is there a way I can create separated image objects for every new monster I create on my main class? 

 

--Monster Class local monster = {} local monster\_mt = { \_\_index = monster } local main = require("main") function monster.new(x, y) monsterImage = display.newImage("images/monster.png") monsterImage.x = x monsterImage.y = y local newMonster = { x = x or 0, y = y or 0 } return setmetatable( newMonster, monster\_mt) end function monster:move() monsterImage.x = self.x + background.x end return monster

Here is a video explaining my problem.

http://www.youtube.com/watch?v=s8mTD6xs_3M&feature=youtu.be

THANK YOU VERY MUCH.

Greetings from Chile.

Do you know middleclass? Great help when you want decent OOP with Lua. I’d have the following class structure: Actor (something that can move around) and subclassed from that the Player and the Enemy.

Anyways, to your errors.

in monster:new you declare monsterImage as a global. And you need monster:new 

-- Monster Class local monster = {} local monster\_mt = { \_\_index = monster } local main = require("main") function monster:new(x, y) local monsterImage = display.newImage("images/monster.png") monsterImage.x = x monsterImage.y = y local newMonster = { x = x or 0, y = y or 0, monsterImage = monsterImage } return setmetatable(newMonster, monster\_mt) end function monster:move() self.monsterImage.x = self.x + background.x end return monster

For the basics on OOP see here: http://onestepback.org/articles/poly/gp-lua.html

Sir, thank you very much.  :D 

You helped me to understand my mistake and how the self. statements work to their respective objects.

I will check out the middleclass, seems very interesting. Thank you for letting me know.

I will put you on the credits of my game if it ever becomes a succes :stuck_out_tongue:

Thank you again, and best regards.

Lucas Peralta Furet.

You need to be very careful about the scope. Better use the local keyword too often than fuck up you stuff. 

http://lua-users.org/wiki/ScopeTutorial

Do you know middleclass? Great help when you want decent OOP with Lua. I’d have the following class structure: Actor (something that can move around) and subclassed from that the Player and the Enemy.

Anyways, to your errors.

in monster:new you declare monsterImage as a global. And you need monster:new 

-- Monster Class local monster = {} local monster\_mt = { \_\_index = monster } local main = require("main") function monster:new(x, y) local monsterImage = display.newImage("images/monster.png") monsterImage.x = x monsterImage.y = y local newMonster = { x = x or 0, y = y or 0, monsterImage = monsterImage } return setmetatable(newMonster, monster\_mt) end function monster:move() self.monsterImage.x = self.x + background.x end return monster

For the basics on OOP see here: http://onestepback.org/articles/poly/gp-lua.html

Sir, thank you very much.  :D 

You helped me to understand my mistake and how the self. statements work to their respective objects.

I will check out the middleclass, seems very interesting. Thank you for letting me know.

I will put you on the credits of my game if it ever becomes a succes :stuck_out_tongue:

Thank you again, and best regards.

Lucas Peralta Furet.

You need to be very careful about the scope. Better use the local keyword too often than fuck up you stuff. 

http://lua-users.org/wiki/ScopeTutorial