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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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) 

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

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..

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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) 

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,

?:)

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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()

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

1
require ("myLibray")

@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,

?:)

views:1832 update:2011/10/11 8:57:46
corona forums © 2003-2011