Cocos2d-x lua开发微信飞机大战

0. 简介

这是微信飞机大战游戏的cocos2d-x lua中的实现,完成了精灵的创建,动画,碰撞检测等内容。

1. 完整代码和图片资源

https://github.com/kompasim/wechat-plane-wars

2. 最终效果

Cocos2d-x lua开发微信飞机大战_第1张图片
image.png

3. 主要逻辑代码

local GameScene = class("GameScene", cc.load("mvc").ViewBase);


function GameScene:onCreate()

    self.bullets = {}
    self.enemies = {}
    self.ufos = {}
    self.plane = nil
    
    display.newSprite("./image/background.png")
        :move(display.center)
        :addTo(self)

    local plane = display.newSprite("./image/hero2.png")
    plane:move(display.cx, display.cy - 300)
    plane:addTo(self)
    self.plane = plane
    


    local function onTouchBegan(touch, event)
        print("onTouchBegan ...")
        if (touch:getLocation().x >= (plane:getPositionX() - 50) and touch:getLocation().x <= (plane:getPositionX() + 50)) and (touch:getLocation().y >= (plane:getPositionY() - 50) and touch:getLocation().y <= (plane:getPositionY() + 50)) then
            self.follow = true
            self.fire = true
            -- 
            local image1 = cc.Director:getInstance():getTextureCache():addImage(cc.FileUtils:getInstance():fullPathForFilename("./image/hero1.png"))
            self.plane:setTexture(image1)  
        end
        return true
    end
    local function onTouchMoved(touch, event)
        if self.follow == true then
            if touch:getLocation().x >= 0 and touch:getLocation().x <= display.width then
                plane:setPositionX(touch:getLocation().x)
            else
                if touch:getLocation().x <= 0 then
                    plane:setPositionX(0)
                else
                    plane:setPositionX(display.width)
                end
            end
            if touch:getLocation().y >= 0 and touch:getLocation().y <= display.height then
                plane:setPositionY(touch:getLocation().y)
            else
                if touch:getLocation().y <= 0 then
                    plane:setPositionY(0)
                else
                    plane:setPositionY(display.height)
                end
            end
        end
        return true
    end
    local function onTouchEnded(touch, event)
        print("onTouchEnded ...")
        if self.follow and self.fire then
            self.follow = false
            self.fire = false
            -- 
            local image2 = cc.Director:getInstance():getTextureCache():addImage(cc.FileUtils:getInstance():fullPathForFilename("./image/hero2.png"))
            self.plane:setTexture(image2)
            print("children of gameScene : " .. self:getChildrenCount())
        end
        return true
    end
    local eventListener = cc.EventListenerTouchOneByOne:create()
    eventListener:setSwallowTouches(true)
    eventListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
    eventListener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED)
    eventListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED)
    self:getEventDispatcher():addEventListenerWithSceneGraphPriority(eventListener, self)

    local scheduler = cc.Director:getInstance():getScheduler()
    scheduler:scheduleScriptFunc(function()
        self:fireFun()
    end, 0.1, false)
    scheduler:scheduleScriptFunc(function()
        self:attackFun()
    end, 0.5, false)
    scheduler:scheduleScriptFunc(function()
        self:checkTheGame()
    end, 0.1, false)
    scheduler:scheduleScriptFunc(function()
        self:makeUfo()
    end, 5.0, false)

end

function GameScene:fireFun()
    if self.fire then
        local bullet = display.newSprite("./image/bullet1.png")
        bullet:move(self.plane:getPositionX(), self.plane:getPositionY() + 70)
        bullet:setScaleX(0.5)
        bullet:addTo(self)
        self.bullets[tostring(bullet)] = bullet
        -- 
        local action = cc.MoveBy:create(2, cc.p(0, 2000))
        transition.execute(bullet, action, {onComplete = function()
            self:removeChild(bullet, true)
            self.bullets[tostring(bullet)] = nil
            bullet = nil
        end})
    end
