Cocos2d-x Lua学习【1】基本元素的创建
发布时间:2020-12-14 19:13:05 所属栏目:百科 来源:网络整理
导读:1、基本层 function GameScene:createInGameLayer() local inGameLayer = cc.Layer::create(); return inGameLayer;end 2、基本场景 local sceneGame = cc.Scene:create() sceneGame:addChild(createInGameLayer()) cc.Director:getInstance():runWithScene(
1、基本层function GameScene:createInGameLayer() local inGameLayer = cc.Layer::create(); return inGameLayer; end 2、基本场景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) end 7、基本加载图片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、基本格式化输出字符串
|