1.CCNotificationCenter废弃了,建议用EventListnerCustom
layer._listener1 = cc.EventListenerCustom:create("component game over",onEvent) local eventDispatcher = cc.Director:getInstance():getEventDispatcher() eventDispatcher:addEventListenerWithFixedPriority(layer._listener1,1)
2.UILabel -------> Text
3. Node节点监听
local function onNodeEvent(event) if "exit" == event then ccexp.AudioEngine:stopAll() end end layer:registerScriptHandler(onNodeEvent)
4. 声效
ccexp.AudioEngine:stopAll()
5 LayerColor
local bgLayer = cc.LayerColor:create(cc.c4b(0,128,255,255));
6 ccui.TextField
local function textFieldEvent(sender,eventType) if eventType == ccui.TextFiledEventType.attach_with_ime then local textField = sender textField:runAction(cc.MoveBy:create(0.225,cc.p(0,20))) self._displayValueLabel:setString("attach with IME") elseif eventType == ccui.TextFiledEventType.detach_with_ime then local textField = sender textField:runAction(cc.MoveBy:create(0.175,-20))) self._displayValueLabel:setString("detach with IME") elseif eventType == ccui.TextFiledEventType.insert_text then self._displayValueLabel:setString("insert words") elseif eventType == ccui.TextFiledEventType.delete_backward then self._displayValueLabel:setString("delete word") end end local textField = ccui.TextField:create() textField:setTouchEnabled(true) textField:setFontName(font_TextName) textField:setFontSize(30) textField:setPlaceHolder("input words here") textField:setPosition(cc.p(widgetSize.width / 2.0,widgetSize.height / 2.0)) textField:addEventListener(textFieldEvent) self._uiLayer:addChild(textField)
7 .PageView getPages():count() -----------> table.getn(cc.PageView:getItems())
8 注册监听返回键
local function touch_key_back(keycode,event) if keyCode == cc.KeyCode.KEY_BACK then end end local listener = cc.EventListenerKeyboard:create() listener:registerScriptHandler(touch_key_back,cc.Handler.EVENT_KEYBOARD_RELEASED ) local eventDispatcher = clayer:getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraPHPriority(listener,clayer)
9自定义消息EventCustom
-- 发送消息 post message -- event将会被传递给消息接收函数,所以所有需要传递的数据都可放到event中 local event = cc.EventCustom:new("MSG_TEST") event._usedata = {["state"]=self.curState,["actionId"]=self.actionId} cc.Director:getInstance():getEventDispatcher():dispatchEvent(event)
----
-- listener local ActorSprite = class("ActorSprite",cc.Node) function ActorSprite:initEventListener() self:enableNodeEvents() -- cc.Node self.eventListeners = {} -- user to delete when dtor local listener = cc.EventListenerCustom:create(MSG_TEST,handler(self,self.onChangeState)) local eventDispatcher = self:getEventDispatcher() eventDispatcher:addEventListenerWithFixedPriority(listener,1) table.insert(self.eventListeners,listener) end function DogSprite:onChangeState(event) local dogId = event._usedata["dogId"] local actionId = event._usedata["actionId"] local eventName = event:getEventName() end
cc.Director:getInstance():getEventDispatcher():removeEventListener(var)
10 ListView
-- 让单个Item 在ListView 水平居中 listView:setGravity(ccui.ListViewGravity.centerHorizontal)
11
cc.SpriteFrameCache:getInstance():addSpriteFrames("zwoptex/grossini.plist")
12 eventlistener
注意 前置层是否有layout,layout默认阻挡触摸
local function onTouchEvent(touch,event) if eventType == "began" then return self:onTouchBegan(touch,event) elseif eventType == "moved" then self:onTouchMoved(touch,event) elseif eventType == "ended" then self:onTouchEnded(touch,event) elseif eventType == "cancelled" then self:onTouchCancelled(touch,event) end end self._touchListener = nil local listener = cc.EventListenerTouchOneByOne:create() listener:setSwallowTouches(true) listener:registerScriptHandler(self.onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) listener:registerScriptHandler(self.onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED ) listener:registerScriptHandler(self.onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED ) listener:registerScriptHandler(self.onTouchCancelled,cc.Handler.EVENT_TOUCH_CANCELLED ) local eventDispatcher = self:getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraPHPriority(listener,self)
13 scheduler
scheduleScriptFunc
self.timer_schedulehandler = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(matchroom_ui.timefunction,1,false)
14 帧事件
local node = cc.CSLoader:createNode("Node.csb") node:setPosition(cc.p(100,100)) self.parent:addChild(node) local action = cc.CSLoader:createTimeline("Node.csb") node:runAction(action) action:gotoFrameAndPlay(0,false) local function onFrameEvent(frame) if nil == frame then return end local str = frame:getEvent() cclog("frame event:" .. str) if str == "end" then node:removeFromParentAndCleanup(true) end end action:setFrameEventCallFunc(onFrameEvent) end
15. c 与 c++ 混合编程
#if __cplusplus extern "C" { #endif /* .... */ #if __cplusplus } #endif
16 平台判断
local targetPlatform = cc.Application:getInstance():getTargetPlatform()
17 android uuid
//获得独一无二的Psuedo ID public static String getUniquePsuedoID() { String serial = null; String m_szDevIDShort = "35" + Build.BOARD.length()%10+ Build.BRAND.length()%10 + Build.cpu_ABI.length()%10 + Build.DEVICE.length()%10 + Build.DISPLAY.length()%10 + Build.HOST.length()%10 + Build.ID.length()%10 + Build.MANUFACTURER.length()%10 + Build.MODEL.length()%10 + Build.PRODUCT.length()%10 + Build.TAGS.length()%10 + Build.TYPE.length()%10 + Build.USER.length()%10 ; //13 位 try { serial = android.os.Build.class.getField("SERIAL").get(null).toString(); //API>=9 使用serial号 return new UUID(m_szDevIDShort.hashCode(),serial.hashCode()).toString(); } catch (Exception exception) { //serial需要一个初始化 serial = "serial"; // 随便一个初始化 } //使用硬件信息拼凑出来的15位号码 return new UUID(m_szDevIDShort.hashCode(),serial.hashCode()).toString();
18.javah 使用
//进入到 src 目录 // -d -- 输出目录 javah -d ../jni [com.xxxx.xxx (packagename)].ClassName
19 cocos Ref
Ref::~Ref() { #if CC_ENABLE_SCRIPT_BINDING && !CC_ENABLE_GC_FOR_NATIVE_OBJECTS // if the object is referenced by Lua engine,remove it if (_luaID) { // add follow 2 lines ScriptEngineProtocol* pEngine = ScriptEngineManager::getInstance()->getScriptEngine(); if (pEngine) ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptObjectByObject(this); } else { ScriptEngineProtocol* pEngine = ScriptEngineManager::getInstance()->getScriptEngine(); if (pEngine != nullptr && pEngine->getScriptType() == kScriptTypeJavascript) { pEngine->removeScriptObjectByObject(this); } } #endif #if CC_REF_LEAK_DETECTION if (_referenceCount != 0) untrackRef(this); #endif }原文链接:https://www.f2er.com/cocos2dx/339064.html