小小菜之Cocos2d-x游戏开发旅程——Lua之抛物线运动

23 篇文章 0 订阅
8 篇文章 0 订阅

今天用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 = 2
    self.nowShell = nil
    self.speed = 0 
    self.vx = 0 
    self.vy = 0
    self.strength = 0   --力度
    self.k = 0  --系数
    --初始角度系数
    self.kx = 1/math.sqrt(2)
    self.ky = 1/math.sqrt(2)
    self.mode = nil   --判断炮弹状态
    self.timeUpdate = nil
    self.indexShell = 0
    self.shellUpdate = nil
end

--x , y 为炮口所在坐标
function Danyao_P:shellRect(x , y)
    if self.type == 1 then
        return cc.rect(x,y,65,64)
    elseif self.type == 2 then
        return cc.rect(x,y,72,69)
    elseif self.type == 3 then
        return cc.rect(x,y,64,28)
    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(0.8)
    -- self.type = typ
    -- self:shellType()
    self:addChild(self.nowShell)
    self:setPosition(x , y)
    self.strength = 200
    self.k = 100
        self.kx = math.cos((15 - armature:getRotation()) * math.pi / 180)--根据炮筒当前角度而改变
    self.ky = math.sin((15 - armature:getRotation()) * math.pi / 180)
    self.speed = self.k * self.strength / 60 
--60为炮弹重量
    self.vx = self.speed * self.kx
    self.vy = self.speed * self.ky
    self.mode = 1
    local function logic(t)
        if self.mode == 0 then
            return
        elseif self.mode == 1 then
            self.vy = self.vy - 6
            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)*180/math.pi)
            --判断是否入水
            if self:getPositionY() < self.visibleSize.height/2 + 110 then
                ccs.ArmatureDataManager:getInstance():addArmatureFileInfo("Animation/texiao001/texiao001.ExportJson")
                local shuihua = ccs.Armature:create("texiao001")
                shuihua:setPosition(self:getPositionX() , self:getPositionY() - 20)     
                shuihua:getAnimation():playWithIndex(0)
                self:getParent():addChild(shuihua)
                local function removeShuihua()
                    shuihua:removeFromParent()
                end
                shuihua:runAction(cc.Sequence:create(cc.DelayTime:create(0.2) , cc.CallFunc:create(removeShuihua)))
                self.mode = 2
            end
        elseif self.mode == 2 then
            if self.vx > 120 then
                self.vx = 60
            end
            self.vx = self.vx*0.98
            if self.vx > -160 then
                self.vy = -50
            end
            if self.vy*0.98 < 150 then
                self.vy = self.vy*0.98
            else
                self.vy = -10
            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)*180/math.pi)
        end
    self.shellUpdate = cc.Director:getInstance():getScheduler():scheduleScriptFunc(logic , 1/60 , 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值