function GameScene:createInGameLayer() local inGameLayer = cc.Layer::create(); return inGameLayer;end2、基本场景
local sceneGame = cc.Scene:create() sceneGame:addChild(createInGameLayer()) cc.Director:getInstance():runWithScene(sceneGame) cc.Director:getInstance():replaceScene(cc.TransitionFade:create(1,WelcomeScene.createScene()))3、基本精灵
function createInGameLayer() local inGameLayer = cc.Layer:create() local bg = cc.Sprite:create("farm.jpg") bg:setAnchorPoint(0,0) inGameLayer:addChild(bg) return inGameLayer end
4、基本定时器
local function tick() end cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick,false)
5、基本触摸事件
local touchBeginPoint = nil local function ontouchBegan(touch,event) local location = touch:getLocation() cclog("ontouchBegan: %0.2f,%0.2f",location.x,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)6、基本音乐
--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) end7、基本加载图片
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")8、基本动作
local function CallFucnCallback1() end local action = cc.Sequence:create( cc.MoveBy:create(2,cc.p(200,0)),cc.CallFunc:create(CallFucnCallback1) ) grossini:runAction(action)9、基本格式化输出字符串
string.format("grossini_dance_%02d.png",j + 1)10、基本按钮
local start = cc.Sprite:createWithSpriteFramename("start.png") local startItem = cc.MenuItemSprite:create(start,start,start) local function menuCallback(sender) cclog("menuCallback...") --tolua.cast(ret:getParent(),"cc.LayerMultiplex"):switchTo(1) end startItem:registerScriptTapHandler(menuCallback) startItem:setposition(50,50) local menu = cc.Menu:create() menu:addChild(startItem) menu:setposition(0,0) layer:addChild(menu)
11、基本菜单
-- create menufunction GameScene:createLayerMenu() local layerMenu = cc.Layer:create() local menuPopup,menuTools,effectID local function menuCallbackClosePopup() -- stop test sound effect cc.SimpleAudioEngine:getInstance():stopEffect(effectID) menuPopup:setVisible(false) end 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 -- add a popup menu local menuPopupItem = cc.MenuItemImage:create("menu2.png","menu2.png") menuPopupItem:setposition(0,0) menuPopupItem:registerScriptTapHandler(menuCallbackClosePopup) menuPopup = cc.Menu:create(menuPopupItem) menuPopup:setposition(self.origin.x + self.visibleSize.wIDth / 2,self.origin.y + self.visibleSize.height / 2) menuPopup:setVisible(false) layerMenu:addChild(menuPopup) -- add the left-bottom "tools" menu to invoke menuPopup local menuToolsItem = cc.MenuItemImage:create("menu1.png","menu1.png") menuToolsItem:setposition(0,0) menuToolsItem:registerScriptTapHandler(menuCallbackOpenPopup) menuTools = cc.Menu:create(menuToolsItem) local itemWIDth = menuToolsItem:getContentSize().wIDth local itemHeight = menuToolsItem:getContentSize().height menuTools:setposition(self.origin.x + itemWIDth/2,self.origin.y + itemHeight/2) layerMenu:addChild(menuTools) return layerMenuend总结
以上是内存溢出为你收集整理的Cocos2d-x Lua学习【1】基本元素的创建全部内容,希望文章能够帮你解决Cocos2d-x Lua学习【1】基本元素的创建所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)