Hi guys this is my first post on the forums now that I have been developing with Corona for about a month and loving it!
I have learned a great deal so I decided to try my hand at developing a tower defense game. I have a very basic map and have created an enemy tank object that goes through the path at a certain speed that I set. My problem comes when I try to create several instances of the enemy object with delay of about 1 second between each “spawn” and make each still follow the path. I have done a lot of research and am at a loss on how to fix this:( Here is my code with only one enemy tank that goes through the course without fail. Any help would be greatly appreciated!!
Map: The red dots are used as guidance pints for the tanks.
[code]
-----------------------------------------------------------------------------------------
–
– level1.lua
–
-----------------------------------------------------------------------------------------
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
– include Corona’s “physics” library
local physics = require "physics"
physics.start()
--------------------------------------------
– forward declarations and other locals
local screenW, screenH, halfW, halfH = display.contentWidth, display.contentHeight, display.contentWidth0.5, display.contentHeight0.5
-----------------------------------------------------------------------------------------
– BEGINNING OF YOUR IMPLEMENTATION
–
– NOTE: Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.
–
-----------------------------------------------------------------------------------------
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view
pointersGroup = display.newGroup()
mapGroup = display.newGroup()
map = display.newImage( “map.png” )
map:setReferencePoint(display.TopLeftReferencePoint)
map.x = 0
map.y = 0
spawner = display.newImage(“pointer.png”)
spawner:setReferencePoint(display.CenterReferencePoint)
spawner.y = 200
spawner.x = -100
pointer1 = display.newImage(“pointer.png”)
pointer1:setReferencePoint(display.CenterReferencePoint)
pointer1.x = 280
pointer1.y = 200
pointer2 = display.newImage(“pointer.png”)
pointer2:setReferencePoint(display.CenterReferencePoint)
pointer2.x = 280
pointer2.y = 360
pointer3 = display.newImage(“pointer.png”)
pointer3:setReferencePoint(display.CenterReferencePoint)
pointer3.x = 520
pointer3.y = 360
pointer4 = display.newImage(“pointer.png”)
pointer4:setReferencePoint(display.CenterReferencePoint)
pointer4.x = 520
pointer4.y = 120
tank = display.newImage(“tank.png”)
tank:setReferencePoint(display.CenterReferencePoint)
tank.x = spawner.x
tank.y = spawner.y
tank.rotation = 90
– all display objects must be inserted into group
pointersGroup:insert( spawner )
pointersGroup:insert( pointer1 )
pointersGroup:insert( pointer2 )
pointersGroup:insert( pointer3 )
pointersGroup:insert( pointer4 )
mapGroup:insert( map )
mapGroup:insert( tank )
group:insert( mapGroup )
group:insert( pointersGroup )
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
function moveTank ()
local speed = 10
if(tank.x < pointer1.x)then
tank.x = tank.x + speed
end
if(tank.x == pointer1.x and tank.y >= pointer1.y)then
tank.rotation = 180
tank.y = tank.y + speed
end
if(tank.y == pointer2.y and tank.x >= pointer2.x)then
tank.rotation = 90
tank.x = tank.x + speed
end
if(tank.x == pointer3.x and tank.y <= pointer3.y)then
tank.rotation = 0
tank.y = tank.y - speed
end
if(tank.x >= pointer4.x and tank.y == pointer4.y)then
tank.rotation = 90
tank.x = tank.x + speed
end
end
timer.performWithDelay(10, moveTank, 0)
physics.start()
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
physics.stop()
end
– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view
package.loaded[physics] = nil
physics = nil
end
-----------------------------------------------------------------------------------------
– END OF YOUR IMPLEMENTATION
-----------------------------------------------------------------------------------------
– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )
-----------------------------------------------------------------------------------------
return scene [import]uid: 118482 topic_id: 26218 reply_id: 326218[/import]