Cocos2d-x 3(5)

– 显示到x=200,y=200的位置

child:setPosition( 200,200 )

ret:addChild(child, 1)

–Sum of all action’s duration is 1.5 second.

– 旋转一个节点,1.5秒,旋转90度

child:runAction(cc.RotateBy:create(1.5, 90))

– 执行动作序列,1.4秒延迟,淡出

child:runAction(cc.Sequence:create(cc.DelayTime:create(1.4),cc.FadeOut:create(1.1)))

local function removeThis()

– 溢出孩子

ret:getParent():removeChild(ret, true)

Helper.nextAction()

end

–After 1.5 second, self will be removed.

– 1.5秒之后,自身会被移除

ret:runAction( cc.Sequence:create(cc.DelayTime:create(1.4),cc.CallFunc:create(removeThis)))

return ret

end


– LogicTest

– 逻辑测试


local function LogicTest()

local ret = createTestLayer(“Logic test”)

– 精灵,s_pPathGrossini为图片路径

local grossini = cc.Sprite:create(s_pPathGrossini)

– 添加一个子节点到容器中,有Z轴顺序和一个标记。

ret:addChild(grossini, 0, 2)

grossini:setPosition(200,200)

local function bugMe(node)

– 停止所有动作

node:stopAllActions() --After this stop next action not working, if remove this stop everything is working

node:runAction(cc.ScaleTo:create(2, 2))

end

– 运行动作序列

grossini:runAction( cc.Sequence:create(cc.MoveBy:create(1, cc.p(150,0)) ,cc.CallFunc:create(bugMe)))

return ret

end


– PauseTest

– 暂停测试


local function PauseTest()

local ret = createTestLayer(“Pause Test”)

local schedulerEntry = nil

local function unpause(dt)

scheduler:unscheduleScriptEntry(schedulerEntry)

schedulerEntry = nil

local node = ret:getChildByTag( kTagGrossini )

local pDirector = cc.Director:getInstance()

pDirector:getActionManager():resumeTarget(node)

end

local function onNodeEvent(event)

– 进入时

if event == “enter” then

local s = cc.Director:getInstance():getWinSize()

local l = cc.Label:createWithTTF(“After 3 seconds grossini should move”, “fonts/Thonburi.ttf”, 16)

ret:addChild(l)

l:setAnchorPoint(cc.p(0.5, 0.5))

l:setPosition( cc.p(s.width / 2, 245) )

local grossini = cc.Sprite:create(s_pPathGrossini)

ret:addChild(grossini, 0, kTagGrossini)

grossini:setPosition(cc.p(200,200))

– 创建移动动作,持续时间1秒,移动到(150,0)的位置

local action = cc.MoveBy:create(1, cc.p(150,0))

local pDirector = cc.Director:getInstance()

– 通过获取director关联的ActionManager并为目标添加动作

– 为一个目标添加动作。 如果目标已经存在,动作将被加在已经存在的目标上。

– 如果目标不存在,将会创建这个目标的新对象,这个动作将被添加在这个新创建出来的对象上 当目标动作被暂停,动作队列的顺序也不会乱。

pDirector:getActionManager():addAction(action, grossini, true)

schedulerEntry = scheduler:scheduleScriptFunc(unpause, 3.0, false)

– 退出

elseif event == “exit” then

if schedulerEntry ~= nil then

scheduler:unscheduleScriptEntry(schedulerEntry)

end

end

end

– 注册响应事件

ret:registerScriptHandler(onNodeEvent)

return ret

end


– RemoveTest


local function RemoveTest()

local ret = createTestLayer(“Remove Test”)

local l = cc.Label:createWithTTF(“Should not crash”, “fonts/Thonburi.ttf”, 16)

– 获得屏幕大小

local s = cc.Director:getInstance():getWinSize()

ret:addChild(l)

l:setAnchorPoint(cc.p(0.5, 0.5))

l:setPosition( cc.p(s.width / 2, 245))

– 创建移动动作,持续2秒,到(200,0)的位置

local pMove = cc.MoveBy:create(2, cc.p(200, 0))

– 停止动作

local function stopAction()

– 根据Tag来获取子节点

local pSprite = ret:getChildByTag(kTagGrossini)

pSprite:stopActionByTag(kTagSequence)

end

– 创建一个回调函数

