lua有关网站http://blog.51cto.com/shahdza/1569003
复选框
local checkbox1=node:getChildByName("CheckBox_1") music=userDefualt:getBoolForKey("yinyue1"); checkbox1:setSelected(music); checkbox1:addEventListener(function ( sender,eventType) -- 0==ccui.CheckBoxEventType.selected -- 1==ccui.CheckBoxEventType.unselected if eventType==0 then cc.SimpleAudioEngine:getInstance():resumeMusic() userDefualt:setBoolForKey("yinyue1",true); userDefualt:flush(); slider:setEnabled(true ); print("点击") elseif eventType==1 then userDefualt:setBoolForKey("yinyue1",false); userDefualt:flush(); slider:setEnabled(false ); cc.SimpleAudioEngine:getInstance():pauseMusic() end
end)
手动滑块
用cocos2d studio拼
slider:setEnabled(music); yinliang=userDefualt:getIntegerForKey("yinliang"); slider:setMaxPercent(10); slider:setPercent(yinliang); slider:addEventListener(function (sender,eventType) if eventType==ccui.SliderEventType.percentChanged then local voile=slider:getPercent(); self.volic=voile cc.SimpleAudioEngine:getInstance():setMusicVolume(voile*0.1); userDefualt:setIntegerForKey("yinliang",voile); userDefualt:flush(); end
end)
-- 触摸接听
-- local listener=cc.EventListenerTouchOneByOne:create()
-- listener:registerScriptHandler(function ( touch,event ) -- print("began") -- return true -- end,cc.Handler.EVENT_TOUCH_BEGAN )
-- listener:registerScriptHandler(function ( touch,event ) -- print("ended") -- end,cc.Handler.EVENT_TOUCH_ENDED)
-- listener:registerScriptHandler(function ( touch,event ) -- print("moved") -- end,cc.Handler.EVENT_TOUCH_MOVED)
-- listener:registerScriptHandler(function ( touch,event ) -- print("canselled") -- end,cc.Handler.EVENT_TOUCH_CANCELLED) -- cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(listener, layer) 或
layer:onTouch(function( event ) if event.name=="began" then print(event.x,event.y) return true elseif event.name=="moved" then print("moved") elseif event.name=="ended" then print("ended") end
end)
碰撞
点面碰撞
if cc.rectContainsPoint(self.butterfly:getBoundingBox(),cc.p(event.x,event.y)) then
print("碰撞了。。。。。。。")
end
面面碰撞
cc.rectIntersectsRect(rect1, rect2)
-- -蝴蝶动画 --加载到精灵帧缓存 display.loadSpriteFrames("hudie.plist","hudie.png")
self.butterfly =display.newSprite("#1.png",display.right -150,display.top-150) self.butterfly:setScale(2) layer:addChild(self.butterfly)
self:startAnimate( )
function StartScene:startAnimate() --创建精灵祯动画 local frames =display.newFrames("%d.png",0 ,8) local animation =display.newAnimation(frames,0.1) self.butterfly:playAnimationForever(animation) local moveto1=cc.MoveTo:create(2 ,cc.p(display.left+150,display.top-150)) local moveto2= cc.MoveTo:create(1,cc.p(260,260)) seq= cc.Sequence: create(moveto1,moveto2) self.butterfly: runAction(seq)
end
或
加载到精灵帧缓存
local spriteFrame = CCSpriteFrameCache:getInstance()
spriteFrame:addSpriteFrames("ant.plist", "ant.png")
local ant= cc.Sprite:createWithSpriteFrame(spriteFrame:getSpriteFrame("ants" .. antNum .. "_0.png"))
self.layer:addChild(ant)
其他都一样(红色字体)
时间调度器
self.antSharedScheduler = self.scene:getScheduler()
-- 或 --self.antShareScheduler = cc.Director:getInstance():getScheduler()
self.antScheduID = self.antSharedScheduler:scheduleScriptFunc(..., 1, false)scheduleScriptFunc(unsigned int nHandler, float fInterval, bool bPaused),见该函数原型。 第一个参数是你要注册的回调函数,第二个是执行该函数循环的秒数,第三个参数是是否暂停。 一般使用例子: scheduleScriptFunc(callbackFunc,0,false)即可。
结束
self.antSharedScheduler:unscheduleScriptEntry(self.antScheduID)
写onEnter( )
local function onNodeEvent( event ) if "enter"==event then self:onEnter() elseif "exit"==event then self:onExit() end
end
--注册layer的 进入 退出事件用法 layer:registerScriptHandler(onNodeEvent)
return layer end
function MainScene:onEnter( ) print("我是onEnter") end function MainScene:onExit( ) print("我是onExit")
end
--碰撞检测:蜗牛和刺 function gameScene:collisionListener() --注册碰撞监听事件 local function onContactBegin(contact) print("****************************") local node1=contact:getShapeA():getBody():getNode() local node2=contact:getShapeB():getBody():getNode() if (node1:getTag()==600 and node2:getTag()==1000) or (node1:getTag()==1000 and node2:getTag()==600) then self:flied( ) end return true end
local contactListener = cc.EventListenerPhysicsContact:create() contactListener:registerScriptHandler(onContactBegin, cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN); local eventDispatcher = cc.Director:getInstance():getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraphPriority(contactListener, self.layer);
|
请发表评论