user2588337
user2588337

Reputation: 71

How to fire a bullet using transition.to function and changing the bullet angle and direction while firing in corona sdk

I want to fire a bullet using transition.to function, i am able to generate bullet on the tap event of object but how can i change the direction in which the bullet is fired when i rotate my object, and also changing the angle in the direction of firing.. Code for generating bullet is below... Please give me some idea how to achieve this functionality.... thanks

display.setStatusBar( display.HiddenStatusBar )


local function rotateObj(event)
        local t = event.target
        local phase = event.phase

        if (phase == "began") then
                display.getCurrentStage():setFocus( t )
                t.isFocus = true

                t.x1 = event.x
                t.y1 = event.y

        elseif t.isFocus then
                if (phase == "moved") then
                        t.x2 = event.x
                        t.y2 = event.y

                        angle1 = 180/math.pi * math.atan2(t.y1 - t.y , t.x1 - t.x)
                        angle2 = 180/math.pi * math.atan2(t.y2 - t.y , t.x2 - t.x)
                        print("angle1 = "..angle1)
                        rotationAmt = angle1 - angle2


                        t.rotation = t.rotation - rotationAmt
                        print ("t.rotation = "..t.rotation)

                        t.x1 = t.x2
                        t.y1 = t.y2

                elseif (phase == "ended") then

                        display.getCurrentStage():setFocus( nil )
                        t.isFocus = false
                end
        end

        return true
end

local function shootfunc(event)
local getxpos=event.target.x
local getypos=event.target.y
local laser = display.newRect(1,1,10,35)
laser.x = getxpos
laser.y = getypos
laser:setFillColor(240,200,0)
transition.to( laser,  { time = 800,x = 600, y = 20 })
end

local shot= display.newRect(1,1,40,100)
shot.x = 450; shot.y = 700
shot:setFillColor(240,200,0)


shot:addEventListener( "touch", rotateObj )

shot:addEventListener( "tap", shootfunc )

Upvotes: 1

Views: 914

Answers (1)

six8
six8

Reputation: 2990

A simple solution is to calculate the x, y coordinates of a point that is some distance (the range of your bullet) from your gun at the same angle as your gun.

display.setStatusBar( display.HiddenStatusBar )

local function rotateObj(event)
    local t = event.target
    local phase = event.phase

    if (phase == "began") then
        display.getCurrentStage():setFocus( t )
        t.isFocus = true

        t.x1 = event.x
        t.y1 = event.y

    elseif t.isFocus then
        if (phase == "moved") then
            t.x2 = event.x
            t.y2 = event.y

            angle1 = 180/math.pi * math.atan2(t.y1 - t.y, t.x1 - t.x)
            angle2 = 180/math.pi * math.atan2(t.y2 - t.y, t.x2 - t.x)
            rotationAmt = angle1 - angle2
            t.rotation = t.rotation - rotationAmt

            t.x1 = t.x2
            t.y1 = t.y2
        elseif (phase == "ended") then
            display.getCurrentStage():setFocus( nil )
            t.isFocus = false
        end
    end

    return true
end

-- forward declare `gun` so `shootfunc` can "see" it 
local gun

--[[
Returns x, y coordinates of a point `distance` units out
along a line at `angle` degrees.
]]--
local function pointAtDistance(angle, distance)
    -- Convert angle to radians as lua math functions expect radians
    local r = math.rad(angle)
    local x = math.cos(r) * distance
    local y = math.sin(r) * distance    

    return x, y
end

local function shootfunc(event)
    local laser = display.newRect(0, 0,10, 35)    
    laser:setFillColor(240, 0, 0)

    -- Laser should come out of gun barrel so we get a point
    -- along the guns barrel to come out of
    local tipX, tipY = pointAtDistance(gun.rotation - 90, (gun.height / 2) + (laser.height / 2)) 
    laser.x = gun.x + tipX
    laser.y = gun.y + tipY

    -- distance to shoot bullet (anywhere offscreen is sufficient)
    local distance = 1000

    -- Make bullet match angle of gun
    laser.rotation = gun.rotation

    -- We actually want to shoot perpendicular to our angle
    local shootAngle = laser.rotation - 90

    -- Plot x, y target for bullet based on rotation
    local x, y = pointAtDistance(shootAngle, distance)
    local targetX = laser.x + x
    local targetY = laser.y + y    

    -- Remove the bullet when we're done with it
    local function destroy()
        laser:removeSelf()
    end

    transition.to(laser, { 
        time = 800,
        -- Fade to invisble
        alpha = 0, 
        x = targetX, 
        y = targetY, 
        -- Destroy when done
        onComplete = destroy
    })
end

-- Use a group for the gun so we can add other objects to it
gun = display.newGroup()
gun.x = display.contentCenterX
gun.y = display.contentCenterY

-- The shape of the gun
local gunBody = display.newRect(-20, -50, 40, 100)
gunBody:setFillColor(240, 200, 0)
gun:insert(gunBody)

-- A site on the gun so we can tell where it's pointed
local site = display.newRect(-5, -55, 10, 20)
site:setFillColor(100, 200, 50)
gun:insert(site)

gun:addEventListener("touch", rotateObj)
gun:addEventListener("tap", shootfunc)

Upvotes: 1

Related Questions