local callfunc = cc.CallFunc:create(stopAction)

local pSequence = cc.Sequence:create(pMove,callfunc)

pSequence:setTag(kTagSequence)

local pChild = cc.Sprite:create(s_pPathGrossini)

pChild:setPosition( 200, 200 )

ret:addChild(pChild, 1, kTagGrossini)

pChild:runAction(pSequence)

return ret

end


– ResumeTest

– 恢复测试


local function ResumeTest()

local ret = createTestLayer(“Resume Test”)

local schedulerEntry = nil

local function resumeGrossini(time)

scheduler:unscheduleScriptEntry(schedulerEntry)

schedulerEntry = nil

local pGrossini = ret:getChildByTag(kTagGrossini)

local pDirector = cc.Director:getInstance()

pDirector:getActionManager():resumeTarget(pGrossini)

end

local function onNodeEvent(event)

if event == “enter” then

local l = cc.Label:createWithTTF(“Grossini only rotate/scale in 3 seconds”, “fonts/Thonburi.ttf”, 16)

ret:addChild(l)

local s = cc.Director:getInstance():getWinSize()

l:setAnchorPoint(cc.p(0.5, 0.5))

l:setPosition( s.width / 2, 245)

local pGrossini = cc.Sprite:create(s_pPathGrossini)

ret:addChild(pGrossini, 0, kTagGrossini)

pGrossini:setPosition(200,200)

– 运行缩放的动作

pGrossini:runAction(cc.ScaleBy:create(2, 2))

local pDirector = cc.Director:getInstance()

– 暂停目标

pDirector:getActionManager():pauseTarget(pGrossini)

– 运行旋转动作,旋转360度,持续2秒

pGrossini:runAction(cc.RotateBy:create(2, 360))

schedulerEntry = scheduler:scheduleScriptFunc(resumeGrossini, 3.0, false)

elseif event == “exit” then

if schedulerEntry ~= nil then

scheduler:unscheduleScriptEntry(schedulerEntry)

end

end

end

ret:registerScriptHandler(onNodeEvent)

return ret

end

function ActionManagerTestMain()

cclog(“ActionManagerTestMain”)

Helper.index = 1 – 初始索引为1

– 设置深度测试

cc.Director:getInstance():setDepthTest(true)

– 创建场景

local scene = cc.Scene:create()

– 初始化方法表

Helper.createFunctionTable = {

CrashTest,

LogicTest,

PauseTest,

RemoveTest,

ResumeTest

}

– 添加层

scene:addChild(CrashTest())

scene:addChild(CreateBackMenuItem())

return scene

end

在例子中用到一些定义好的资源路径,还有相关帮助类,童鞋们可以到可以到相应目录下进行查找:

help.lua(帮助类,封装定义了相关方法,创建测试层、切换场景等)

require “Cocos2d”

CC_CONTENT_SCALE_FACTOR = function()

– 获取surface的大小,单位为像素

return cc.Director:getInstance():getContentScaleFactor()

end

–把以像素为单位的矩形转换为以点为单位的矩形

CC_POINT_PIXELS_TO_POINTS = function(pixels)

return cc.p(pixels.x/CC_CONTENT_SCALE_FACTOR(), pixels.y/CC_CONTENT_SCALE_FACTOR())

end

– 把以点为单位的矩形转换为以像素为单位的矩形

CC_POINT_POINTS_TO_PIXELS = function(points)

return cc.p(points.xCC_CONTENT_SCALE_FACTOR(), points.yCC_CONTENT_SCALE_FACTOR())

end

– cclog 打印日志

cclog = function(…)

print(string.format(…))

end

– change table to enum type 把表转换为枚举类型

function CreateEnumTable(tbl, index)

local enumTable = {}

local enumIndex = index or -1

for i, v in ipairs(tbl) do

enumTable[v] = enumIndex + i

end

return enumTable

end

– back menu callback 返回菜单回调

local function MainMenuCallback()

local scene = cc.Scene:create()

scene:addChild(CreateTestMenu())

– 切换场景

cc.Director:getInstance():replaceScene(scene)

end

– add the menu item for back to main menu

– 为返回主菜单添加菜单项

function CreateBackMenuItem()

– 创建一个标签

local label = cc.Label:createWithTTF(“MainMenu”, s_arialPath, 20)

– 设置器锚点

label:setAnchorPoint(cc.p(0.5, 0.5))

