cocos2d-x TestLua 之 CCMenuItemLabel CCMenuItemSprite CCMenuItemFont 菜单类
2015-12-18 10:01
736 查看
1.CCMenu是由菜单项CCMenuItem 组成的,通常先创建菜单项,然后使用1个或多个菜单项生成菜单。
2.CCMenuItem子类:
CCMenuItemImage:图片按钮菜单项(通过图片资源创建菜单项)
CCMenuItemFont:文本按钮菜单项(通过文本创建菜单项)
CCMenuItemLabel:文本标签按钮菜单项(通过文本标签创建菜单项)
CCMenuItemToggle:开关按钮菜单项(通过其它类如CCMenuItemImage,CCMenuItemFont,CCMenuItemSprite创建菜单项)
CCMenuItemSprite:精灵按钮菜单项(通过精灵CCSprite创建菜单项)[plain]
view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
local function MenuLayer2()
local layer2 = CCLayer:create()
local item3 = nil
local function item1CallFunc(tag,sender)
item3:setEnabled(not item3:isEnabled())
item3:stopAllActions()
end
local function item2CallFunc(tag,sender)
local multiplexLayer = tolua.cast(layer2:getParent(),"CCLayerMultiplex")
multiplexLayer:switchTo(0)
end
local function item3CallFunc(tag,sender)
end
--CCLabelBMFont这个类使用之前,需要添加好字体文件,包括一个图片文件 (**.png)和一个字体坐标文件(**.fnt)。
local label = CCLabelBMFont:create("song_hui_xiang","bitmapFontTest3.fnt")
local item1 = CCMenuItemLabel:create(label)
item1:setPosition(ccp(size.width*0.5,size.height*0.8))
item1:registerScriptTapHandler(item1CallFunc)
--CCMenuItemFont
CCMenuItemFont:setFontName("Marker Felt") --设置字体类型
CCMenuItemFont:setFontSize(28) --设置字体大小
local item2 = CCMenuItemFont:create("-----go back-----")
item2:setPosition(ccp(size.width*0.5,size.height*0.5))
item2:registerScriptTapHandler(item2CallFunc) --注册触摸事件
--CCMenuItemSprite
local sp1 = CCSprite:create("menuitemsprite.png",CCRectMake(0,23*2,115,23)) --参数1:图片名,参数2:截取menuitemsprite.png的一个对象(x,y,w,h),以图片左上角为起始原点(x,y),向右为宽(w),向下为高(h)
local sp2 = CCSprite:create("menuitemsprite.png",CCRectMake(0,23,115,23))
local sp3 = CCSprite:create("menuitemsprite.png",CCRectMake(0,0,115,23))
item3 = CCMenuItemSprite:create(sp1,sp2,sp3) --依次为正常显示、按下、不可点击三种状态
item3:setPosition(ccp(size.width*0.5,size.height*0.2))
item3:registerScriptTapHandler(item3CallFunc)
item3:setEnabled(false)
local rotate1 = CCRotateBy:create(3,360) --创建旋转动作
local rotate2 = tolua.cast(rotate1:copy():autorelease(),"CCActionInterval") --复制一个rotate1动作
local rotate3 = tolua.cast(rotate1:copy():autorelease(),"CCActionInterval")
item1:runAction(CCRepeatForever:create(rotate1)) --永久执行rotate1动作
item3:runAction(CCRepeatForever:create(rotate3))
local jump = CCJumpBy:create(3,ccp(300,0),20,5) --创建跳跃动作 参数1:是动作经过的时间,参数2:是目标位置(目标位置是相对坐标,以精灵原来的位置为参照坐标),参数3:是跳跃的高度,参数4:是跳跃次数
local jump_back = jump:reverse()
item2:runAction(CCRepeatForever:create(CCSequence:createWithTwoActions(jump,jump_back)))
item2:runAction(CCRepeatForever:create(rotate2))
local menu = CCMenu:create() --创建CCMenu菜单
menu:setPosition(ccp(0,0))
menu:addChild(item1) --加入其它菜单项
menu:addChild(item2)
menu:addChild(item3)
layer2:addChild(menu)
return layer2
end
相关文章推荐
- Cocos2d-x v3.9发布
- cocos2dx之UserDefault: 数据存储及加密 (上)
- Cocos2d-x优化中纹理优化
- cocos2dx
- CocosBuilder使用总结
- Cocos2dx 3.0 交流篇
- Quick-Cocos2d-x3.3配置文件分析
- cocos2d-x之动作类说明
- Cocos2d-x应用:保存背景音乐和音效设置
- quick-cocos2d-x开发工具sublime text及其强力插件QuickXDev
- 在cocos2d-x中动态绘制二维码
- cocos2d-js 在Chrome的最小字体
- COCOS学习笔记--Cocos3.x引擎开发环境搭建
- 【Cocos2d-x 粒子系统】火球用手指飞起来
- cocos2d 和 cocos2d-x引擎选择
- cocos2dx如何使用CCFileUtils进行文件查找的
- cocos2d-x学习笔记(14)笨木头游戏系列LittleRunner
- cocos2d移植到安卓引入第三方so文件时候编译会删除解决方案
- cocos2d-js html5自定义鼠标图案及设置原点方法
- cocos2d 中文显示问题解决