标签:quick-cocos2d-x3.2 游戏 lua quick 简单
经典的入门小游戏,这里用quick-cocos2d-x3.2重新写一遍,以便熟悉下quick
首先,创建工程,如果不会自行百度啊。
1、编译效果如下:
2、将游戏背景设置为白色,同时我们也来看一下MainScene.lua的文件
local MainScene = class("MainScene", function() return display.newScene("MainScene") end) -- 相当于C++中的构造函数 function MainScene:ctor() -- 将背景设置为白色,而且这个作为当前场景的层,以便以后实现触摸事件 self.touchLayer_ = display.newColorLayer(cc.c4b(255,255,255,255)):addTo(self) end function MainScene:onEnter() end function MainScene:onExit() end return MainScene
添加忍者将其放在屏幕左边中间,以及添加每帧刷新的方法。在ctor方法的后面添加如下代码。
--添加忍者精灵 self.player_ = display.newSprite("Player.png"):addTo(self.touchLayer_) self.player_:pos(self.player_:getContentSize().width / 2 , display.height / 2) -- 初始化存储飞镖和敌人(怪物)的数组 self.projectiles_ = {} self.monsters_ = {} -- 添加每帧刷新的方法 self:addNodeEventListener(cc.NODE_ENTER_FRAME_EVENT,handler(self , self.update)) self:scheduleUpdate()
--添加敌人 function MainScene:addMonster() end function MainScene:onTouchEnded(x , y) end --每帧刷新 function MainScene:update(dt) print("update") end
添加每秒调用的方法。在MainScene.lua的最上面添加如下代码
local scheduler = require(cc.PACKAGE_NAME .. ".scheduler")
-- 添加一个每秒刷新的方法 scheduler.scheduleGlobal(function() self:addMonster() --每秒调用的方法 end , 1) -- 1 为时间
--添加敌人 function MainScene:addMonster() local monster = display.newSprite("monster.png"):addTo(self.touchLayer_) local minY = monster:getContentSize().height / 2 local maxY = display.height - monster:getContentSize().height / 2 local rangeY = maxY - minY local actualY = math.random(rangeY) monster:pos(display.width + monster:getContentSize().width / 2 , actualY) local minDuration = 2.0 local maxDuration = 4.0 local rangeDuration = maxDuration - minDuration local actualDuration = (math.random(rangeDuration)) transition.moveTo(monster,{ x = -monster:getContentSize().width / 2, y = actualY , time = actualDuration, onComplete = function (event) --执行结束后删除 self.monsters_[event] = nil event:removeSelf() end }) -- 放入 存放怪物的数据 以后以便删除 self.monsters_[monster] = monster end
添加触摸事件,在ctor方法的最下面
--添加触摸事件 self.touchLayer_:setTouchEnabled(true) self.touchLayer_:addNodeEventListener(cc.NODE_TOUCH_EVENT,function(event) if event.name == "ended" then self:onTouchEnded(event.x,event.y) end return true end)
function MainScene:onTouchEnded(x , y) print("onTouchEnded") local location = cc.p(x,y) local projectile = display.newSprite("Projectile.png"):addTo(self) projectile:pos(20 , display.height / 2) local px , py = projectile:getPosition() local offset = cc.pSub(location,cc.p(px,py)) if (offset.x <= 0) then return end local realX = display.width + projectile:getContentSize().width / 2 local ratio = offset.y / offset.x local realY = realX * ratio +py local realDest = cc.p(realX,realY) local offRealX = realX - px local offRealY = realY - py local length = math.sqrt(offRealX * offRealX + offRealY * offRealY) local velocity = 480 / 1 local realMoveDuration = length / velocity transition.moveTo(projectile,{ x = realDest.x , y = realDest.y , time = realMoveDuration , onComplete = function (event) self.projectiles_[event] = nil event:removeSelf() end }) self.projectiles_[projectile] = projectile end
-- 求两点间距离 local function dist(ax, ay, bx, by) local dx, dy = bx - ax, by - ay return math.sqrt(dx * dx + dy * dy) end --每帧刷新 function MainScene:update(dt) -- print("update") local projectilesToDelete = {} for k_pro, v_pro in pairs(self.projectiles_) do local monstersToDelete = {} for k_mon , v_mon in pairs(self.monsters_) do local p_x , p_y = v_pro:getPosition() local m_x,m_y = v_mon:getPosition() if dist(p_x,p_y,m_x,m_y) <= 30 then monstersToDelete[v_mon] = v_mon projectilesToDelete[v_pro] = v_pro end end for k_m_d , v_m_d in pairs(monstersToDelete) do -- local x , y = v_m_d:getPosition() self.monsters_[v_m_d] = nil v_m_d:removeSelf() end monstersToDelete = nil end for k_p_d , v_p_d in pairs(projectilesToDelete) do self.projectiles_[v_p_d] = nil v_p_d:removeSelf() end projectilesToDelete = nil end
代码下载:http://download.csdn.net/detail/qqmcy/8087361
原文地址:http://blog.csdn.net/qqmcy/article/details/40508873
标签:quick-cocos2d-x3.2 游戏 lua quick 简单
原文地址:http://blog.csdn.net/qqmcy/article/details/40508873