橢圓形運(yùn)動(dòng)軌跡

感謝2位大佬的貢獻(xiàn)

--[[
橢圓運(yùn)動(dòng)軌跡 by 胡廣超
]]
local LRActionInterval = class("LRActionInterval")

local sin = math.sin
local cos = math.cos

function LRActionInterval:tuoyuanXat(angle)--返回X坐標(biāo) 
    return  self._aLength*cos(angle) --參數(shù)方程 
end

function LRActionInterval:tuoyuanYat(angle)--返回Y坐標(biāo)
    return self._bLength*sin(angle)
end


function LRActionInterval:create(args)
    local obj = LRActionInterval.new(args)
    if obj then 
        obj:init(args)
    end
    return obj
end


function LRActionInterval:init(args)
    local detlaAngle = args.detlaAngleor or 10-- 變化角度
    local startAngle = args.startAngle or 0-- 初始角度


    self._detlaAngle = 2* math.pi * detlaAngle/360 
    self._startAngle = 2* math.pi * startAngle/360  
    self._center = args.center or cc.p(500,200)
    self._time = 0
    self._aLength = args.a or 200   
    self._bLength = args.b or 120
    self._pTarget = args.node  
    self._parent = args.parent  --父節(jié)點(diǎn)
    self.speed = args.speed  --旋轉(zhuǎn)速度
    self:setTarget(self._pTarget, self._parent)
end

function LRActionInterval:setTarget(node,parent)
    if parent then
       self.drawNode = cc.DrawNode:create()
        parent:addChild(self.drawNode)
    end

    local x = self:tuoyuanXat(self._startAngle)
    local y = self:tuoyuanYat(self._startAngle)
    local pos = cc.p(x + self._center.x, y + self._center.y)
    self._pTarget:setPosition(pos)
end


function LRActionInterval:star()
    self.scheduleTimeID = g_scheduleScriptFunc(handler(self,self.update), 0.01)
end

function LRActionInterval:close()
    if self.scheduleTimeID  then
        g_unscheduleScriptEntry(self.scheduleTimeID )
        self.scheduleTimeID = nil 
    end
end

function LRActionInterval:update(time_)
    self._time = self._time - time_
    if self._pTarget then
        local angle = self._startAngle + self._detlaAngle * self._time*self.speed  

        local x = self:tuoyuanXat(angle) 
        local y = self:tuoyuanYat(angle) 
        local pos = cc.p(x + self._center.x, y + self._center.y)
        self._pTarget:setPosition(pos)
        if self.drawNode then
            self.drawNode:drawPoint(pos, 2, cc.c4f(1, 0, 0, 1))
        end
        
    end
end


return LRActionInterval

使用樣例 by 劉新夏

        self.tbSch = {}
    local detla = {0,90,180,270}
    for i=1,4 do
        local title = g_seekWidgetByName(self._root, "btn_tpye_"..i, handler(self, self.btnChooseTypeFun))
        title:setTag(i)
        g_seekWidgetByName(title,"Image_11"):hide()
        g_seekWidgetByName(title,"Image_12"):hide()
        local pos = cc.p(title:getPosition())
        table.insert(_title,i,title)
        table.insert(title_pos,i,pos)
        table.insert(cur_pos,i)

    
        local size = title:getParent():getContentSize()
        local args = { startAngle = detla[i], node = title ,center = cc.p(size.width/2,size.height/2), a = 200, b = 120, speed = 1}
        local lra = LRActionInterval:create(args)
        lra:star()
        table.insert(self.tbSch, lra)
    end
旋轉(zhuǎn)效果.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容