今天用Lua實作了抛物線運動,将炮彈發射出去,呈抛物線運動,當進入水裡之後,進行減速
直接上代碼吧
local Danyao_P = class("Danyao_P" , function ()
return cc.Node:create()
end)
function Danyao_P:ctor()
self.visibleSize = cc.Director:getInstance():getVisibleSize()
self.type =
self.nowShell = nil
self.speed =
self.vx =
self.vy =
self.strength = --力度
self.k = --系數
--初始角度系數
self.kx = /math.sqrt()
self.ky = /math.sqrt()
self.mode = nil --判斷炮彈狀态
self.timeUpdate = nil
self.indexShell =
self.shellUpdate = nil
end
--x , y 為炮口所在坐标
function Danyao_P:shellRect(x , y)
if self.type == then
return cc.rect(x,y,,)
elseif self.type == then
return cc.rect(x,y,,)
elseif self.type == then
return cc.rect(x,y,,)
end
end
function Danyao_P:removeLogic()
if self.shellUpdate ~= nil then
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.shellUpdate)
self.shellUpdate = nil
end
end
function Danyao_P:load(x , y)
cc.SpriteFrameCache:getInstance():addSpriteFrames("Shell/ShellList.plist" , "Shell/ShellList.png")
cc.SpriteFrameCache:getInstance():addSpriteFrames("cocostudio/Common/Common.plist" , "cocostudio/Common/Common.png")
self.nowShell = cc.Sprite:createWithSpriteFrameName("danyao2.png")
self.nowShell:setScale(.)
-- self.type = typ
-- self:shellType()
self:addChild(self.nowShell)
self:setPosition(x , y)
self.strength =
self.k =
self.kx = math.cos(( - armature:getRotation()) * math.pi / )--根據炮筒目前角度而改變
self.ky = math.sin(( - armature:getRotation()) * math.pi / )
self.speed = self.k * self.strength /
--為炮彈重量
self.vx = self.speed * self.kx
self.vy = self.speed * self.ky
self.mode =
local function logic(t)
if self.mode == then
return
elseif self.mode == then
self.vy = self.vy -
local act = cc.MoveBy:create(t , cc.p(self.vx*t , self.vy*t))
self:runAction(act)
self:setRotation(-math.atan(self.vy/self.vx)*/math.pi)
--判斷是否入水
if self:getPositionY() < self.visibleSize.height/ + then
ccs.ArmatureDataManager:getInstance():addArmatureFileInfo("Animation/texiao001/texiao001.ExportJson")
local shuihua = ccs.Armature:create("texiao001")
shuihua:setPosition(self:getPositionX() , self:getPositionY() - )
shuihua:getAnimation():playWithIndex()
self:getParent():addChild(shuihua)
local function removeShuihua()
shuihua:removeFromParent()
end
shuihua:runAction(cc.Sequence:create(cc.DelayTime:create(.) , cc.CallFunc:create(removeShuihua)))
self.mode =
end
elseif self.mode == then
if self.vx > then
self.vx =
end
self.vx = self.vx*.
if self.vx > - then
self.vy = -
end
if self.vy*. < then
self.vy = self.vy*.
else
self.vy = -
end
local act = cc.MoveBy:create(t , cc.p(self.vx*t , self.vy*t))
self:runAction(act)
self:setRotation(-math.atan(self.vy/self.vx)*/math.pi)
end
self.shellUpdate = cc.Director:getInstance():getScheduler():scheduleScriptFunc(logic , / , false)
end
--------------------自定義函數 end -------------------------
function Danyao_P:onEnter()
-- body
end
function Danyao_P:onExit()
-- body
end
function Danyao_P:cleanUp()
APUtils.EventDispatcher:getInstance():removeAllFuncOfObject(self)
end
function Danyao_P:init(x , y)
self:load(x , y)
local function onNodeEvent(event)
if event == "cleanup" then
self:cleanUp()
elseif event == "exit" then
self:onExit()
elseif event == "enter" then
self:onEnter()
end
end
self:registerScriptHandler(onNodeEvent)
end
function Danyao_P:create(x , y)
local nodes = Danyao_P.new()
nodes:init(x , y)
return nodes
end
return Danyao_P