Hi!
This is driving me crazy. I have a turret which shoots bullets. I have planes coming across the screen. The bullets is supposed to shoot down the planes, but no collision appears when they hit - they just flies by. Instead, the collision happens when I shoot on the walls of the display!
local function onCollision(self, event)
-- Bullet hit enemy
print("collision")
if (self.name == "bullet" and event.other.name == "plane") then
local explosion = display.newImage( "poof.png", bullet.x, bullet.y )
bullet:removeSelf()
plane:removeSelf()
local function removeExplosion( event )
explosion:removeSelf()
end
timer.performWithDelay( 50, removeExplosion)
end
end
local fire = function()
print("fire")
local bullet = display.newImage("bullet.png")
bullet.x = weapon.x
bullet.y = weapon.y
physics.addBody(bullet, "kinematic", {bounce = 0})
bullet.name = "bullet"
bullet.velocity = {}
bullet.velocity.x = -math.cos( math.rad( weapon.rotation - 90 ) )
bullet.velocity.y = -math.sin( math.rad( weapon.rotation - 90 ) )
bullets[#bullets + 1] = bullet
bullet.collision = onCollision
bullet:addEventListener("collision", bullet)
gameLayer:insert(bullet)
end
local function gameLoop(event)
if event.time - timeLastPlane \>= math.random(4000, 5000) then
require "sprite"
local sheet
sheet = sprite.newSpriteSheet( "plane.png", 225, 125 )
planexscale = 0.3
planeyscale = 0.3
thetime = 10000
pentagonShape = { -3.68085098266602, -13.5531921386719 , 28.3404273986816, -14.0851058959961 , 29.8297882080078, 3.04255294799805 , -11.1276588439941, 14.2127661705017 , -27.6170210838318, 11.021276473999 , -30.8085105419159, -10.7872333526611 }
stringHeight = math.random(20, 190)
local spriteSet = sprite.newSpriteSet(sheet, 1, 4)
sprite.add( spriteSet, "plane", 1, 4, 200, 0 )
local instance = sprite.newSprite( spriteSet )
instance.x = 520
instance.y = stringHeight
instance.xScale = planexscale
instance.yScale = planeyscale
instance:prepare("plane")
instance:play()
instance.name = "plane"
physics.addBody(instance, "kinematic", {bounce = 0, shape=pentagonShape})
transition.to( instance, { time=thetime, alpha=1, x=-420, y=stringHeight, onComplete = function(self) self.parent:remove(self); self = nil; end } )
enemiesLayer:insert(instance)
timeLastPlane = event.time
end
end
local onUpdate = function( event )
for i = 1, #bullets, 1 do
bullets[i]:move( bullets[i].velocity.x \* bulletSpeed, bullets[i].velocity.y \* bulletSpeed )
end
end
local onTouch = function( event )
if event.phase == "began" or event.phase == "moved" then
local angle = math.angleBetween( event, weapon )
weapon.rotation = angle
if (weapon.rotation \> 0) and (weapon.rotation \< 180) then
weapon.xScale = 1
elseif (weapon.rotation \> 180) and (weapon.rotation \< 359) then
weapon.xScale = -1
end
else
fire()
end
end
Runtime:addEventListener("enterFrame", gameLoop)
Runtime:addEventListener( "enterFrame", onUpdate )
Runtime:addEventListener( "touch", onTouch )
Any idea what I’m doing wrong?
Best regards,
joelwe [import]uid: 54640 topic_id: 9885 reply_id: 309885[/import]

