初学cocos2dx lua

本文档详细介绍了使用 Cocos2d-x 游戏引擎进行游戏开发的基础知识,包括创建游戏层、场景、精灵等核心元素,设置定时器及触摸事件,播放背景音乐和音效,加载图片资源,实现基本动作动画,以及如何添加和处理按钮点击事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

From: http://blog.csdn.net/w18767104183/article/details/21177589

最基本的层

  1. function createInGameLayer()   
  2.         local inGameLayer = cc.Layer:create()  
  3.         return inGameLayer  
  4.     end  
function createInGameLayer() 
        local inGameLayer = cc.Layer:create()
        return inGameLayer
    end

最基本的场景

  1.  local sceneGame = cc.Scene:create()  
  2.     sceneGame:addChild(createInGameLayer())  
  3.   
  4. cc.Director:getInstance():runWithScene(sceneGame)  
  5. cc.Director:getInstance():replaceScene(cc.TransitionFade:create(1,WelcomeScene.createScene()))  
 local sceneGame = cc.Scene:create()
    sceneGame:addChild(createInGameLayer())

cc.Director:getInstance():runWithScene(sceneGame)
cc.Director:getInstance():replaceScene(cc.TransitionFade:create(1,WelcomeScene.createScene()))

最基本的精灵

  1. function createInGameLayer()   
  2.         local inGameLayer = cc.Layer:create()  
  3.         local bg = cc.Sprite:create("farm.jpg")  
  4.         bg:setAnchorPoint(0,0)  
  5.         inGameLayer:addChild(bg)  
  6.         return inGameLayer  
  7. end  
function createInGameLayer() 
        local inGameLayer = cc.Layer:create()
        local bg = cc.Sprite:create("farm.jpg")
        bg:setAnchorPoint(0,0)
        inGameLayer:addChild(bg)
        return inGameLayer
end

最基本的定时器
  1. local function tick()  
  2.             
  3.        end  
  4.   
  5.        cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick, 0, false)  
 local function tick()
           
        end

        cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick, 0, false)


最基本的触摸事件

  1. local touchBeginPoint = nil  
  2.         local function onTouchBegan(touch, event)  
  3.             local location = touch:getLocation()  
  4.             cclog("onTouchBegan: %0.2f, %0.2f", location.x, location.y)  
  5.             touchBeginPoint = {x = location.x, y = location.y}  
  6.             -- CCTOUCHBEGAN event must return true  
  7.             --[[多点  
  8.              for i = 1,table.getn(touches) do  
  9.              local location = touches[i]:getLocation()  
  10.              Sprite1.addNewSpriteWithCoords(Helper.currentLayer, location)  
  11.              end  
  12.             ]]--  
  13.             return true  
  14.         end  
  15.   
  16.         local function onTouchMoved(touch, event)  
  17.             local location = touch:getLocation()  
  18.             cclog("onTouchMoved: %0.2f, %0.2f", location.x, location.y)  
  19.             if touchBeginPoint then  
  20.                 local cx, cy = layerFarm:getPosition()  
  21.                 layerFarm:setPosition(cx + location.x - touchBeginPoint.x,  
  22.                                       cy + location.y - touchBeginPoint.y)  
  23.                 touchBeginPoint = {x = location.x, y = location.y}  
  24.             end  
  25.         end  
  26.   
  27.         local function onTouchEnded(touch, event)  
  28.             local location = touch:getLocation()  
  29.             cclog("onTouchEnded: %0.2f, %0.2f", location.x, location.y)  
  30.             touchBeginPoint = nil  
  31.             spriteDog.isPaused = false  
  32.         end  
  33.   
  34.         local listener = cc.EventListenerTouchOneByOne:create()  
  35.         --local listener = cc.EventListenerTouchAllAtOnce:create() 多点  
  36.         listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )  
  37.         listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )  
  38.         listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )  
  39.         local eventDispatcher = layerFarm:getEventDispatcher()  
  40.         eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layerFarm)  
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, %0.2f", location.x, 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, %0.2f", location.x, 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)

最基本的音乐
  1. --local bgMusicPath = CCFileUtils:getInstance():fullPathForFilename("background.ogg")  
  2.   
  3.    local bgMusicPath = cc.FileUtils:getInstance():fullPathForFilename("background.mp3")  
  4.   
  5.    cc.SimpleAudioEngine:getInstance():playMusic(bgMusicPath, true)  
  6.    local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")  
  7.   
  8.    cc.SimpleAudioEngine:getInstance():preloadEffect(effectPath)  
  9.   
  10.    local function menuCallbackOpenPopup()  
  11.            -- loop test sound effect  
  12.            local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")  
  13.            effectID = cc.SimpleAudioEngine:getInstance():playEffect(effectPath)  
  14.            menuPopup:setVisible(true)  
  15.        end  
 --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

最基本的加载图片
  1. cc.Director:getInstance():getTextureCache():addImageAsync("DartBlood.png",imageLoaded)  
  2. local texture0 = cc.Director:getInstance():getTextureCache():addImage( "Images/grossini_dance_atlas.png")  
  3.   
  4. function LoadingScene.imageLoaded( pObj)  
  5.     -- body  
  6. end  
  7.   
  8. cc.Director:getInstance():getTextureCache():removeTextureForKey("Images/grossinis_sister1-testalpha.png")  
  9. cc.Director:getInstance():getTextureCache():removeAllTextures()  
  10. cc.Director:getInstance():getTextureCache():removeUnusedTextures()  
  11.   
  12. local cache = cc.SpriteFrameCache:getInstance()  
  13. cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png")  
  14. SpriteFrameTest.m_pSprite1 = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png")  
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")

最基础的动作

  1. local function CallFucnCallback1()  
  2.       
  3. end  
  4.   
  5. local action = cc.Sequence:create(  
  6.         cc.MoveBy:create(2, cc.p(200,0)),  
  7.         cc.CallFunc:create(CallFucnCallback1) )  
  8.  grossini:runAction(action)  
local function CallFucnCallback1()
	
end

local action = cc.Sequence:create(
        cc.MoveBy:create(2, cc.p(200,0)),
        cc.CallFunc:create(CallFucnCallback1) )
 grossini:runAction(action)


最基础的字符格式化

  1. string.format("grossini_dance_%02d.png", j + 1)  
string.format("grossini_dance_%02d.png", j + 1)

最基础的按钮
  1. local start = cc.Sprite:createWithSpriteFrameName("start.png")  
  2.   
  3.   
  4. local  startItem = cc.MenuItemSprite:create(start, start, start)  
  5.   
  6.   
  7. local function menuCallback(sender)  
  8.     cclog("menuCallback...")  
  9.     --tolua.cast(ret:getParent(), "cc.LayerMultiplex"):switchTo(1)  
  10. end  
  11.   
  12.   
  13. startItem:registerScriptTapHandler(menuCallback)  
  14. startItem:setPosition(50,50)  
  15.   
  16.   
  17. local  menu = cc.Menu:create()  
  18. menu:addChild(startItem)  
  19. menu:setPosition(0,0)  
  20. layer:addChild(menu) 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值