Oliver Bennett
Oliver Bennett

Reputation: 157

Make projectile transition to edge of screen

I've tried googling this however the tutorials only tell you how to make a projectile transition to a certain target.In my case I want it to travel towards the target but then carry on until the edge of the screen.

This is my current shoot function:

local function shoot()
--Invert coordinates 
local x = screenWidth - x
local y = screenHeight - y

--a2+b2=c2
local sqDistance = ((x-centerX)*(x-centerX))+((y-centerY)*(y-centerY)) 
--Sqaure root it.
local dist = mSqrt(sqDistance); 

--speed = d/t
distTime = dist/BULLET_SPEED
--if distTime is negative, this makes it positive
if distTime < 0 then 
    distTime = distTime -distTime -distTime
end 

--Create the bullet
local shot = display.newRect(1,1,6,2)
shot.x = centerX; shot.y = centerY; 
shot:setFillColor(240,200,0)
shot.rotation = angleBetween+90;                        
bulletGroup:insert(shot)

--Remove bullet 
local function removeShot()
    display.remove(shot)
    shot = nil
end     

--Move the bullet
transition.to(shot, {time = distTime, x = x, y = y, onComplete = removeShot})


end

Upvotes: 1

Views: 192

Answers (2)

NaviRamyle
NaviRamyle

Reputation: 4007

Make the body a bullet by setting the object.isBullet = true to act as a bullet.

See reference here http://docs.coronalabs.com/api/type/Body/isBullet.html

Upvotes: 0

Oliver Bennett
Oliver Bennett

Reputation: 157

Found out how to do this, unfortunately it seems you have to use the physics library.

When creating the bullet add this line:

physics.addBody(shot,"dynamic") 

And use this instead of transition.to to move the bullet:

--Move the bullet
local xVelocity = (x-centerX)*BULLET_SPEED
local yVelocity = (y- centerY)*BULLET_SPEED
shot:setLinearVelocity(xVelocity,yVelocity)

Upvotes: 1

Related Questions