– 设置菜单项标签

local MenuItem = cc.MenuItemLabel:create(label)

MenuItem:registerScriptTapHandler(MainMenuCallback)

– 获得屏幕大小

local s = cc.Director:getInstance():getWinSize()

– 创建菜单

local Menu = cc.Menu:create()

– 添加菜单项

Menu:addChild(MenuItem)

– 设置菜单位置

Menu:setPosition(0, 0)

– 设置菜单项位置,大致在右下角的位置

MenuItem:setPosition(s.width - 50, 25)

return Menu

end

– 帮助类

Helper = {

index = 1, – 索引

createFunctioinTable = nil, – 存储方法的表

currentLayer = nil, – 当前层

titleLabel = nil, – 标题

subtitleLabel = nil – 子标题

}

– 下个动作

function Helper.nextAction()

Helper.index = Helper.index + 1 – 索引加1

if Helper.index > table.getn(Helper.createFunctionTable) then

Helper.index = 1

end

return Helper.newScene()

end

– 回退动作

function Helper.backAction()

Helper.index = Helper.index - 1

if Helper.index == 0 then

Helper.index = table.getn(Helper.createFunctionTable)

end

return Helper.newScene()

end

– 重新开始动作

function Helper.restartAction()

return Helper.newScene()

end

– 切换新的场景

function Helper.newScene()

local scene

– 如果使用物理效果

if Helper.usePhysics then

– 创建一个带物理效果的场景

scene = cc.Scene:createWithPhysics()

else

scene = cc.Scene:create()

end

Helper.currentLayer = Helper.createFunctionTableHelper.index

– 添加当前层

scene:addChild(Helper.currentLayer)

– 添加返回菜单

scene:addChild(CreateBackMenuItem())

– 切换场景

cc.Director:getInstance():replaceScene(scene)

end

– 初始化层

function Helper.initWithLayer(layer)

Helper.currentLayer = layer

– 获取屏幕大小

local size = cc.Director:getInstance():getWinSize()

Helper.titleLabel = cc.Label:createWithTTF(“”, s_arialPath, 28)

Helper.titleLabel:setAnchorPoint(cc.p(0.5, 0.5))

layer:addChild(Helper.titleLabel, 1)

Helper.titleLabel:setPosition(size.width / 2, size.height - 50)

Helper.subtitleLabel = cc.Label:createWithTTF(“”, s_thonburiPath, 16)

最后

那我们该怎么做才能做到年薪60万+呢,对于程序员来说,只有不断学习,不断提升自己的实力。我之前有篇文章提到过,感兴趣的可以看看,到底要学习哪些知识才能达到年薪60万+。

通过职友集数据可以查看,以北京 Android 相关岗位为例,其中 【20k-30k】 薪酬的 Android 工程师,占到了整体从业者的 30.8%!

北京 Android 工程师「工资收入水平 」

今天重点内容是怎么去学,怎么提高自己的技术。

1.合理安排时间

2.找对好的系统的学习资料

3.有老师带,可以随时解决问题

4.有明确的学习路线

当然图中有什么需要补充的或者是需要改善的,可以在评论区写下来,一起交流学习。

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
.5, 0.5))

layer:addChild(Helper.titleLabel, 1)

Helper.titleLabel:setPosition(size.width / 2, size.height - 50)

Helper.subtitleLabel = cc.Label:createWithTTF(“”, s_thonburiPath, 16)

最后

那我们该怎么做才能做到年薪60万+呢,对于程序员来说,只有不断学习,不断提升自己的实力。我之前有篇文章提到过,感兴趣的可以看看,到底要学习哪些知识才能达到年薪60万+。

通过职友集数据可以查看,以北京 Android 相关岗位为例,其中 【20k-30k】 薪酬的 Android 工程师,占到了整体从业者的 30.8%!

北京 Android 工程师「工资收入水平 」

[外链图片转存中…(img-GnumwQcV-1714936700687)]

今天重点内容是怎么去学,怎么提高自己的技术。

1.合理安排时间

2.找对好的系统的学习资料

3.有老师带,可以随时解决问题

4.有明确的学习路线

当然图中有什么需要补充的或者是需要改善的,可以在评论区写下来,一起交流学习。

[外链图片转存中…(img-jHeKAPPC-1714936700688)]

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 30
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值