lua写项目常用知识点

xiaoxiao2021-02-28  53

-- 跳转场景                 local StartScene = require("app.views.StartScene")

local trans = cc.TransitionPageTurn:create(1,StartScene.create(),false)

local trans=Game.create()

cc.Director:getInstance():replaceScene(trans)

开始场景都写

local MainScene= class("MainScene",function ( ) return cc.Scene:create();end)function MainScene.create( ) local scene = cc.Scene:create(); -- local scene = cc.MainScene.new() scene:addChild(MainScene:createLayer()) return scene;endfunction MainScene:createLayer(  ) local layer = cc.Layer:create()。。。。 return layerend

return MainScene;

-- 获取整体父节点local root = cc.CSLoader:createNode("Layer10.csb")

layer:addChild(root)

local   btn1=root:getChildByName("Button_1");

btn1:addClickEventListener(function ( psender )

end)

手写按钮  一般就是加cc,ui控件就加ccui

local bg = ccui.Button:create("backarrow.png")bg:setAnchorPoint(cc.p(0,1))bg:setPosition(cc.p(0,320))bg:setScale(0.6)

layer:addChild(bg,5)

模态对话框中的触摸吞噬

local widget = ccui.Widget:create();

   widget:setAnchorPoint(cc.p(0, 0));    widget:setPosition(cc.p(0, 0));    widget:setContentSize(cc.size(480,320));    widget:setTouchEnabled(true);//设置是否启用触摸事件    widget:setSwallowTouches(true);//设置是否启用触摸吞噬    layer:addChild(widget,6);

粒子效果

self.snow=cc.ParticleSnow:create()        self.snow:setPosition(cc.p(display.cx,display.top))        layer:addChild( self.snow,3,1)        self.isMusic=true        self.isMusicOn=true

        self.volic=5

Lua复选框 和c++复选框  监听事件写法不太一样

 local checkbox1 = node:getChildByName("CheckBox_1")            checkbox1:addEventListener(function ( sender,eventType)            -- 0==ccui.CheckBoxEventType.selected            -- 1==ccui.CheckBoxEventType.unselected            if eventType==0 then            cc.SimpleAudioEngine:getInstance():resumeBackgroundMusic()                                           elseif   eventType==1 then                  cc.SimpleAudioEngine:getInstance():pauseBackgroundMusic()              end            end)

main.lua这样设置

cc.FileUtils:getInstance():setPopupNotify(false)require "config"require "cocos.init"local MainScene = require("app.views.MainScene")local function main()    -- require("app.MyApp"):create():run()    cc.Director:getInstance():runWithScene(MainScene.create())endlocal status, msg = xpcall(main, __G__TRACKBACK__)if not status then    print(msg)end

转载请注明原文地址: https://www.6miu.com/read-2630849.html

最新回复(0)