飞机大战之四:飞机跟随触屏手指移动

单点触屏事件参考网址:http://blog.csdn.net/sdhjob/article/details/8176588

飞机部分参考网址:http://blog.csdn.net/jackystudio/article/details/11860007


源码下载地址:点击打开链接

关于svn的简单使用:点击打开链接

一:单点触屏事件。

        智能手机中玩家主要是通过触摸屏幕、重力感应与游戏互动的。先了解下触屏事件。在cocos2d中对应的有

CCStandradTouch(多点触摸),CCTargetedTouch(单点触摸)两个类。这里只介绍CCTargetedTouch类。

        一个层接受并处理触摸消息要如下步骤:

1 ,在初始化阶段将该层的触摸响应打开 setTouchEnabled(true); //开启触屏响应

2 ,重载虚函数 registerWithTouchDispatcher(void);  因为默认的触屏响应是CCStandradTouch,只需在函数中

        添加语句。

    CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,0, true)
3 ,重载以下触摸响应函数

    ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); // 开始按下
    ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);  //滑动
    ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);  //抬起
需要注意的事第一个函数是必须实现的,否则点击会抛出异常

二:飞机大战代码部分。

在GameLayer.h文件中添加如下方法的声明

    bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
    void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
    void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
    void registerWithTouchDispatcher();

并在GameLayer.cpp文件中实现以上声明

bool GameLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
    return true;
}

void GameLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{

}

void GameLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
    if (this->planeLayer->isAlive)
    {
        CCPoint beginPoint = pTouch->getLocationInView();
        beginPoint = CCDirector::sharedDirector()->convertToGL(beginPoint);
        CCRect planeRect = this->planeLayer->getChildByTag(AIRPLANE)->boundingBox();
        planeRect.origin.x -= 15;
        planeRect.origin.y -= 15;
        planeRect.size.width += 30;
        planeRect.size.height += 30;

        if(planeRect.containsPoint(this->getParent()->convertTouchToNodeSpace(pTouch)))
        {
            CCPoint endPoint = pTouch->getPreviousLocationInView();
            endPoint = CCDirector::sharedDirector()->convertToGL(endPoint);

            CCPoint offset = ccpSub(beginPoint, endPoint);
            CCPoint toPoint = ccpAdd(this->planeLayer->getChildByTag(AIRPLANE)->getPosition(),offset);
            this->planeLayer->MoveTo(toPoint);
        }
    }
}

void GameLayer::registerWithTouchDispatcher()
{
    CCDirector *pDirector=CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this,0,true);
}

并在其中init时打开触屏响应,添加语句         this->setTouchEnabled(true);  即可


在PlaneLayer.h中添加声明

    virtual bool init();
    void MoveTo(CCPoint location);
    bool isAlive;

在PlaneLayer.cpp中分别实现或者初始化它们

构造时初始化    isAlive = true;

MoveTo的实现

void PlaneLayer::MoveTo(CCPoint location)
{
    if(isAlive && !CCDirector::sharedDirector()->isPaused())
    {
        CCPoint actualPoint;
        CCSize winSize = CCDirector::sharedDirector()->getWinSize();
        CCSize planeSize = this->getChildByTag(AIRPLANE)->getContentSize();

        if(location.x < planeSize.width/2)
        {
            location.x = planeSize.width/2;
        }

        if(location.x > winSize.width - planeSize.width/2)
        {
            location.x = winSize.width - planeSize.width/2;
        }

        if(location.y < planeSize.height/2)
        {
            location.y = planeSize.height/2;
        }

        if (location.y > winSize.height - planeSize.height/2)
        {
            location.y = winSize.height - planeSize.height/2;
        }
        this->getChildByTag(AIRPLANE)->setPosition(location);
    }
}

编译运行如图:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值