This is from another question i answered.
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 )