quick cocos naja

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-12-25 12:46:54

quick cocos naja的相关文章

quick cocos 或者 Cocos2dx 项目下的Android.mk文件的学习

android.mk文件的作用:编译需要的cpp文件,生成.so动态库,供android端调用. 先上一个android.mk文件: 第一次创建项目,在Android平台编译时,都需要通过android.mk文件编译整个cocos2dx的库(第一次编译我们需要等待很长的时间.....). 首先知道$(call import-module,dir_name)的作用,然后顺着lib/proj.android目录继续找对应目录下的android.mk文件 类似于递归一样,把所有目录下的android.

quick cocos UIListView之isItemInViewRect方法修正

功能描述:一个滚动列表,当列表可视区域上部有内容时则上部出现向上箭头提示,当列表可视区域下部有内容则下部出现向下箭头提示. 功能实现:应用cocos studio1.6制作界面,上面放置一个背景,一个滚动列表,然后程序加载解析这个界面的json文件,应用quick3.3final下的UIListView的方法isItemInViewRect进行检测第一条与最后一条是否在可视区域内. 问题:当界面加载进来,坐标设置0,0时,isItemInViewRect方法判断都没问题,但当把界面调整位置时,i

quick cocos 的scheduler 定时器

cocos2dx原生lua对于定时器的写法: 1.每帧调用: void scheduleUpdateWithPriority(int priority) void scheduleUpdateWithPriorityLua (int nHandler,int priority) 2.指定调用间隔时间的: unsigned int scheduleScriptFunc (unsigned int nHandler, float fInterval, bool bPaused) 3.取消定时器事件

quick cocos table

local scheduler = require(cc.PACKAGE_NAME .. ".scheduler") local MainScene = class("MainScene", function() return display.newScene("MainScene") end) function MainScene:ctor() --add Plist display.addSpriteFrames("bandit.p

quick cocos scheduler update

local scheduler = require(cc.PACKAGE_NAME .. ".scheduler") local MainScene = class("MainScene", function() return display.newScene("MainScene") end) function MainScene:ctor() self:addNodeEventListener(cc.NODE_ENTER_FRAME_EVEN

quick cocos schedule

local scheduler = require(cc.PACKAGE_NAME .. ".scheduler") local MainScene = class("MainScene", function() return display.newScene("MainScene") end) function MainScene:ctor() scheduler.scheduleGlobal(function() self:foo() --每

quick cocos touch

local TestScene = class("TestScene", function() return display.newScene("TestScene") end) function TestScene:ctor() local layer = display.newLayer():addTo(self) layer:setTouchEnabled(true) layer:setTouchMode(cc.TOUCH_MODE_ONE_BY_ONE) l

quick cocos sprite 3 zhongfangshi

local TestScene = class("TestScene", function() return display.newScene("TestScene") end) function TestScene:ctor() display.newSprite("hero1.png",display.cx/2,display.cy):addTo(self) display.addSpriteFrames("bandit.plist

quick cocos animate2

local MainScene = class("MainScene", function() return display.newScene("MainScene") end) function MainScene:ctor() --单张的不可以 display.addSpriteFrames("bandit.plist", "bandit.png") --添加帧缓存 local sp = display.newSprite