【cocos2d-x lua】飞机大战小游戏3:主角飞机

【cocos2d-x lua】飞机大战小游戏3:主角飞机

创建好背景就该创建主角飞机了。



一、创建主角飞机层

和背景层类似,创建PlaneLayer。
为了实现主角飞机的动态飞行效果,为主角飞机设置了两帧,创建序列帧动画循环播放。

local PlaneLayer = class("PlaneLayer",function ()
    local planeLayer = display.newLayer()
	local spriteFrame = cc.SpriteFrameCache:getInstance()
    -- 添加plist
    spriteFrame:addSpriteFrames("ui/shoot.plist","ui/shoot.png")
	local plane = cc.Sprite:createWithSpriteFrameName("hero1.png")
	plane:setPosition(cc.p(display.cx,plane:getContentSize().height/2))

	planeLayer:addChild(plane)
	planeLayer.plane = plane

	-- 设置plane序列帧动画
	local blink = CCBlink:create(1,3)
	local animation = CCAnimation:create()
	animation:setDelayPerUnit(0.1)
	animation:addSpriteFrame(spriteFrame:getSpriteFrame("hero1.png"))
	animation:addSpriteFrame(spriteFrame:getSpriteFrame("hero2.png"))
	local animate = CCAnimate:create(animation)
	
	plane:runAction(blink)
	plane:runAction(CCRepeatForever:create(animate))
	
    return planeLayer 
end)

return PlaneLayer

二、添加主角飞机层

在游戏页面GameScene添加PlaneLayer层

local PlaneLayer = import(".layers/PlaneLayer")
 	-- 创建plane层
    local plane = PlaneLayer.new()
    self:addChild(plane)
    self.plane = plane

三、实现主角飞机触摸移动

创建一个单点触摸事件,在plane的范围内点击触发触摸事件,在移动过程中实时更新plane位置,并且检测边框位置,使飞机无法移动至屏幕外。

	-- 移动plane
    local function onTouchBegan( touch, event )
        local point = touch:getLocation()      --获取鼠标坐标
        local rect = plane.plane:getBoundingBox()    --  获取plane的范围   
        if (cc.rectContainsPoint(rect,point)) then   --判断鼠标是否在plane范围内,是触发
            return true
        end
        return false    --不在范围内不触发
    end
    local function onTouchEnded( touch, event )
    end
 
    local function onTouchMoved(touch, event)
        local target = plane.plane  --获取当前plane
        local posX,posY = target:getPosition()  --获取当前的位置
        local delta = touch:getDelta() --获取滑动的距离
        local visibleSize = CCDirector:getInstance():getVisibleSize()
        local contentSize = target:getContentSize()
        posX = posX + delta.x
        posY = posY + delta.y
        if posX > visibleSize.width - contentSize.width / 2 then
            posX = visibleSize.width - contentSize.width / 2
        end
        if posX < contentSize.width / 2 then
            posX = contentSize.width / 2
        end
        if posY > visibleSize.height - contentSize.height / 2 then
            posY = visibleSize.height - contentSize.height / 2
        end
        if posY < contentSize.height / 2 then
            posY = contentSize.height / 2
        end
        target:setPosition(cc.p(posX ,posY)) --重新设置plane的位置
    end
 
    local listener1 = cc.EventListenerTouchOneByOne:create()  --创建一个单点事件监听
    listener1:setSwallowTouches(true)  --是否向下传递
    listener1:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
    listener1:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
    listener1:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
    local eventDispatcher = self:getEventDispatcher() 
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener1, plane.plane)  --分发监听事件


总结

至此,主角飞机创建完毕,玩家能够在游戏页面移动飞机。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值