end

function GameScene:attackFun()
    for i=1,3 do
        local enemy = display.newSprite("./image/enemy1.png")
        enemy:move(math.random(0, display.width), display.height)
        enemy:addTo(self)
        self.enemies[tostring(enemy)] = enemy
        --
        local action = cc.MoveBy:create(10, cc.p(math.random(-200*i, 200*i),-2000))
        transition.execute(enemy, action, {onComplete = function()
            self:removeChild(enemy, true)
            self.enemies[tostring(enemy)] = nil
            enemy = nil
        end})
    end
end


function GameScene:makeUfo()
    local ufo = display.newSprite("./image/ufo2.png")
    if math.random( 1, 2) == 1 then 
        ufo = display.newSprite("./image/ufo1.png")
    end
    ufo:move(math.random(0, display.width), display.height)
    ufo:addTo(self)
    self.ufos[tostring(ufo)] = ufo
    -- 
    local action = cc.MoveBy:create(20, cc.p(0, -2000))
    transition.execute(ufo, action, {onComplete = function()
        self:removeChild(ufo, true)
        self.ufos[tostring(ufo)] = nil
        ufo = nil
    end})
end


function GameScene:checkTheGame()
    -- print("check ...")
    local px = self.plane:getPositionX()
    local py = self.plane:getPositionY()
    -- enemy
    for ek,ev in pairs(self.enemies) do
        if type(ev) == "userdata" then
            local ex = ev:getPositionX()
            local ey = ev:getPositionY()
            -- plane hit to the enemy
            if ex - px <= 60 and ex - px > -60 and ey - py < 60 and ey -py > -60 then
                self:removeChild(ev)
                self.enemies[ek] = nil
            end
            -- bullet hit to the enemy
            for bk,bv in pairs(self.bullets) do
                if type(bv) == "userdata" then
                    local bx = bv:getPositionX()
                    local by = bv:getPositionY()
                    if bx - ex <= 25 and bx - ex > -25 and by - ey < 25 and by -ey > -25 then
                        -- 
                        self:removeChild(bv)
                        self.bullets[bk] = nil
                        -- 
                        local image2 = cc.Director:getInstance():getTextureCache():addImage(cc.FileUtils:getInstance():fullPathForFilename("./image/enemy1_down2.png"))
                        ev:setTexture(image2)
                        local action2 = cc.RotateBy:create(0.1, 90)
                        transition.execute(ev, action2, {onComplete = function()
                            self:removeChild(ev)
                            self.enemies[ek] = nil
                        end})
                    end
                end
            end
        end
    end
    -- ufo
    for uk,uv in pairs(self.ufos) do
        if type(uv) == "userdata" then
            local ux = uv:getPositionX()
            local uy = uv:getPositionY()
            -- plane hit to the enemy
            if ux - px <= 60 and ux - px > -60 and uy - py < 60 and uy -py > -60 then
                self:removeChild(uv)
                self.ufos[uk] = nil
            end
            -- bullet hit to the enemy
            for bk,bv in pairs(self.bullets) do
                if type(bv) == "userdata" then
                    local bx = bv:getPositionX()
                    local by = bv:getPositionY()
                    if bx - ux <= 25 and bx - ux > -25 and by - uy < 25 and by -uy > -25 then
                        -- -- 
                        self:removeChild(bv)
                        self.bullets[bk] = nil
                        -- 
                        local image2 = cc.Director:getInstance():getTextureCache():addImage(cc.FileUtils:getInstance():fullPathForFilename("./image/ufo_down.png"))
                        uv:setTexture(image2)
                        local action2 = cc.RotateBy:create(0.1, 90)
                        transition.execute(uv, action2, {onComplete = function()
                            self:removeChild(uv)
                            self.ufos[uk] = nil
                        end})
                        break
                    end
                end
            end
        end
    end

    
end


return GameScene

你可能感兴趣的:(Cocos2d-x lua开发微信飞机大战)