how to make self variable so not all actors die only the one being attacked?

ok day 4 of corona. at the moment i have an enemy that spawns every 3 seconds and comes in from the left side of the screen moving right. if i try to kill the first one by tapping that dies just fine. if i allow more than 1 to spawn on the screen then tapping the first one no longer kills it and it kills whatever else is on the screen.

im not suprised this is happening based on my code. but i dont know how to make a “life” type variable only apply to individual actors. so i can have multiple targets on the screen and only kill the ones im actually tapping on.

heres my code

[lua]local function enemySender ()

enemy1 = display.newImageRect (“images/final-0001.png”, 32, 32)
enemy1.x = -50
enemy1.y = 250

transition.to (enemy1,{ time=6000, x=300})

local function killenemy()
enemy1:removeSelf ()
end

enemy1.isHitTestable = true
enemy1:addEventListener (“tap”, killenemy)

end

timer.performWithDelay(3000,enemySender,0) [/lua]
also how can i randomise that timer a bit? so lets say in addition to enemy1 theres also enemy2 and enemy3 and every 3 seconds the game randomly selects one of those 3 actors to send out. [import]uid: 80874 topic_id: 14558 reply_id: 314558[/import]

i think you need to keep track of your “enemies” in a table and add value to it with every summoned object
something like:
tableWithEnemies = {}

local function spawn_enemies()
local numEnemies = numEnemies+1
tableWithEnemies[numEnemies] = display.newImage(“enemy”
end

its juts out of the head and i didnt test it, but i think its done like that [import]uid: 16142 topic_id: 14558 reply_id: 53853[/import]

would that fit in with my code that i have now or do i have to replace something? sorry for being a complete noob.

also what you just posted. i really have no idea what im saying lol so just tell me if im wrong, but when you say numEnemies+1 am i right in thinking that the table would just become a “counter”? is it able to discriminate against what im actually tapping on if i use that way?

so lets say the enemies spawn in this order enemy 1, enemy 2, and enemy 3. they’re all moving across the screen. but i decide i only want to kill enemy 2 so i tap on just that to kill it. will it work that way? thanks in advance for the help… [import]uid: 80874 topic_id: 14558 reply_id: 53857[/import]

wasnt sure how to implement that bit of code you gave me, but i added local (by accident) infront of the the first enemy1 statement and now its doing what i wanted it to do. [import]uid: 80874 topic_id: 14558 reply_id: 53901[/import]

[blockcode]
local enemy1 = {}
local enemy1Count = 1
local function enemySender ()
enemy1[enemy1Count] = display.newImageRect (“images/final-0001.png”, 32, 32)
enemy1[enemy1Count].x = -50
enemy1[enemy1Count].y = 250
enemy1[enemy1Count].isHitTestable = true
transition.to (enemy1[enemy1Count],{ time=6000, x=300})
end

local function killenemy(event)
if event.phase == “ended” then
event.target:removeSelf ()
end
end

enemy1[enemy1Count]:addEventListener (“touch”, killenemy)
timer.performWithDelay(3000,enemySender,0) [import]uid: 7911 topic_id: 14558 reply_id: 53916[/import]

If you have any experience writing classes or understand abstraction, you can put all your code, etc into one *class* that way when you create an enemy, it can have all the functions associated to do what you are after and will track accordingly.

If you follow the HOWTO on making Function Libraries, you can get a hint on what I am talking

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 14558 reply_id: 53920[/import]

I will be posting a tutorial on spawning in a few days but for now, here is a basic spawning function :

  
local spawnTable = {}  
  
local function spawnObject(params)  
 local self = {}  
  
 self.object = display.newImage(params.image)  
 self.object.x = params.x or 0  
 self.object.y = params.y or 0  
 self.object.tblToInsert = params.tblToInsert or {}  
 self.object.index = #self.object.tblToInsert + 1  
 self.object.myName = params.myName  
  
 self.object.tblToInsert[self.object.index] = self.object  
  
 return self.object  
end  
  
--Spawn 10 objects --Each get inserted into the spawnTable  
for i = 1, 10 do  
 spawnObject(  
 {  
 image = "myImage.png",  
 x = 10 \* i,  
 y = 10 \* i,  
 tblToInsert = spawnTable,  
 myName = "Object" .. i,  
 }  
 )  
  
--Print the names of all objects in the spawntable to prove it worked :P  
for i = 1, 10 do  
 print("spawnTable : " .. i .. " Name = " .. spawnTable.myName)  
end  
  
--Removing an object would go like so (there are countless other ways to do this (even better ones which i will explain in my tutorial)  
local function removeSpawn(event)  
 local target = event.target  
  
 if target.removeSelf then  
 table.remove(target.tblToInsert, target.index)   
 target:removeSelf()  
 target = nil  
 end  
end  
  

[import]uid: 84637 topic_id: 14558 reply_id: 53924[/import]

thats my quick understanding of problem, i think its not good and even not pretty at all, still it works fine
if anyone can, please tell me whats wrong with my function? how can i make it more cleaner and overall better?
[lua]local enemytable = {}
local numEnemies = 0

local function rem(event)
if event.phase == “ended” then
event.target:removeSelf()
end
end

local function enemies()
for i=1, 10 do
enemytable[numEnemies] = display.newRect(0,0,50,50)
enemytable[numEnemies].x = math.random(1,300)
enemytable[numEnemies].y = math.random(1,300)
transition.to(enemytable[numEnemies], {time=1000, x = math.random(1,600), y= math.random(1,800)})
enemytable[numEnemies].name = “enemy”
enemytable[numEnemies]:addEventListener(“touch”, rem)
for i,v in pairs(enemytable[numEnemies]) do
print(i,v)
end
end
end
enemies()[/lua] [import]uid: 16142 topic_id: 14558 reply_id: 53930[/import]

thanks all. will try to implment the different methods. still trying to get my head around using tables.
@jayantv

i read the howto u linked. if you call an external module with just stating

[lua]require (“myLibray”)[/lua]

instead of putting local infront of it, will that run slower than if i did?
@Danny

i will be waiting for that tutorial.
can anyone answer a question amount memory management. i added in a memory monitor to my project to output to the terminal and texture memory atm hovers around 4mb constantly while memusage hovers around 86-88mb. ive been told on a 2g device you might get crashes when texture memory is around 40mb but what about that “other memory usage”? that 86-88 seems high but i only see people worrying about how much texture memory they have left. can someone clarify that for me please. [import]uid: 80874 topic_id: 14558 reply_id: 53974[/import]

@harley,
local or no local is how the variables are stored into memory. On the mobile devices it is suggested that you use local, so that as soon as the scope is over, the memory is freed. However need to test that to confirm, so cannot comment on speed differences at this time.

I will also post a small tute on Memory and what the different types are and do.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 14558 reply_id: 54000[/import]