Problem spawning object associated to its spawner

local function spawnEnemy() enemies[#enemies + 1] = display.newImage("images/enemy1.png")         enemy = enemies[#enemies];         enemy:setReferencePoint(display.CenterReferencePoint);         enemy.x, enemy.y = random(\_W + 200, \_W\*2), ground.y - ground.height\*0.5;         enemy.hPoints = 5; --health points         enemy.scorePoints = 100;         enemy.name = "enemy";         physics.addBody(enemy);         enemy:setSequence("walking");         enemy:play();         localGroup:insert(enemy);    function spawnAttack()                            attackTable[#attackTable + 1] = display.newImage("images/attack.png");                attack = attackTable[#attackTable];                attack:setReferencePoint(display.CenterReferencePoint);                attack.x, attack.y = player.x + 100, enemy.y;                physics.addBody(attack);                attack.name = "attack";                localGroup:insert(attack);                local playerXDist = player.x - attack.x;                attack:setLinearVelocity(-playerXDist/2, 0);    end         enemy.trans = transition.to(enemy, {time = 5000, x = player.x + 100, onComplete = spawnAttack}) end  

Then, after creating the spawnEnemy function, I set up a timer like this:

timer.performWithDelay(3000, spawnEnemy, 2);  

Results: the first enemy comes to the position player.x + 100, and stops, but DO NOT toss an “attack” object as it’s supposed to do; the second enemy comes to the position, and DO toss an “attack” object.

So… what’s wrong with my code, folks?

Thank you.

your “enemy” variable is taking the last created enemies[#enemies + 1] object from the time the spawnEnemy function is being called. If you were to extend the time to something above 5000 ms, it should work correctly.

timer.performWithDelay(7000, spawnEnemy, 2);

Wow. It worked. :stuck_out_tongue: Thank you, dude.

Another doubt: what if I set up more kinds of enemies and want to make the function above work in less time?

Do you understand what I’m talking about?

Have you tried changing the “enemy” reference in your transition.to function to the enemies[#enemies + 1]? The enemy variable seems to be what is holding up your logic.

Yes, I’ve tried that.

When I try that, the error “attempt to index field ‘?’ (a nil value)” occurs.

I spawn my enemies based on

allDisks[#allDisks + 1] = display.newImage( randImage ) local disk = allDisks[#allDisks]  

You can find that in the MultiPuck sample.

you should take that out and throw a local ahead of the enemy object. See below:

local enemies = display.newImage("images/enemy1.png")

And use the enemies variable with all transitions functions.

“enemy” is a local variable I call at the top of my code to receive “enemies[#enemies]”, as well as I call “enemies” as a table to receive the image. It’s like the MultiPuck sample.

Do you think is it worth trying your idea?

Yea, as long as you don’t need a reference to your enemy objects later on you should be good just making the enemy variable local to the function. If you do need a reference outside of the function, there are better ways to achieve that. Do you need a reference outside of the function, or are you just testing spawning code?

Yes, I need to reference my enemies outside the function.

I’m having troubles on detecting whether each enemy is hit by my player and on removing the enemy which is hit…

How can I make a right reference to an enemy and verify whether it’s hit or not, then remove only it? *

*I want to remove its transition too.

I have a set of tutorials explaining this exact process:

http://www.panc.co/tutorials.html

/shameless self-promotion

There is no end to the tutorials that exist for Corona SDK/Box2D collision management. Google will yield a cornucopia of info for you.

Thank you for your tutorials, dude. I’ve learned a lot by reading them. Nononetheless, how can I use the references you use for uncountable enemies as well as in the function I created? I want the enemies to be created indefinitely using a timer.

Do you get it?

I have to be honest, I’m having trouble following your logic without code examples. However, I will try to interpret your issue without examples.

I think you want to create unlimited enemies and have them move through your defined logic, while still being able to reference the enemies outside of the spawning function. If this is the case, using a “for” loop is probably going to be your best bet. If you take a look at my tutorial at panc.co you’ll see how you can spawn objects and assign identifiers to them. You can create a large amount of enemies off-screen and just run your transitions based on their identifiers.

your “enemy” variable is taking the last created enemies[#enemies + 1] object from the time the spawnEnemy function is being called. If you were to extend the time to something above 5000 ms, it should work correctly.

timer.performWithDelay(7000, spawnEnemy, 2);

Wow. It worked. :stuck_out_tongue: Thank you, dude.

Another doubt: what if I set up more kinds of enemies and want to make the function above work in less time?

Do you understand what I’m talking about?

Have you tried changing the “enemy” reference in your transition.to function to the enemies[#enemies + 1]? The enemy variable seems to be what is holding up your logic.

Yes, I’ve tried that.

When I try that, the error “attempt to index field ‘?’ (a nil value)” occurs.

I spawn my enemies based on

allDisks[#allDisks + 1] = display.newImage( randImage ) local disk = allDisks[#allDisks]  

You can find that in the MultiPuck sample.

you should take that out and throw a local ahead of the enemy object. See below:

local enemies = display.newImage("images/enemy1.png")

And use the enemies variable with all transitions functions.

“enemy” is a local variable I call at the top of my code to receive “enemies[#enemies]”, as well as I call “enemies” as a table to receive the image. It’s like the MultiPuck sample.

Do you think is it worth trying your idea?

Yea, as long as you don’t need a reference to your enemy objects later on you should be good just making the enemy variable local to the function. If you do need a reference outside of the function, there are better ways to achieve that. Do you need a reference outside of the function, or are you just testing spawning code?

Yes, I need to reference my enemies outside the function.

I’m having troubles on detecting whether each enemy is hit by my player and on removing the enemy which is hit…

How can I make a right reference to an enemy and verify whether it’s hit or not, then remove only it? *

*I want to remove its transition too.