码迷,mamicode.com
首页 > 移动开发 > 详细

cocos2d-x ios游戏开发初认识(八) 触摸事件与碰撞检测

时间:2014-08-20 00:08:25      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   文件   for   

玩过植物大战僵尸都知道,要在草坪里放一朵向日葵或者其它的植物只需触摸那个植物将其拖入到想要摆放的位置,这其实就是这节要写的触摸事件。还可以发现当我们的僵尸出来的时候,我们的小豌豆会发子弹攻击僵尸,当子弹与僵尸碰撞的时候子弹自动消失,这就这节要说的碰撞检测。

下面具体看代码的实现:

做ios开发有触摸事件cocos2d同样也有

一、先覆写touch事件

.h文件

using namespace cocos2d;


class MainScene:public CCLayer {

private:

    virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent); //覆写虚函数


.m文件实现

要想让层接收触摸事件要先使能触摸:

在初始化函数要添加

    setTouchEnabled(true); //接收触屏事件

//触屏事件调用的方法

void MainScene::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)

{

    CCTouch *touch = (CCTouch *)pTouches->anyObject();

    CCPoint point = touch->getLocation(); //得到触摸的点 (位置)

    

    CCSprite *sp = CCSprite::create("Peashooter1.tiff"); //创建一个精灵

    sp->setPosition(point);  //设置精灵的位置为触摸点的位置

    this->addChild(sp);

    

}

运行:

在屏幕上随意点击:

bubuko.com,布布扣

可以看到点击的地方就出现一个豌豆。

再增加一些功能,现在的豌豆不会动,下面给豌豆做一个摇头的帧动画。

先添加13张摇头的帧图片:

bubuko.com,布布扣

//触屏事件调用的方法

void MainScene::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)

{

    CCTouch *touch = (CCTouch *)pTouches->anyObject();

    CCPoint point = touch->getLocation(); //得到触摸的点 (位置)

    

    CCSprite *sp = CCSprite::create("Peashooter1.tiff"); //创建一个精灵

    sp->setPosition(point);  //设置精灵的位置为触摸点的位置

    this->addChild(sp);

    

    //帧动画

    CCAnimation *animation = CCAnimation::create();

    for (int i=1; i<=13; i++) {

        CCString *string = CCString::createWithFormat("Peashooter%d.tiff",i);

        CCSpriteFrame *frame = CCSpriteFrame::create(string->getCString(), CCRectMake(0, 0, 71, 71));

        

        animation->addSpriteFrame(frame);

    }

    animation->setDelayPerUnit(0.1);

    animation->setLoops(-1);  //循环的次数 -1 无限次

    CCAnimate *animate = CCAnimate::create(animation); //添加到动画

    

    sp->runAction(animate);  //运行动画

}

运行:


bubuko.com,布布扣

可以看到 小豌豆在摇头。。。。


下面看精灵碰撞检测:

碰撞检测说得简单点其实就是说一个精灵的移动到的位置是否在另外一个精灵位置的包含内。

具体实现:

.h文件 定义

class MainScene:public CCLayer {

private:

    virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent); //覆写虚函数

    CCSprite *zom;   //子弹精灵

    CCSprite *pb;    //僵尸精灵


.m文件实现

    //子弹

    pb = CCSprite::create("PB01.png");

    pb->setPosition(ccp(20, 300));

    this->addChild(pb);

    

    CCMoveBy *by = CCMoveBy::create(4, ccp(800, 0)); //4s移动到800的位置

    pb->runAction(by);

    

    //僵尸

    zom = CCSprite::create("Zombie1.tiff");

    zom->setPosition(ccp(810, 300));

    this->addChild(zom);

    

    //设置帧回掉函数

    this->schedule(schedule_selector(MainScene::update));


//回掉函数:

//回掉函数

void MainScene::update(float t)

{

    /*设置回掉函数的操作*/

    

    //让每一帧向右移动 记得把sprite设置为全局变量

   // sprite->setPosition(ccpAdd(sprite->getPosition(), ccp(1, 0))); //让精灵每一帧 x轴上加1


    if(pb!= NULL && pb->boundingBox().intersectsRect(zom->boundingBox())) //两个精灵碰撞

    {

        CCLOG("碰撞!!!");

        //碰撞了 让子弹消失

        pb->stopAllActions();

        pb->removeFromParentAndCleanup(true);

        pb = NULL;

    }

}

运行:

bubuko.com,布布扣 精灵在运动!!


bubuko.com,布布扣当碰撞到了子弹消失,碰撞!!被打印出来、、

这就是这节要写的内容,大家可以自己试一下。。

cocos2d-x ios游戏开发初认识(八) 触摸事件与碰撞检测,布布扣,bubuko.com

cocos2d-x ios游戏开发初认识(八) 触摸事件与碰撞检测

标签:style   blog   http   color   os   io   文件   for   

原文地址:http://blog.csdn.net/itpeng523/article/details/38688499

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!