Projectile Help

Hello guys im hoping somebody may be able to help with my problem.

I am creating a platform ninja game and I am currently trying to implement a throwing star. I have manged to get the star animated and to be created in front of the enemy who throws it. I have have also added force to the star which now hits my main player but the star is being spawned to many times, I need to limit the amount of stars the enemy can throw, any suggestions?

Kane Lakin

[code]
function enemyThrow()
local distance = math.abs( character.x - enemy.x)
if (distance > 50) then
ninjaThrow= movieclip.newAnim{“images/projectileA.png”,“images/projectileB.png”}
ninjaThrow.x = enemy.x
ninjaThrow.y = enemy.y
physics.addBody(ninjaThrow)
ninjaThrow:applyForce( 10, 0, ninjaThrow.x, ninjaThrow.y )
ninjaThrow:play()

end
end
[/code] [import]uid: 217352 topic_id: 35808 reply_id: 335808[/import]

Hello Kane,
Is this function is a touch listener? If so, you should filter the responses by the phase, as in “began”, but -not- “ended” or “moved”. If I see the line that is calling (or setting this function as a touch listener), I can provide a clearer answer.

Thanks,
Brent Sorrentino [import]uid: 200026 topic_id: 35808 reply_id: 142410[/import]

Hi Brent

No this is not a touch listener, when the character (you) come within a certain distance of the enemy’s x position then the enemy will begin to fire the throwing stars, and as this is not a touch listener the stars are spawned continuously.

Does that sense

Kane [import]uid: 217352 topic_id: 35808 reply_id: 142411[/import]

I’ve tried adding these commands to see if i can control the amount of stars being spawned but its is not helping might give you a clearer picture though

Kane

[code]
function enemyThrow()
local distance = math.abs( character.x - enemy.x)
if (distance > 50) then
ninjaThrow= movieclip.newAnim{“images/projectileA.png”,“images/projectileB.png”}
ninjaThrow.x = enemy.x + enemy.x
ninjaThrow.y = enemy.y
physics.addBody(ninjaThrow)
ninjaThrow:applyForce( 10, 0, ninjaThrow.x, ninjaThrow.y )
ninjaThrow:play()
timer.performWithDelay( 10000, enemyThrow, 0)

elseif (distance > 60) then
ninjaThrow:removeSelf()

end
end
[/code] [import]uid: 217352 topic_id: 35808 reply_id: 142415[/import]

Hi Kane,
So this runs on a Runtime listener, listening to the position of the player versus the enemy? If so, I suggest you use a repeating timer to control how often the stars are thrown, starting the timer when the player enters the “range” and canceling it when the player is out of range (actually, you would need to instantly fire the first star when the range is breached, to throw that first star, then you begin the timer for successive stars).

Brent [import]uid: 200026 topic_id: 35808 reply_id: 142436[/import]

Yeah that’s right would you have an example of how i could implement that code? [import]uid: 217352 topic_id: 35808 reply_id: 142526[/import]

Hi Kane, one method would basically look like the code below. But, I encourage you to investigate other methods (i.e. physics sensors) to figure out the best way for your game.

[code]
local function fireStar()
–fire star code
end

local starTimer = “”
local isThrowing = false --flag

local function sensePosition()

local distance = math.abs( character.x - enemy.x)

if ( distance > 50 and isThrowing == false ) then --code to start throwing
isThrowing = true --set flag to true
fireStar() --fire initial star
starTimer = timer.performWithDelay( 1000, fireStar, 0 ) --start repeating timer of 1000 ms

elseif ( distance <= 50 and isThrowing == true ) --code to stop throwing
isThrowing = false
timer.cancel( starTimer ) ; starTimer = “”

end
end

Runtime:addEventListener( “enterFrame”, sensePosition )
[/code] [import]uid: 200026 topic_id: 35808 reply_id: 142580[/import]

Hello Kane,
Is this function is a touch listener? If so, you should filter the responses by the phase, as in “began”, but -not- “ended” or “moved”. If I see the line that is calling (or setting this function as a touch listener), I can provide a clearer answer.

Thanks,
Brent Sorrentino [import]uid: 200026 topic_id: 35808 reply_id: 142410[/import]

Hi Brent

No this is not a touch listener, when the character (you) come within a certain distance of the enemy’s x position then the enemy will begin to fire the throwing stars, and as this is not a touch listener the stars are spawned continuously.

Does that sense

Kane [import]uid: 217352 topic_id: 35808 reply_id: 142411[/import]

I’ve tried adding these commands to see if i can control the amount of stars being spawned but its is not helping might give you a clearer picture though

Kane

[code]
function enemyThrow()
local distance = math.abs( character.x - enemy.x)
if (distance > 50) then
ninjaThrow= movieclip.newAnim{“images/projectileA.png”,“images/projectileB.png”}
ninjaThrow.x = enemy.x + enemy.x
ninjaThrow.y = enemy.y
physics.addBody(ninjaThrow)
ninjaThrow:applyForce( 10, 0, ninjaThrow.x, ninjaThrow.y )
ninjaThrow:play()
timer.performWithDelay( 10000, enemyThrow, 0)

elseif (distance > 60) then
ninjaThrow:removeSelf()

end
end
[/code] [import]uid: 217352 topic_id: 35808 reply_id: 142415[/import]

Hi Kane,
So this runs on a Runtime listener, listening to the position of the player versus the enemy? If so, I suggest you use a repeating timer to control how often the stars are thrown, starting the timer when the player enters the “range” and canceling it when the player is out of range (actually, you would need to instantly fire the first star when the range is breached, to throw that first star, then you begin the timer for successive stars).

Brent [import]uid: 200026 topic_id: 35808 reply_id: 142436[/import]

Yeah that’s right would you have an example of how i could implement that code? [import]uid: 217352 topic_id: 35808 reply_id: 142526[/import]

Hi Kane, one method would basically look like the code below. But, I encourage you to investigate other methods (i.e. physics sensors) to figure out the best way for your game.

[code]
local function fireStar()
–fire star code
end

local starTimer = “”
local isThrowing = false --flag

local function sensePosition()

local distance = math.abs( character.x - enemy.x)

if ( distance > 50 and isThrowing == false ) then --code to start throwing
isThrowing = true --set flag to true
fireStar() --fire initial star
starTimer = timer.performWithDelay( 1000, fireStar, 0 ) --start repeating timer of 1000 ms

elseif ( distance <= 50 and isThrowing == true ) --code to stop throwing
isThrowing = false
timer.cancel( starTimer ) ; starTimer = “”

end
end

Runtime:addEventListener( “enterFrame”, sensePosition )
[/code] [import]uid: 200026 topic_id: 35808 reply_id: 142580[/import]