I am creating the following object orgMonst, when it’s hit in a collision the object is destroyed and recreated with the same name with different image and physics but I want to create more than one orgMonst in the beginning (random number) and once the collision happens delete only the one involved in it.
Here is the code for the original object, the collision function and the function to swap the object upon collision
[lua]–CREATE MONSTER
local physicsData = (require “gamePhysics”).physicsData(0.333)
enemy = display.newImage(“monst2.png”)
enemy.x = 250 enemy.y = 200
physics.addBody(enemy, physicsData:get(“2bMonst”))
enemy.myName = “enemy”
enemy.bodyType = “static”
enemy.isFixedRotation = true
local swapEnem = function()
physicsData = (require “gamePhysics”).physicsData(0.333)
enemy = display.newImage(“monst1.png”)
enemy.x = tempX enemy.y = tempY
physics.addBody(enemy, physicsData:get(“1bMonst”))
enemy.myName = “newEnem”
enemy.bodyType = “static”
enemy.isFixedRotation = true
print (“ORIGINAL MONSTER REMOVED NEW ONE CREATED WITH SAME OBJECT NAME: enemy”)
end
local function playerCollision( self, event )
– player hits and removes enemy
if ( self.myName == “player” and event.other.myName == “enemy” ) then
if (( event.selfElement == 3 and event.otherElement == 1 ) or ( event.selfElement == 3 and event.otherElement == 2 )) then
tempX = enemy.x; tempY = enemy.y
enemy:removeSelf()
timer.performWithDelay(33, swapEnem, 1)
end
end
end [import]uid: 43696 topic_id: 10665 reply_id: 310665[/import]