Questions about joystick and bullets

Hi,

I found a joystick file online and I got a few questions that confuse me.

Right now, I have a 360 degree joystick that control the player. 

I can move the player everywhere, but after I release the button.

The player will automatically rotate back to 0 degree.

Here is the joystick code.

[lua]local js = {}
local Pi = math.pi
local Sqr = math.sqrt
local Rad = math.rad
local Sin = math.sin
local Cos = math.cos
local Ceil = math.ceil
local Atan2 = math.atan2
local NewStick
local Group


– FUNCTION: CREATE

function NewStick( Props )

     Group = display.newGroup()
     Group.x = Props.x
     Group.y = Props.y
     Group.Timer = nil
     Group.angle = 0
     Group.distance = 0
     Group.percent = 0
     Group.maxDist = Props.borderSize
     Group.snapBackSpeed = Props.snapBackSpeed ~= nil and Props.snapBackSpeed or .7

     Group.Border = display.newImage(“joystickmain1a.png”)
     Group:insert(Group.Border)

     Group.Thumb = display.newImage(“joystickmain1b.png”) – picture here?
     Group.Thumb.x0 = 0
     Group.Thumb.y0 = 0
     Group:insert(Group.Thumb)


– METHOD: DELETE STICK

function Group:delete()
     self.Border = nil
     self.Thumb = nil
     if self.Timer ~= nil then timer.cancel(self.Timer); self.Timer = nil end
     self:removeSelf()
end


– METHOD: MOVE AN OBJECT

function Group:move(Obj, maxSpeed, rotate)
     if rotate == true then Obj.rotation = self.angle end
     print("self.angle = "…tostring(self.angle))
     print("self.distance = "…tostring(self.distance))
     print("maxSpeed * self.percent = "…tostring(maxSpeed * self.percent))
     Obj.x = Obj.x + Cos( Rad(self.angle-90) ) * (maxSpeed * self.percent)
     Obj.y = Obj.y + Sin( Rad(self.angle-90) ) * (maxSpeed * self.percent)
end


– GETTER METHODS

function Group:getDistance() return self.distance end
function Group:getPercent () return self.percent end
function Group:getAngle () return Ceil(self.angle) end


– HANDLER: ON DRAG

Group.onDrag = function ( event )

     local T = event.target – THUMB
     local S = T.parent – STICK
     local phase = event.phase
     local ex,ey = S:contentToLocal(event.x, event.y)
          ex = ex - T.x0
          ey = ey - T.y0

     if “began” == phase then
          if S.Timer ~= nil then timer.cancel(S.Timer); S.Timer = nil end
          display.getCurrentStage():setFocus( T )
          T.isFocus = true
          – STORE INITIAL POSITION
          T.x0 = ex - T.x
          T.y0 = ey - T.y

     elseif T.isFocus then
          if “moved” == phase then
               -----------
               S.distance = Sqr (ex*ex + ey*ey)
               if S.distance > S.maxDist then S.distance = S.maxDist end
               S.angle = ( (Atan2( ex-0,ey-0 )*180 / Pi) - 180 ) * -1
               S.percent = S.distance / S.maxDist
               -----------
               T.x = Cos( Rad(S.angle-90) ) * (S.maxDist * S.percent)
               T.y = Sin( Rad(S.angle-90) ) * (S.maxDist * S.percent)

          elseif “ended”== phase or “cancelled” == phase then
               T.x0 = 0
               T.y0 = 0
               T.isFocus = false
               display.getCurrentStage():setFocus( nil )

               S.Timer = timer.performWithDelay( 33, S.onRelease, 0 )
               S.Timer.MyStick = S
               --added so that angle reverted to 0 when stick is released
               S.angle = 0
          end
end

          – STOP FURTHER PROPAGATION OF TOUCH EVENT!
          return true

end

     
     ---------------------------------------------
     – HANDLER: ON DRAG RELEASE
     ---------------------------------------------
     Group.onRelease = function( event )

          local S = event.source.MyStick
          local T = S.Thumb

          local dist = S.distance > S.maxDist and S.maxDist or S.distance
          dist = dist * S.snapBackSpeed

          T.x = Cos( Rad(S.angle-90) ) * dist
          T.y = Sin( Rad(S.angle-90) ) * dist

          local ex = T.x
          local ey = T.y
          -----------
          S.distance = Sqr (ex*ex + ey*ey)
          if S.distance > S.maxDist then S.distance = S.maxDist end
          S.angle = ( (Atan2( ex-0,ey-0 )*180 / Pi) - 180 ) * -1
          S.percent = S.distance / S.maxDist
          -----------
          if S.distance < .5 then
          S.distance = 0
          S.percent = 0
          T.x = 0
          T.y = 0
          timer.cancel(S.Timer); S.Timer = nil
     end

end

     Group.Thumb:addEventListener( “touch”, Group.onDrag )

     return Group

end

js.NewStick = NewStick

return js
[/lua]

And this is my Main.lua

[lua]local StickLib = require(“lib_analog_stick”)
local physics = require( ‘physics’ )
physics.start()

local screenW = display.contentWidth
local screenH = display.contentHeight
local Text = display.newText( " ", screenW*.6, screenH-20, native.systemFont, 15 )

– CREATE A SPACE SHIP
local Ship = display.newGroup()
Ship.x = screenW * .5
Ship.y = screenH * .5
Tmp = display.newImageRect (“player.png”, 50, 50)
Ship:insert (Tmp)

– CREATE ANALOG STICK
MyStick = StickLib.NewStick(
     {
     x = screenW*.15,
     y = screenH*0.85,
     thumbSize = 16,
     borderSize = 32,
     snapBackSpeed = .2,
     R = 0,
     G = 255,
     B = 255
     } )


– MAIN LOOP

local function main( event )

     – MOVE THE SHIP
     MyStick:move(Ship, 9.0, true)

     – SHOW STICK INFO
     Text.text = “ANGLE = “…MyStick:getAngle()…” DIST = “…math.ceil(MyStick:getDistance())…” PERCENT =      “…math.ceil(MyStick:getPercent()*100)…”%”

     end
timer.performWithDelay(2000, function()
end, 1)
Runtime:addEventListener( “enterFrame”, main )[/lua]

So now, when I hold the joystick, my player’s angle will rotate and move where the joystick direct.

However when I release the joystick the player won’t stay in the last angle that i released. It will return to 0 degree after I release. What can I change to make the player won’t rotate after I release the joystick

(Stay at the same angle where I release the button)

I have a further question about bullets

Here is bullet’s code

[lua]function shoot (event)

     if (event.phase == “began”) then
          local bullet = display.newRect (Ship.x, Ship.y, 10, 30)
          physics.addBody ( bullet, “dynamic”)
          bullet.gravityScale = 0
          bullet.isBullet = true
          bullet:setLinearVelocity ( 0, 500 )
     end
end

Runtime:addEventListener (“touch”, shoot)[/lua]

My code only shoots in the same direction. What can i do to shoot my bullet in the front of my player’s face while he is rotating and change the destination when I rotate my player.

Since english is not my first language 

I’m sorry if you don’t understand what I’m trying to say. 

I’ll try to explain what Im trying to say if u leave the question 

Anyone please !?!?

Anyone please !?!?