hey all, im making a top down shooter and i was wondering what would be the best way to make make enemy bullets fire at my heros position at the time of the firing . I was previously using a a transition.moveTo but the problem with this is that as soon as the bullet reached the position of the hero it would stop but i want it to carry on so that it looks like an actual bullet.
[lua]
local Ship = display.newImage( “Neon_Cube.png” )
Ship.x = screenW * .5
Ship.y = screenH * .5
local function main( event )
– MOVE THE SHIP
MyStick:move(Ship, 10, true)
end
– spawn bullets
local function SpawnBullet (event)
if event.phase == “began” then
local bullet = display.newImage(“Bullet1.png”)
bullet.x = Ship.x
bullet.y = Ship.y
bullet.isBullet = true
bullet.rotation = Ship.rotation
local radians = math.rad(bullet.rotation-90)
local speed = 10
local dx = math.cos( radians ) * speed
local dy = math.sin( radians ) * speed
transition.to( bullet, {time = 1000, x = bullet.x + dx*100, y = bullet.y + dy*100} )
end
end
local enemybullet = display.newRoundedRect( 0, 0, 30, 30, 3)
transition.moveTo(enemy,{x = Ship.x, y = Ship.y, time = 800 })
[/lua]
If you want it to go further, say the edge of the screen, use trigonometry to calculate y for x = 0 or x = display.contentWidth, y = 0 or y = display.contentHeight (depending on which way it is going)