Bullets

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]
 

The code looks a bit confused. You calculate a ‘speed’ but that’s actually a distance - how far you want to go - or to be more accurate 1/100 of it. It sort of defines the speed because your time is constant.

Bullets usually stop when they hit things :slight_smile: 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)

Hi.  I’ve coded up three examples of how to do this:

(Fixed Video)

https://www.youtube.com/watch?v=wklDDanKEeQ&feature=youtu.be&hd=1

  1. Using transitions only, and no collisions.

  2. Same but with collisions

  3. Similar, but bullets use setLinearVelocity() to move.

You can get it here.

Warning, contains some heavy-duty code.

PS - Modify line 38 in main.lua to select between the 3 examples: 1, 2, or 3.

PPS - Oops, wrong video.  Fixed that.

ahh thanks a lot roaming gamer, exactly what i needed this will help in many future projects too :slight_smile:

The code looks a bit confused. You calculate a ‘speed’ but that’s actually a distance - how far you want to go - or to be more accurate 1/100 of it. It sort of defines the speed because your time is constant.

Bullets usually stop when they hit things :slight_smile: 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)

Hi.  I’ve coded up three examples of how to do this:

(Fixed Video)

https://www.youtube.com/watch?v=wklDDanKEeQ&feature=youtu.be&hd=1

  1. Using transitions only, and no collisions.

  2. Same but with collisions

  3. Similar, but bullets use setLinearVelocity() to move.

You can get it here.

Warning, contains some heavy-duty code.

PS - Modify line 38 in main.lua to select between the 3 examples: 1, 2, or 3.

PPS - Oops, wrong video.  Fixed that.

ahh thanks a lot roaming gamer, exactly what i needed this will help in many future projects too :slight_smile:

This code is back up here: 

This code is back up here: