开始接触 Cocos2dx3.15 - Lua

前端之家收集整理的这篇文章主要介绍了开始接触 Cocos2dx3.15 - Lua前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

话不多言,干货已经备全@H_502_2@

mai.lua@H_502_2@

cc.FileUtils:getInstance():setPopupNotify(false)

require "config"
require "cocos.init"

local function main()
    --require("app.MyApp"):create():run()
    local scene = require("MainMenu"):create()
    cc.Director:getInstance():runWithScene(scene)
end

local status,msg = xpcall(main,__G__TRACKBACK__)
if not status then
    print(msg)
end

MainMenu.lua
local MainMenu = class("MainMenu",function ()
    return cc.Scene:create()
end)

function MainMenu.create()
    local scene = MainMenu.new()
    return scene
end

function MainMenu:ctor()
    print("MainMenu function ctor")
    self:init()
end

function MainMenu:init()
    print("MainMenu function init")

    -- 创建层
    self:showUI()

    -- 创建按钮
    self:addButton()
end

function MainMenu:showUI()
    -- 添加图片
    local winSize = cc.Director:getInstance():getVisibleSize()
    local bg = cc.Sprite:create("HelloWorld.png")
    bg:setPosition(cc.p(winSize.width/2,winSize.height/2))
    self:addChild(bg)

    -- 添加文字
    local label = cc.Label:createWithSystemFont("Hello Lua","Arial",40)
    self:addChild(label)
    label:setPosition(cc.p(winSize.width/2,winSize.height/2))
end

function MainMenu:addButton()
    local winSize = cc.Director:getInstance():getWinSize()
    local spacing = 100;

    -- MenuItemFont
    local menuItemFont = cc.MenuItemFont:create("btnStart")
    menuItemFont:setPosition(-winSize.width/2 + spacing,-winSize.height/2 + spacing)  
    local function menuItemFontCallback(sender)  
        print("menuItemFontCallback")  
    end
    menuItemFont:registerScriptTapHandler(menuItemFontCallback)  

    -- MenuItemImage
    local normal = cc.MenuItemImage:create("btn_green.png","btn_blue.png")
    normal:setPosition(-winSize.width/2 + spacing,-winSize.height/2 + spacing * 2)
    normal:setTag(1)
    local function menuCallback(tag,menuItem)
        print("menuItem:" .. tag)
    end
    normal:registerScriptTapHandler(menuCallback)

    -- MenuItemSprite
    local normalSprite = cc.Sprite:create("btn_blue.png")
    local selectedSprite = cc.Sprite:create("btn_green.png")
    local menuItemSprite = cc.MenuItemSprite:create(normalSprite,selectedSprite)
    menuItemSprite:setPosition(-winSize.width/2 + spacing,-winSize.height/2 + spacing * 3)
    local function menuItemSpriteCallback(sender)
        print("menuItemSpriteCallback")
    end
    menuItemSprite:registerScriptTapHandler(menuItemSpriteCallback)

    --MenuItemToggle  
    local onMenuItem = cc.MenuItemImage:create("mute_bg_on.png","mute_bg_on.png")  
    local offMenuItem = cc.MenuItemImage:create("mute_bg_off.png","mute_bg_off.png")  
    local menuItemToggle = cc.MenuItemToggle:create(onMenuItem,offMenuItem)  
    menuItemToggle:setPosition(-winSize.width/2 + spacing,-winSize.height/2 + spacing * 4)  
    local function menuItemToggleCallback(sender)  
        print("menuItemToggleCallback")  
    end  
    menuItemToggle:registerScriptTapHandler(menuItemToggleCallback)

    local menu = cc.Menu:create(menuItemFont,normal,menuItemSprite,menuItemToggle)
    menu:setPosition(cc.p(winSize.width/2,winSize.height/2))
    self:addChild(menu)
end

function MainMenu:onButtonClicked()
    
end

return MainMenu

运行结果:


@H_502_2@


@H_502_2@

从最初的C++ 语言编程,到开始接触 Lua, 还真是有许多的不适应:@H_502_2@

1.书写代码没有调用接口的提示功能@H_502_2@

2.接口参数不能确定数量及其类型,徐查找C++端接口代码@H_502_2@

3.无法使用断点调试,只能通过打印日志定位问题@H_502_2@


@H_502_2@

虽然有很多问题相较于以前的语言编程,但为啥还要用 Lua 脚本呢, 说到脚本,当然, Lua 有自己的有点:@H_502_2@

1.Lua作为轻量级脚本语言,便于上手开发@H_502_2@

2.最重要的是,使用Lua 便于实现热更新@H_502_2@

原文链接:https://www.f2er.com/cocos2dx/338385.html

猜你在找的Cocos2d-x相关文章