
在main.lua中。必须要的代码
cc.fileUtils:getInstance():addSearchPath("src")cc.fileUtils:getInstance():addSearchPath("res")-- CC_USE_DEPRECATED_API = truerequire "cocos.init"-- cclogcclog = function(...) print(string.format(...))end-- for ccluaEngine tracebackfunction __G__TRACKBACK__(msg) cclog("----------------------------------------") cclog("LUA ERROR: " .. tostring(msg) .. "\n") cclog(deBUG.traceback()) cclog("----------------------------------------") return msgend--------------------------------------------local function main() collectgarbage("collect") -- avoID memory leak collectgarbage("setpause",100) collectgarbage("setstepmul",5000) -- initialize director local director = cc.Director:getInstance() local glvIEw = director:getopenGLVIEw() if nil == glvIEw then glvIEw = cc.GLVIEwImpl:createWithRect("Hellolua",cc.rect(0,900,640)) director:setopenGLVIEw(glvIEw) end glvIEw:setDesignResolutionSize(480,320,cc.ResolutionPolicy.NO_border) -- turn on display FPS director:setdisplayStats(true) -- set FPS. the default value is 1.0/60 if you don't call this director:setAnimationInterval(1.0 / 60) local schedulerID = 0 -- support deBUG local targetPlatform = cc.Application:getInstance():getTargetPlatform() if (cc.PLATFORM_OS_IPHONE == targetPlatform) or(cc.PLATFORM_OS_IPAD == targetPlatform) or (cc.PLATFORM_OS_ANDROID == targetPlatform) or(cc.PLATFORM_OS_windows == targetPlatform) or (cc.PLATFORM_OS_MAC == targetPlatform) then cclog("result is ") -- require('deBUGger')() end -------------------------------------------- <span > require "GameScene"</span>
-- local gameScene = require("GameScene") -- local scene = gameScene.create() --scene:playBgMusic() local scene = GameScene:create() scene:playBgMusic() if cc.Director:getInstance():getRunningScene() then cc.Director:getInstance():replaceScene(scene) else cc.Director:getInstance():runWithScene(scene) endendlocal status,msg = xpcall(main,__G__TRACKBACK__)if not status then error(msg)end 然后定义另外一个GameScene.lua文件
<pre name="code" > GameScene = class("GameScene",function() return cc.Scene:create()end)function GameScene:create() local scene = GameScene.new() scene:addChild(scene:createLayerFarm()) scene:addChild(scene:createLayerMenu()) return sceneendfunction GameScene:ctor() self.visibleSize = cc.Director:getInstance():getVisibleSize() self.origin = cc.Director:getInstance():getVisibleOrigin() self.schedulerID = nilendfunction GameScene:playBgMusic() 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)endfunction GameScene:creatDog() local frameWIDth = 105 local frameHeight = 95 -- create dog animate local textureDog = cc.Director:getInstance():getTextureCache():addImage("dog.png") local rect = cc.rect(0,frameWIDth,frameHeight) local frame0 = cc.SpriteFrame:createWithTexture(textureDog,rect) rect = cc.rect(frameWIDth,frameHeight) local frame1 = cc.SpriteFrame:createWithTexture(textureDog,rect) local spriteDog = cc.Sprite:createWithSpriteFrame(frame0) spriteDog:setposition(self.origin.x,self.origin.y + self.visibleSize.height / 4 * 3) spriteDog.isPaused = false local animation = cc.Animation:createWithSpriteFrames({frame0,frame1},0.5) local animate = cc.Animate:create(animation); spriteDog:runAction(cc.RepeatForever:create(animate)) -- moving dog at every frame local function tick() if spriteDog.isPaused then return end local x,y = spriteDog:getposition() if x > self.origin.x + self.visibleSize.wIDth then x = self.origin.x else x = x + 1 end spriteDog:setpositionX(x) end self.schedulerID = cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick,false) return spriteDogend-- create farmfunction GameScene:createLayerFarm() local layerFarm = cc.Layer:create() -- add in farm background local bg = cc.Sprite:create("farm.jpg") bg:setposition(self.origin.x + self.visibleSize.wIDth / 2 + 80,self.origin.y + self.visibleSize.height / 2) layerFarm:addChild(bg) -- add land sprite for i = 0,3 do for j = 0,1 do local spriteLand = cc.Sprite:create("land.png") spriteLand:setposition(200 + j * 180 - i % 2 * 90,10 + i * 95 / 2) layerFarm:addChild(spriteLand) end end -- add crop local frameCrop = cc.SpriteFrame:create("crop.png",105,95)) for i = 0,1 do local spriteCrop = cc.Sprite:createWithSpriteFrame(frameCrop); spriteCrop:setposition(210 + j * 180 - i % 2 * 90,40 + i * 95 / 2) layerFarm:addChild(spriteCrop) end end -- add moving dog local spriteDog = self:creatDog() layerFarm:addChild(spriteDog) -- handing touch events 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} spriteDog.isPaused = true -- CCtouchBEGAN event must return true 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() 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) local function onNodeEvent(event) if "exit" == event then cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.schedulerID) end end layerFarm:registerScriptHandler(onNodeEvent) return layerFarmend-- 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-3.3 rec】 LUA 学习全部内容,希望文章能够帮你解决【cocos2d x-3.3 rec】 LUA 学习所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)