Hey,
I had to work on some other stuff, so I just recently had time to work on this. :]
I’m very close to it working, but I’ve got a minor problem…
First, here is my code:
[lua]
local function shootBullet()
PlayerBulletCount = PlayerBulletCount + 1
print(“PlayerBulletCount increased by 1”)
local pt = mathapi.rotateAboutPoint( {x=user_turret.x,y=user_turret.y-60}, user_turret, user_turret.rotation - 90 )
PTbullet = loader:createSpriteFromSHDocument(“Weapon5ex”, “GameFX”, “LevelAssets.pshs”);
PTbullet.x = pt.x; PTbullet.y = pt.y;
physics.addBody( PTbullet , “dynamic”, { friction= 0, bounce= 1.0, density= .6, radius= 7, filter = bulletCollisionFilter} );
PTbullet:scale(.6,.6);
PTbullet.class = “bullet”
PTbullet.isBullet = true
PTbullet.rotation = user_turret.rotation + 90
PTbullet:applyForce( pt.x - user_turret.x, pt.y - user_turret.y, user_turret.x, user_turret.y )
local function createBulletEmitters()
local bodyName = “E” … (#BEmitters + 1) – This will concatenate the current number of enemy tanks to the end of the string, resulting in E1 for the first, E2 for the second, etc.
local Emitter = display.newGroup()
Particles.CreateEmitter(bodyName, 0, 0, 0, true, true) – Uses the base name (generated dynamically with each tank)
Emitter.body = Particles.GetEmitter(bodyName)
Particles.CreateParticleType (“BulletSTrail”,
{
imagePath = “gfx/smoke01.png”,
imageWidth = 12,
imageHeight = 12,
directionVariation = 45,
velocityVariation = 50,
rotationVariation = 360,
rotationChange = 30,
useEmitterRotation = false,
alphaStart = 0.5,
fadeInSpeed = 1.5,
fadeOutSpeed = .5,
fadeOutDelay = 100,
scaleStart = 0.5,
scaleVariation = 0.5,
scaleInSpeed = 1.0,
emissionShape = 1,
emissionRadius = 1,
killOutsideScreen = true,
lifeTime = 200,
blendMode = “subtract”,
} )
Particles.AttachParticleType(bodyName , “BulletSTrail”, 32, 99999,0)
local R = 153
local G = 153
local B = 153
Particles.SetParticleProperty(“BulletSTrail”, “colorStart”, {R,G,B})
if BEmitters then
print( "Number of Emitters: " … tostring( #BEmitters ) )
if #BEmitters~=0 then
Particles.SetEmitterTarget( bodyName, PTbullet, true, 0, 0, 0 )
Particles.StartEmitter(bodyName)
end
end
Emitter:insert( Emitter.body )
camera:add(Emitter, 6, false)
table.insert(BEmitters, Emitter) – This adds the tank to the enemyTanks table and increments the #enemyTanks by one
Emitter.body._index = table.indexOf(BEmitters, Emitter)
return Emitter
end
for i=1,5 do
BEmitters[#BEmitters + 1] = createBulletEmitters()
end
timer.performWithDelay(10, createBulletEmitters)
bullets:insert( PTbullet )
PTbullet.collision = onBulletCollision
PTbullet:addEventListener( “collision”, PTbullet )
end
[/lua]
I know these lines are most likely the cause of my troubles:
[lua]
if #BEmitters~=0 then
Particles.SetEmitterTarget( bodyName, PTbullet, true, 0, 0, 0 )
Particles.StartEmitter(bodyName)
end
[/lua]
Obviously it would set every single emitter’s target as the bullet, which is seen when I fire a bullet - all emitters work, but they all get attached to the bullet.
Two questions:
1. How do I attatch just one emitter to the bullet?
2. How would I return an emitter back to the table once the bullet has collided and been removed?
Cheers!
-Saer
Edit: Sorry if the code looks weird; parts of it weren’t rendering correctly. (I am an extreme perfectionist when it comes to ‘tabbing’ my code and making it readable… hopefully it actually is readable 'n such
Edit: actually, I think I’ve solved the issue with multiple emitters being attached to one bullet -
I changed this:
for i=1,5 do BEmitters[#BEmitters + 1] = createBulletEmitters() end
to this:
for i=1,1 do BEmitters[#BEmitters + 1] = createBulletEmitters() end
I still can’t figure out how to remove the emitter though.
I’m guessing I don’t need to “return” an emitter since each time a bullet is fired one is created…