Hello!
I am making a shooting game that, like most, requires enemies to spawn. What I want is for the enemy to walk to the middle of the screen, stop, and start attacking. I have code that creates an image each time the function is called, but I have no way of differentiating between the different instances. Here is what happens: the level starts…a timer calls a spawn function…the spawn function creates an image and starts its transition…the spawn function calls an attack function which changes the animation and gives me an error. Here is my code:
– Up here, I have code to load audio and sprite data and images.
lvl1 = { enemyIterations = 5 }
enemy = {}
function attack()
enemy[i]:setSequence(“enemyShoot”)
enemy[i]:play()
audio:play(sndEnemy1)
timer.performWithDelay( 1000, idle)
end
function idle()
enemy[i]:setSequence(“enemyIdle”)
enemy[i]:play()
timer.performWithDelay( math.random(700, 1300), attack)
end
function spawnEnemy()
i = i + 1
local enemy[i] = display.newSprite( enemySheet, sequenceDataEnemy )
enemy[i].xScale = 0.7
enemy[i].yScale = 0.7
local enemyX = math.random(1,2)
if(enemyX == 1) then
enemy[i].x = display.contentWidth + 50
enemy[i]:setSequence(“enemyWalkingRight”)
enemy[i].targetX = enemy[i].x - math.random(100, 400)
enemy[i].frame = “walkingRight”
transitionTime = (enemy[i].x-100)/enemy[i].targetX*1000
elseif(enemyX == 2) then
enemy[i].x = -50
enemy[i]:setSequence(“enemyWalkingLeft”)
enemy[i].frame = “walkingLeft”
enemy[i].targetX = enemy[i].x + math.random(100, 400)
transitionTime = enemy[i].targetX/(enemy[i].x+100)*1000
end
enemy[i].y = math.random(200, 300)
enemy[i]:play()
transition.to( enemy, {x = enemy[i].targetX, time = transitionTime, onComplete = idle})
print(“spawn enemy”…i)
end
function startLevel()
timer.performWithDelay( math.random(1000, 2000), spawnEnemy, t.enemyIterations)
end
startLevel(lvl1)
If anyone has help of any kind, it would be much appreciated.