local scheduler = require(cc.PACKAGE_NAME .. ".scheduler") 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) --添加忍者精灵 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() -- 添加一个每秒刷新的方法 scheduler.scheduleGlobal(function() self:addMonster() --每秒调用的方法 end , 1) -- 1 为时间 --添加触摸事件 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) end --添加敌人 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 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 function MainScene:onEnter() end function MainScene:onExit() end return MainScene
时间: 2024-10-24 13:33:07