Spawn object easy way..

i have enemy… and i wanna spawn bullet from enemy.x,y at current distance…

how to do that??

This is very easy. 

Before i give you code i want to ask some questions.

Where are you shooting the bullets? 

Do you want a one tap and one bullet shoots or tap and hold and they shoot?

@InfiSnyp : i mean the enemy have gun,
the enemy not moving, the bullet spawn automaticly every 5 sec at 400 range…

from x = 400 to x = -400 (from right to left)
help me sir :frowning:

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 )

@InfiSnyp : wow thanks sir… ill try this code first :smiley:
yes sir, im using physic…

If the code doesn’t make sense i can add in comments saying what does what.

@InfiSnyp : i got some error sir…
“attempt to index global value ‘bullet’(a nil value)”

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 :frowning:

i also try this
[lua]
transition.to(bullet, {time=1000, x=300, itirasions=-1,transition=easing.continuousLoop})
[/lua]
but it doesnt work :frowning:

Are you trying to spawn the bullet on the hero and make it shoot like a machine gun?

@InfiSnyp : no sir… the enemy did…
enemy shoot to hero… repeatly…
its look simple but im confused :frowning:

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 )

@InfiSnyp : aw… thx so much sir…
sry for late replay… ill try this soon :smiley:

This is very easy. 

Before i give you code i want to ask some questions.

Where are you shooting the bullets? 

Do you want a one tap and one bullet shoots or tap and hold and they shoot?

@InfiSnyp : i mean the enemy have gun,
the enemy not moving, the bullet spawn automaticly every 5 sec at 400 range…

from x = 400 to x = -400 (from right to left)
help me sir :frowning: