类似刀剑乱斗的围绕圆点转圈(2d)

local radius = 100 --半径
local num = 0 --个数
local x0,y0=200,200 --圆心
local spList = {} --node数组

local function onAdd(self, w)
	radius = math.random(20,200) --设置半径

	local sp = getSpriteNode(self) --创建node
	self:addChild(sp)
	sp:setRotation(math.random()>0.5 and 90 or 0) --node是否旋转(设不设都行)
	table.insert(spList,sp) --插入到列表里

	if #spList > 1 then --获取个数
		num = 360 / (#spList) --(小于360则顺时针)
	end

	if not self._tickID then --在计时器内 根据长度 重置位置
		local offset = 0 --每个node之间偏移的值
		local cb = function ( ... )
			for k,v in pairs(spList) do
				local radian = (2* math.pi / 360) * (num*k - offset) --计算弧度
				local x =  x0 + math.sin(radian) * radius --计算位置
				local y =  y0 + math.cos(radian) * radius
				v:setPosition(x,y)
			end
			offset = offset + 6
		end
		self._tickID = self:schedule(cb,0.05)
	end
end

 


版权声明:本文为xbdf1234原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。