cocos2d-lua

創建工程

  1. 用cocos直接創建
    在這裏插入圖片描述
  2. cmd命令行創建
1.cd  C:\Cocos\Cocos2d-x\cocos2d-x-3.10\tools\cocos2d-console\bin
2.python cocos.py new 遊戲名 -p ID號 -l lua -d 遊戲路徑

載入場景

--class("PlayScene",...)PlayScene自己定義的類名
local PlayScene = class("PlayScene", cc.load("mvc").ModuleBase)--繼承類MoubleBase
PlayScene.RESOURCE_FILENAME = "game/Pork/PlayScene.csb"--載入cocos studio設置的場景 csb格式

讀取cocos studio中場景控件

function PlayScene:onCreate()  
     self.button=self.mView["button"]
     --變量名 = self.mView["cocos sdudio中設置的控件名"]
end

require

  1. 加載指定的模塊(類似#include),加載了該模塊,那麼就可已使用模塊中的全局函數和全局數據
  2. require 用於搜索 Lua 文件的路徑是存放在全局變量 package.path 中
  3. import() 與 require() 功能相同,但具有一定程度的自動化特性。
--直接 切換場景
require("logic.game.Pork.PlayScene"):create():showWithScene()
--讀取這個路徑下文件,並賦值給前面的變量 
local playscene = require("logic.game.Pork.PlayScene")
playScene.new:showWithScene()--切換
  1. 具體可見菜鳥教程 http://www.runoob.com/lua/lua-modules-packages.html

Lua中函數使用:

定義方式:

1.  tab.func=function ( 參數)
     end
2. function tab.func( 參數)
   end
3.  function tab:func( 參數) 
     end

調用方式:

  1. b.func(參數)
  2. tab:func(參數)

ps:調用方式可採用self:func()
如果用self.func() 會報錯,正確用法self.func(self)

moveTo & moveBy

存在座標pos1(x1,y1),pos2(x2,y2)
cc.MoveTo:由pos1移動到pos2
cc.MoveBy:終座標pos3=pos1+pos2 (相對偏移向量)

進度條的屬性

  1. left:setMidpoint(cc.p(0, 0))
    setMidpoint()函數是設置進度條的起始點
    (0,y)表示最左邊
    (1,y)表示最右邊
    (x,1)表示最上面
    (x,0)表示最下面
  2. right:setBarChangeRate(cc.p(1, 0))
    setBarChangeRate()函數是用來設置進度條動畫方向的
    (1,0)表示橫方向
    (0,1)表示縱方向
  3. 通過cocos studio中控件直接設置
self.loadingBar:setPercent(100-count)
  1. 具體可見 https://blog.csdn.net/tropicofcancer9/article/details/80018091
    https://www.2cto.com/kf/201407/317409.html

貝塞爾曲線 + 翻轉

在這裏插入圖片描述

 local flipxAction = cc.FlipX:create(true)    
 --參數(時間,{起點,控制點、終點})
 local moveTo1 = cc.BezierTo:create(10, {cc.p(sprites:getPosition()), cc.p(spritePointS[1], spritePointS[2]), cc.p(spriteFinalS[1], spriteFinalS[2])})    
 local moveTo2 = cc.BezierTo:create(10, {cc.p(sprites:getPosition()), cc.p(spritePoint[1], spritePoint[2]), cc.p(spriteFinal[1], spriteFinal[2])})     
 local action = cc.Sequence:create(moveTo1, flipxAction,  moveTo2, flipxAction) 

畫直線

local draw = cc.DrawNode:create()
:addTo(self)
draw:drawLine(cc.p(0,0),cc.p(1000,1000),cc.c4f(0,0,0,5))
--參數(起點,終點,線條顏色)

旋轉

--以錨點爲中心1s旋轉360度
local run = cc.RotateBy:create(1,360)
local action = cc.RepeatForever:create(run)
self.sprite:runAction(action)

--停止所有動作
self.sprite:stopAllActions()

讀取文件內容

function playScene:ReadScore(path)   
 --r 讀取/ a 追加/ w 寫/ b 打開二進制 /r+ 更新,之前所有數據將保存 / w+ 更新,之前所有數據將被清除 /a+ 文件末尾添加    
  	local file = io.open(path,"r")     
 	if file then        
 		local content = file:read("*a")        
 		io.close(file)        
 		return content    
 	end    
 	return 0 
 end

將內容寫入文件

function playScene:WriteScore(path, content ,mode)    
	local file = io.open(path, mode)    
	if file then        
		if file:write(content) == nil then            
			return false        
		end        
		io.close(file)        
		return true    
	else         
		return false    
	end 
end

UserDefault (key-value)

local file  = cc.UserDefault:getInstance()
local a = file:getIntegerForKey("key")
print(a)
file:setIntegerForKey("key", value)
local a = file:getIntegerForKey("key")
print(a)

滑動條 (坑)

-- --滑動條的使用   控制音量 失敗   
local function onChangedSlider(sender,eventType)  --回調函數  (滑塊對象, 事件類型)    
	local slider = sender    
	if eventType == ccui.SliderEventType.percentChanged then  --滑塊值改變事件  
		local percent = slider:getPercent()  --獲取音量 
		audio.stopMusic()
		audio.setMusicVolume(0)  --?????????  
		print(percent)
	end   
end  
self.audioSlider:addEventListener(onChangedSlider)

計時器的使用

  1. 方法1(切換場景時存在一些問題)
local director = cc.Director:getInstance()
local function update(dt)
  ......
end    
director:getScheduler():scheduleScriptFunc(update,1,false) 
  1. 方法2(推薦)
function PlayScene:onCreate()   
    self:onUpdate(handler(self,self.update)) --刷新時間
    local count = 0
end

function PlayScene:update(dt)		
    count = count + dt
    print(count)
end

序列幀動畫的播放

    self.fishgroup = cc.CSLoader:createNode("res/game/try/Node.csb")        
    :addTo(self)    
    local action   = cc.CSLoader:createTimeline("res/game/try/Node.csb")   
    self.fishgroup:runAction(action)    
    action:gotoFrameAndPlay(0,60,true)   --0-60幀  true循環

觸摸事件

--觸摸回調函數
local function onTouchBegan( touch, event )                
end
local function onTouchEnded( touch, event )       
end       
local function onTouchMoved(touch, event)       
end        
local listener1 = cc.EventListenerTouchOneByOne:create()  --創建單點觸摸事件監聽        
listener1:setSwallowTouches(false)  --是否向下傳遞        
--註冊觸摸事件
listener1:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )  
listener1:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )  
listener1:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )        
local eventDispatcher = self:getEventDispatcher()         
eventDispatcher:addEventListenerWithFixedPriority(listener1,-1) --分發監聽事件        
self._listener = listener1

彈幕lua

https://blog.csdn.net/u011755031/article/details/50931089

遇到的問題

  1. 使用cocos studio創建ui時:
    路徑發步與打包必須時res中,否則後續會導致很多錯誤,這步非常關鍵
  2. 關於世界座標和本地座標的轉換失敗的情況,暫時我也沒弄懂,等我弄懂了就來補坑,目前我的解決方法是不要設置場景元素的父子節點,直接全都放在世界場景中。
  3. 瞬時動作和延時動作在刷新時間函數的時候可能會產生衝突,可用Sequence解決
local action = cc.Sequence:create(... , ...)
...:runAction(action)
  1. UserDefault數據未永久保存,可能是文檔裏面缺少root
    在這裏插入圖片描述
    在這裏插入圖片描述
發佈了31 篇原創文章 · 獲贊 1 · 訪問量 5891
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章