From: http://www.jb51.cc/article/p-xenprwqz-kt.html
最基本的层
function createInGameLayer() local inGameLayer = cc.Layer:create() return inGameLayer end
最基本的场景
local sceneGame = cc.Scene:create() sceneGame:addChild(createInGameLayer()) cc.Director:getInstance():runWithScene(sceneGame) cc.Director:getInstance():replaceScene(cc.TransitionFade:create(1,WelcomeScene.createScene()))
最基本的精灵
function createInGameLayer() local inGameLayer = cc.Layer:create() local bg = cc.Sprite:create("farm.jpg") bg:setAnchorPoint(0,0) inGameLayer:addChild(bg) return inGameLayer end
最基本的定时器
local function tick() end cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick,false)
最基本的触摸事件
- localtouchBeginPoint=nil@H_404_17@@H_404_17@
- localfunctiononTouchBegan(touch,event)@H_404_17@
- locallocation=touch:getLocation()@H_404_17@
- cclog("onTouchBegan:%0.2f,%0.2f",location.x,location.y)@H_404_17@
- touchBeginPoint={x=location.x,y=location.y}@H_404_17@
- --CCTOUCHBEGANeventmustreturntrue@H_404_17@
- --[[多点@H_404_17@
- fori=1,table.getn(touches)do@H_404_17@
- locallocation=touches[i]:getLocation()@H_404_17@
- Sprite1.addNewSpriteWithCoords(Helper.currentLayer,location)@H_404_17@
- end@H_404_17@
- ]]--@H_404_17@
- returntrue@H_404_17@
- end@H_404_17@
- @H_404_17@
- localfunctiononTouchMoved(touch,event)@H_404_17@
- locallocation=touch:getLocation()@H_404_17@
- cclog("onTouchMoved:%0.2f,location.y)@H_404_17@
- iftouchBeginPointthen@H_404_17@
- localcx,cy=layerFarm:getPosition()@H_404_17@
- layerFarm:setPosition(cx+location.x-touchBeginPoint.x,@H_404_17@
- cy+location.y-touchBeginPoint.y)@H_404_17@
- touchBeginPoint={x=location.x,y=location.y}@H_404_17@
- end@H_404_17@
- end@H_404_17@
- @H_404_17@
- localfunctiononTouchEnded(touch,event)@H_404_17@
- locallocation=touch:getLocation()@H_404_17@
- cclog("onTouchEnded:%0.2f,location.y)@H_404_17@
- touchBeginPoint=nil@H_404_17@
- spriteDog.isPaused=false@H_404_17@
- end@H_404_17@
- @H_404_17@
- locallistener=cc.EventListenerTouchOneByOne:create()@H_404_17@
- --locallistener=cc.EventListenerTouchAllAtOnce:create()多点@H_404_17@
- listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN)@H_404_17@
- listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED)@H_404_17@
- listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED)@H_404_17@
- localeventDispatcher=layerFarm:getEventDispatcher()@H_404_17@
- eventDispatcher:addEventListenerWithSceneGraPHPriority(listener,layerFarm)@H_404_17@
local touchBeginPoint = nil local function onTouchBegan(touch,event) local location = touch:getLocation() cclog("onTouchBegan: %0.2f,location.y) touchBeginPoint = {x = location.x,y = location.y} -- CCTOUCHBEGAN event must return true --[[多点 for i = 1,table.getn(touches) do local location = touches[i]:getLocation() Sprite1.addNewSpriteWithCoords(Helper.currentLayer,location) end ]]-- return true end local function onTouchMoved(touch,event) local location = touch:getLocation() cclog("onTouchMoved: %0.2f,location.y) if touchBeginPoint then local cx,cy = layerFarm:getPosition() layerFarm:setPosition(cx + location.x - touchBeginPoint.x,cy + location.y - touchBeginPoint.y) touchBeginPoint = {x = location.x,y = location.y} end end local function onTouchEnded(touch,event) local location = touch:getLocation() cclog("onTouchEnded: %0.2f,location.y) touchBeginPoint = nil spriteDog.isPaused = false end local listener = cc.EventListenerTouchOneByOne:create() --local listener = cc.EventListenerTouchAllAtOnce:create() 多点 listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED ) listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED ) local eventDispatcher = layerFarm:getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraPHPriority(listener,layerFarm)
最基本的音乐
- --localbgMusicPath=CCFileUtils:getInstance():fullPathForFilename("background.ogg")@H_404_17@@H_404_17@
- @H_404_17@
- localbgMusicPath=cc.FileUtils:getInstance():fullPathForFilename("background.mp3")@H_404_17@
- @H_404_17@
- cc.SimpleAudioEngine:getInstance():playMusic(bgMusicPath,true)@H_404_17@
- localeffectPath=cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")@H_404_17@
- @H_404_17@
- cc.SimpleAudioEngine:getInstance():preloadEffect(effectPath)@H_404_17@
- @H_404_17@
- localfunctionmenuCallbackOpenPopup()@H_404_17@
- --looptestsoundeffect@H_404_17@
- localeffectPath=cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")@H_404_17@
- effectID=cc.SimpleAudioEngine:getInstance():playEffect(effectPath)@H_404_17@
- menuPopup:setVisible(true)@H_404_17@
- end@H_404_17@
--local bgMusicPath = CCFileUtils:getInstance():fullPathForFilename("background.ogg") local bgMusicPath = cc.FileUtils:getInstance():fullPathForFilename("background.mp3") cc.SimpleAudioEngine:getInstance():playMusic(bgMusicPath,true) local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav") cc.SimpleAudioEngine:getInstance():preloadEffect(effectPath) local function menuCallbackOpenPopup() -- loop test sound effect local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav") effectID = cc.SimpleAudioEngine:getInstance():playEffect(effectPath) menuPopup:setVisible(true) end
最基本的加载图片
- cc.Director:getInstance():getTextureCache():addImageAsync("DartBlood.png",imageLoaded)@H_404_17@@H_404_17@
- localtexture0=cc.Director:getInstance():getTextureCache():addImage("Images/grossini_dance_atlas.png")@H_404_17@
- @H_404_17@
- functionLoadingScene.imageLoaded(pObj)@H_404_17@
- --body@H_404_17@
- end@H_404_17@
- @H_404_17@
- cc.Director:getInstance():getTextureCache():removeTextureForKey("Images/grossinis_sister1-testalpha.png")@H_404_17@
- cc.Director:getInstance():getTextureCache():removeAllTextures()@H_404_17@
- cc.Director:getInstance():getTextureCache():removeUnusedTextures()@H_404_17@
- @H_404_17@
- localcache=cc.SpriteFrameCache:getInstance()@H_404_17@
- cache:addSpriteFrames("animations/grossini_gray.plist","animations/grossini_gray.png")@H_404_17@
- SpriteFrameTest.m_pSprite1=cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png")@H_404_17@
cc.Director:getInstance():getTextureCache():addImageAsync("DartBlood.png",imageLoaded) local texture0 = cc.Director:getInstance():getTextureCache():addImage( "Images/grossini_dance_atlas.png") function LoadingScene.imageLoaded( pObj) -- body end cc.Director:getInstance():getTextureCache():removeTextureForKey("Images/grossinis_sister1-testalpha.png") cc.Director:getInstance():getTextureCache():removeAllTextures() cc.Director:getInstance():getTextureCache():removeUnusedTextures() local cache = cc.SpriteFrameCache:getInstance() cache:addSpriteFrames("animations/grossini_gray.plist","animations/grossini_gray.png") SpriteFrameTest.m_pSprite1 = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png")
最基础的动作
local function CallFucnCallback1() end local action = cc.Sequence:create( cc.MoveBy:create(2,cc.CallFunc:create(CallFucnCallback1) ) grossini:runAction(action)
最基础的字符格式化
string.format("grossini_dance_%02d.png",j + 1)
最基础的按钮
- localstart=cc.Sprite:createWithSpriteFrameName("start.png")@H_404_17@@H_404_17@
- @H_404_17@
- @H_404_17@
- localstartItem=cc.MenuItemSprite:create(start,start,start)@H_404_17@
- @H_404_17@
- @H_404_17@
- localfunctionmenuCallback(sender)@H_404_17@
- cclog("menuCallback...")@H_404_17@
- --tolua.cast(ret:getParent(),"cc.LayerMultiplex"):switchTo(1)@H_404_17@
- end@H_404_17@
- @H_404_17@
- @H_404_17@
- startItem:registerScriptTapHandler(menuCallback)@H_404_17@
- startItem:setPosition(50,50)@H_404_17@
- @H_404_17@
- @H_404_17@
- localmenu=cc.Menu:create()@H_404_17@
- menu:addChild(startItem)@H_404_17@
- menu:setPosition(0,0)@H_404_17@
- layer:addChild(menu) @H_404_17@