Ok so I’ve spent a crap ton of time with this code today.
I’m completely changing it to something else, but I’m trying to get a concept down and I am not sure what is up.
Here is what I would like to do:
In addition to shooting enemies with the lasers, how would I attach a listener to the enemies so I can remove them via touch?
The reason why I want to know how to do that as I see that method you are using on other samples, and spawning random objects. It’s learning how to get ACCESS to those damn objects that is making me mad.
I know if I have 2 local variables on screen and they both have the same name, and I attach a touch listener to do a display.remove(object) only one will disappear. That leads me to believe that what I am doing won’t work in the way I am trying. Of course, I can’t just remove the group as that removes them all! Arrrrgh.
Hmmmmmmmmmmmmm?
Ok on with the show, so here on on line 153
local function spawnEnemy()
local imageInt = mr(1,4) --\< use one of the 4 images)
local enemy = display.newImageRect("images/ship\_"..imageInt..".png",50,54) --imageInt is replaced with the # of the png, mmmk gotcha.
enemy.x = mr( 20, 300 ); enemy.y = -30; enemy.rotation = 180
enemy.name = "enemy"; physics.addBody( enemy, { isSensor = true } )
enemyGroup:insert( enemy )
I tried to place this at the bottom of code, but seems like I can’t remove enemy, as it’s randomized between the 4 png’s in the images. So I know it’s something like removing a target, or doing some array or index or some other fancy pants term I don’t know that well. I understand YOUR code, but now I’m trying to learn and change things around a bit.
local function removeShip(event)
if event.phase == "began" then
print ("enemy ship touch test")
---display.remove(enemy)
end
end
enemy:addEventListener ("touch", removeShip)
Of course that spits out can’t reference the “enemy” bla bla bla. I thought the enemy was a local variable in the function, but I can’t see it? I even tried putting
local enemy at the top so that it was a local variable.
Yes, I am still new. June 2011, seems a while ago but some of these things throw me off and at times I don’t “get it” haha.
So what you think? Is this a possible thing to do and remove enemies by touch, using your current code or what fancy pants things does a person have to do to get that to work?
-Nick [import]uid: 61600 topic_id: 24801 reply_id: 100657[/import]