Problem with a class

Hi !

I have a problem with class…

I have a zombie class that is called when I create a physical body as shown called zombie when zombie is located between the variable laax I apply force, the problem is that if I call two or more zombies only apply force to the last! why?

the code below…

 Zombie.ZombieMovement(laax)  
  
 local zombie1 = Zombie.new(lol.x, lol.y, ZombiePosition)  
 game:insert(zombie1)  
  
  
module(..., package.seeall)  
  
function Zombie(x, y, ZombiePosition)  
  
  
 position = ZombiePosition  
  
  
 local AnimZombie = graphics.newImageSheet( "images/zombie.png", { width=71, height=83, numFrames=10 } )  
  
 local Zombie = display.newSprite( AnimZombie, { name="zombie", start=1, count=10, time=850 } )  
 Zombie.x = x  
 Zombie.y = y  
 Zombie.type = "kill"  
 Zombie.isAlive = true  
 physics.addBody( Zombie, "dynamic", { friction=0, density=3, bounce=0} )  
 Zombie:play()  
  
 function ZombieMovement(laax)  
  
  
 if(Zombie and Zombie.isAlive == true )then  
  
 if(Zombie.x \< laax + 9 and Zombie.x \> laax - 9 )then  
  
 Zombie:applyForce(0, -100, Zombie.x, Zombie.y )  
  
 end   
 end  
  
 end  
  
  
  
 return Zombie  
  
end  
  
function new(x, y, ZombiePosition)  
 return Zombie(x, y, ZombiePosition)  
end  

Thanks to all !!! [import]uid: 47941 topic_id: 34067 reply_id: 334067[/import]

The problem is when you call zombie1 the first time it’s all good but when you call it again now zombie1 points to the second zombie and has no reference to the first anymore. Try making zombie a table

Zombies ={}
Then when you create the zombies use zombies[1], zombies[2], … [import]uid: 7911 topic_id: 34067 reply_id: 135452[/import]

Thanks jstrahan , but i tried so but same problem…

[code]
zombie = {}

for i = 1, 3 do
aasd = aasd + 150
zombie[i] = Zombie.new(lol.x + 400, lol.y, ZombiePosition)
game:insert(zombie[i])
end

[/code] [import]uid: 47941 topic_id: 34067 reply_id: 135454[/import]

I solved it!

now I have another problem I want to make sure that you can layer two zombies without one pushes the other zombie, and I did it this way:

but sometimes it returns this error “attempt to index field contact” why?

[code]
Zombie.preCollision = function(self, event)

if(event.other.type == “killm”)then

event.contact.isEnabled = false

end

end

Zombie:addEventListener( “preCollision”, Zombie )

[/code] [import]uid: 47941 topic_id: 34067 reply_id: 135485[/import]

The problem is when you call zombie1 the first time it’s all good but when you call it again now zombie1 points to the second zombie and has no reference to the first anymore. Try making zombie a table

Zombies ={}
Then when you create the zombies use zombies[1], zombies[2], … [import]uid: 7911 topic_id: 34067 reply_id: 135452[/import]

Thanks jstrahan , but i tried so but same problem…

[code]
zombie = {}

for i = 1, 3 do
aasd = aasd + 150
zombie[i] = Zombie.new(lol.x + 400, lol.y, ZombiePosition)
game:insert(zombie[i])
end

[/code] [import]uid: 47941 topic_id: 34067 reply_id: 135454[/import]

I solved it!

now I have another problem I want to make sure that you can layer two zombies without one pushes the other zombie, and I did it this way:

but sometimes it returns this error “attempt to index field contact” why?

[code]
Zombie.preCollision = function(self, event)

if(event.other.type == “killm”)then

event.contact.isEnabled = false

end

end

Zombie:addEventListener( “preCollision”, Zombie )

[/code] [import]uid: 47941 topic_id: 34067 reply_id: 135485[/import]