Cocos實戰篇[3.2]——《戰神傳說》Lua版

【嘮叨】

    當時學Lua的時候,我將《戰神傳說》用Lua也寫了一遍。

    C++版的《戰神傳說》參考這篇:http://shahdza.blog.51cto.com/2410787/1549660


【源碼】

    https://github.com/shahdza/Cocos-Lua_Plane




【學習心得】


1、繼承自cc.Sprite後,設置自身紋理圖片的方式

	-- 【方式一】通過精靈幀設置
	cc.SpriteFrameCache:getInstance():addSpriteFrames("123.plist")
	local frame = cc.SpriteFrameCache:getInstance():getSpriteFrame("1.png")
	self:setSpriteFrame(frame)

	-- 【方式二】通過圖片紋理
	local texture = cc.Director:getInstance():getTextureCache():addImage("ship01.png")
	local frame = cc.SpriteFrame:createWithTexture(texture, cc.rect(0, 0, 60, 38))
	self:setSpriteFrame(frame)


2、混合模式

	-- local cbl = {GL_SRC_ALPHA, GL_ONE}
	self:setBlendFunc(GL_SRC_ALPHA, GL_ONE)


3、定時器

	-- 【默認定時器】
	self:scheduleUpdateWithPriorityLua(update, priority)
	self:unscheduleUpdate()

	-- 【自定義定時器】schedule
	local dt = 0
	function GameLayer:addUpdate()
		local function update(_dt) 
			dt = _dt 
		end
		self:scheduleUpdateWithPriorityLua(update, 0)
	end
	
	function GameLayer:xxxxx()
		local function func()
			print(dt)
		end
		schedule(self, func, 1.0 / 60.0)
	end


4、按鈕回調

	-- registerScriptTapHandler
	local function turnToLoadingScene(tag, sender)
		self:turnToLoadingScene()
	end

	local backlb = cc.Label:createWithBMFont("Font/bitmapFontTest.fnt", "Go Back")
	local pback = cc.MenuItemLabel:create(backlb)
	pback:registerScriptTapHandler(turnToLoadingScene)


5、觸摸事件

	-- registerScriptHandler
	-- 【單點觸摸】
	local dispatcher = self:getEventDispatcher()
	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)
	dispatcher:addEventListenerWithSceneGraphPriority(listener, self)
	-- 【吞噬觸摸】
	listener:setSwallowTouches(true)


6、單例類

	Effect = class("Effect", function()
		return cc.Node:create()
	end)

	local _effect = nil
	function Effect:getInstance()
		if nil == _effect then
			_effect = clone(Effect) -- 用clone() ,好像用.new()有問題
			_effect:init()
		end
		return _effect
	end


7、貝塞爾曲線運動

	local bezier = {
		cc.p(sgn * dx, 0),		-- controlPoint_1
		cc.p(sgn * dx, -dy),	-- controlPoint_1
		cc.p(0, -dy),			-- bezier.endPosition
	}
	local b0 = cc.BezierBy:create(dt, bezier)


8、動畫

	-- 【方式一】通過精靈幀創建
	local arr = {}
	for i=1, 34 do
		local str = string.format("explosion_%02d.png" , i)
		-- getSpriteFrameByName -> getSpriteFrame
		local frame = cc.SpriteFrameCache:getInstance():getSpriteFrame(str)
		table.insert(arr, frame)
	end
	local animation = cc.Animation:createWithSpriteFrames(arr, 0.02)
	cc.AnimationCache:getInstance():addAnimation(animation, "Explosion")
	
	-- 【方式二】通過圖片紋理
	local texture = cc.Director:getInstance():getTextureCache():addImage("ship01.png")
	local sp1 = cc.SpriteFrame:createWithTexture(texture, cc.rect(0, 0, 60, 38))
	local sp2 = cc.SpriteFrame:createWithTexture(texture, cc.rect(60, 0, 60, 38))
	local animation = cc.Animation:create()
	an:addSpriteFrame(sp1)
	an:addSpriteFrame(sp2)
	an:setDelayPerUnit(0.1)
	an:setRestoreOriginalFrame(true)
	self:runAction(cc.RepeatForever:create(cc.Animate:create(animation)))


9、背景滾動

	-- 【1】添加背景
	function GameLayer:addBG()
		self.bg1 = cc.Sprite:create("bg01.jpg")
		self.bg2 = cc.Sprite:create("bg01.jpg")
		self.bg1:setAnchorPoint(cc.p(0, 0))
		self.bg2:setAnchorPoint(cc.p(0, 0))
		self.bg1:setPosition(0, 0)
		self.bg2:setPosition(0, self.bg1:getContentSize().height)
		self:addChild(self.bg1, -10)
		self:addChild(self.bg2, -10)
	end
	-- 【2】背景滾動
	function GameLayer:moveBG()
		local height = self.bg1:getContentSize().height
		local function updateBG()
			self.bg1:setPositionY(self.bg1:getPositionY() - 1)
			self.bg2:setPositionY(self.bg1:getPositionY() + height)
			if self.bg1:getPositionY() <= -height then
				self.bg1, self.bg2 = self.bg2, self.bg1
				self.bg2:setPositionY(WIN_SIZE.height)
			end
		end
		schedule(self, updateBG, 0)
	end


10、物理碰撞

    > 碰撞事件 & 條件

(1)b1.CategoryBitmask 與 b2.ContactTestBitmask 進行按位與

(2)b2.CategoryBitmask 與 b1.ContactTestBitmask 進行按位與

只有(1)、(2)都不爲0的時候,觸發碰撞檢測事件。

    > 物體碰撞 & 條件

(1)b1.CategoryBitmask 與 b2.CollisionBitmask 進行按位與

(2)b2.CategoryBitmask 與 b1.CollisionBitmask 進行按位與

只有(1)、(2)都不爲0的時候,觸發物體碰撞。

	-- 設置場景物理信息
	scene:getPhysicsWorld():setGravity(cc.p(0, 0))
	scene:getPhysicsWorld():setDebugDrawMask(cc.PhysicsWorld.DEBUGDRAW_ALL)
	
	-- 創建物體
	b1 = cc.Sprite:create("123.png")
	-- 半徑、材質(密度、彈性、摩擦力)、偏移量
	local body = cc.PhysicsBody:createCircle(3, cc.PhysicsMaterial(0.1, 1, 0), cc.p(0, 16)) 
	b1:setPhysicsBody(body)
	b1:getPhysicsBody():setCategoryBitmask(2)
	b1:getPhysicsBody():setCollisionBitmask(3)
	b1:getPhysicsBody():setContactTestBitmask(12)
	
	-- 註冊碰撞事件
	local function onContactBegin(contact)
		local a = contact:getShapeA():getBody():getNode()
		local b = contact:getShapeB():getBody():getNode()
		if a ~= nil and b ~= nil then
			a:setPosition(0, 0)
			b:setPosition(0, 0)
		end
		return true
	end
	local dispatcher = self:getEventDispatcher()
	local contactListener = cc.EventListenerPhysicsContact:create()
	contactListener:registerScriptHandler(onContactBegin, cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN)
	dispatcher:addEventListenerWithSceneGraphPriority(contactListener, self)


11、程序結束

	-- 【方式一】
	cc.Director:getInstance():endToLua()

	-- 【方式二】
	os.exit(0)


12、問題集

	-- 【敵人管理器】
	-- 不加入層中,好像就調用不了enemyManager的方法
	self.enemyManager = EnemyManager:create(self)
	self:addChild(self.enemyManager)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章