Trying to get some basic functionality with a ‘space invaders’ type simulation. I have three questions about my code:
-
How do I make multiple enemies, and determine which one got hit so the program knows which one to delete? In essence trying to combine the Polylines and Collision Detection tutorials. Make multiple, then when one gets shot, it gets deleted.
-
In my very simple code, you tap the screen once, it fires a shot, hits the target, and the bullet and target disappear. Then the next shot seems to shoot backwards. It only does this after the target is hit (for example, comment out the target, and it does not do this). The rest of the shots are fine. Why does it do this?
-
I am normally a PHP/MySQL app developer, games are a new kind of logic to me, and I have about four hours experience with Lua/Corona. Am I working this out correctly? Logically? Code? Any suggestions so far?
[code]
local physics = require( “physics” )
physics.start()
physics.setGravity( 0, 0 )
display.setStatusBar( display.HiddenStatusBar )
local screenW, screenH = display.contentWidth, display.contentHeight
local enemy = {}
enemy[1] = display.newCircle( screenW / 2, screenH * 0.1, 20 )
enemy[1]:setFillColor(0, 255, 0)
physics.addBody( enemy[1], “static”, { density=3.0, friction=0.5, bounce=0.3 } )
local myShip = display.newRect(0,0,40,20)
myShip.x = screenW / 2
myShip.y = screenH - 30
physics.addBody( myShip, “static”, { density=3.0, friction=0.5, bounce=0.3 } )
local tapToShoot = function( event )
if event.phase == “ended” then
print( “Screen tapped, shoot!” )
bullet = display.newCircle( screenW / 2 ,screenH - 30,5)
physics.addBody( bullet, { density=3.0, friction=0.5, bounce=0.05 } )
bullet.isBullet = true
bullet:applyForce( 0, -300, bullet.x, bullet.y )
else
–do nothing
end
return true
end
Runtime:addEventListener( “touch”, tapToShoot )
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
print( “hit” )
enemy[1]:removeSelf()
bullet:removeSelf()
else
–do nothing
end
end
enemy[1].collision = onLocalCollision
enemy[1]:addEventListener( “collision”, enemy[1] )
[/code] [import]uid: 13726 topic_id: 4942 reply_id: 304942[/import]
lol Good luck. [import]uid: 11860 topic_id: 4942 reply_id: 16041[/import]