Are you using physics in your game?
I didn’t test this but it’s a general idea.
local physics = require("physics") physics.start() local bullet = {} local bCounter = 1 local bTimer local centerX = display.contentCenterX local centerY = display.contentCenterY function spawnBullet() local bWidth = 5 local bHeight = 10 local xFly = 0 local yFly = -200 bullet[bCounter] = display.newImageRect( centerX, centerY, bWidth, bHeight ) physics.addBody( bullet[bCounter], "dynamic", { isSensor = false } ) bullet[bCounter].gravityScale = 0 bullet[bCounter]:setLinearVelocity( xFly, yFly ) bCounter = bCounter + 1 end bTimer = timer.performWithDelay( 500, spawnBullet, -1 )
If the code doesn’t make sense i can add in comments saying what does what.
Oops.
local physics = require("physics") physics.start() local bullet = {} local bCounter = 1 local bTimer local centerX = display.contentCenterX local centerY = display.contentCenterY function spawnBullet() local bWidth = 5 local bHeight = 10 local xFly = 0 local yFly = -200 bullet[bCounter] = display.newRect( centerX, centerY, bWidth, bHeight ) physics.addBody( bullet[bCounter], "dynamic", { isSensor = false } ) bullet[bCounter].gravityScale = 0 bullet[bCounter]:setLinearVelocity( xFly, yFly ) bCounter = bCounter + 1 end bTimer = timer.performWithDelay( 500, spawnBullet, -1 )
@InfiSnyp : its work sir… but bullet spawn at wrong place… i wanna enemy shoot the hero
so if enemy name “enemy”
how to spawn bullet from enemy.x to hero.x??
i also have perspective camera… should i add bullet like this??
[lua]
camera:add(bullet,1,false)
[/lua]
You would do hero.x …
I dont know how you add you camera but just do some experementing and see if it works.
@InfiSynp : i try a lot but cant spawn at enemy.x
also when i insert at camera “camera:add(bullet)” it show error
i also try this
[lua]
transition.to(bullet, {time=1000, x=300, itirasions=-1,transition=easing.continuousLoop})
[/lua]
but it doesnt work
Are you trying to spawn the bullet on the hero and make it shoot like a machine gun?
Oh. So your trying to get the bad guy to shoot the good guy?
Well for that you’d need to do some math. Let me make a small sample game for you.
Here. I think this is along the lines of what you want?
local physics = require("physics") physics.start() local bullet = {} local bCounter = 1 local bTimer local centerX = display.contentCenterX local centerY = display.contentCenterY local hero = display.newRect( centerX, centerY - 200, 20, 20 ) physics.addBody( hero, "dynamic" ) hero.gravityScale = 0 hero.myName = "hero" local enemy = display.newRect( centerX, centerY, 20, 20 ) physics.addBody( enemy, "static" ) enemy.gravityScale = 0 local function moveHero(event) if event.phase == "began" then display.getCurrentStage():setFocus( event.target ) elseif event.phase == "moved" then hero.x = event.x hero.y = event.y elseif event.phase == "ended" then display.getCurrentStage():setFocus(nil) end end hero:addEventListener( "touch", moveHero ) function onCollision(event) if event.phase == "began" then if event.target.myName == "hero" and event.other.myName == "bullet" then local removeIt = event.other display.remove(removeIt) end end end hero:addEventListener( "collision", onCollision ) function spawnBullet() local cirRadius = 2 local xFly = enemy.x - hero.x local yFly = enemy.y - hero.y bullet[bCounter] = display.newCircle( enemy.x, enemy.y, cirRadius ) physics.addBody( bullet[bCounter], "dynamic", { isSensor = true } ) bullet[bCounter].gravityScale = 0 bullet[bCounter]:setLinearVelocity( -xFly, -yFly ) bullet[bCounter].myName = "bullet" bCounter = bCounter + 1 end bTimer = timer.performWithDelay( 500, spawnBullet, -1 )
Here’s some “upgraded” code.
local physics = require("physics") physics.start() local bullet = {} local bCounter = 1 local bTimer local centerX = display.contentCenterX local centerY = display.contentCenterY local hero = display.newRect( centerX, centerY - 200, 20, 20 ) physics.addBody( hero, "dynamic" ) hero.gravityScale = 0 hero.myName = "hero" local enemy = display.newRect( centerX, centerY, 20, 20 ) physics.addBody( enemy, "static" ) enemy.gravityScale = 0 local function moveHero(event) if event.phase == "began" then display.getCurrentStage():setFocus( event.target ) elseif event.phase == "moved" then hero.x = event.x hero.y = event.y elseif event.phase == "ended" then display.getCurrentStage():setFocus(nil) end end hero:addEventListener( "touch", moveHero ) function onCollision(event) if event.phase == "began" then if event.target.myName == "hero" and event.other.myName == "bullet" then local removeIt = event.other display.remove(removeIt) end end end hero:addEventListener( "collision", onCollision ) function spawnBullet() local cirRadius = 2 local speed = 2 local xFly = enemy.x - hero.x local yFly = enemy.y - hero.y local diff = xFly \* yFly local normX = xFly / diff local normY = yFly / diff bullet[bCounter] = display.newCircle( enemy.x, enemy.y, cirRadius ) physics.addBody( bullet[bCounter], "dynamic", { isSensor = true } ) bullet[bCounter].gravityScale = 0 bullet[bCounter]:setLinearVelocity( -xFly \* speed, -yFly \* speed ) bullet[bCounter].myName = "bullet" bCounter = bCounter + 1 end bTimer = timer.performWithDelay( 500, spawnBullet, -1 )