码迷,mamicode.com
首页 > 其他好文 > 详细

超级马丽与怪物水平碰撞和跳起上下碰撞检测

时间:2014-12-18 22:08:07      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   sp   strong   on   

超级马丽与怪物的碰撞检测,利用intersectsRect函数检测碰撞。水平碰撞时,怪物杀了超级马丽;跳起来踩怪物时,怪物被杀死。

EnemyVSHero CCEnemy::checkCollisionWithHero()
{
    EnemyVSHero ret = eVS_nonKilled;

    CCPoint heroPos = CCHero::getHeroInstance()->getPosition();
    CCSize heroSize = CCHero::getHeroInstance()->getContentSize();
    CCRect heroRect = CCRectMake(heroPos.x - heroSize.width/2 + 2, heroPos.y + 3, 
        heroSize.width - 4, heroSize.height - 4);

    CCRect heroRectVS = CCRectMake(heroPos.x - heroSize.width/2 - 3, heroPos.y, 
        heroSize.width - 6, 2);

    CCPoint enemyPos = this->getPosition();
    CCRect enemyRect = CCRectMake(enemyPos.x - bodySize.width/2 + 1, enemyPos.y, 
        bodySize.width - 2, bodySize.height - 4);

    CCRect enemyRectVS = CCRectMake(enemyPos.x - bodySize.width/2 - 2, enemyPos.y + bodySize.height - 4, 
        bodySize.width - 4, 4);

    if (heroRectVS.intersectsRect(enemyRectVS))
    {
        ret = eVS_enemyKilled;
        return ret;
    }

    if (heroRect.intersectsRect(enemyRect))
    {
        ret = eVS_heroKilled;
        return ret;
    }

    return ret;
}

bubuko.com,布布扣

画一张图,看起来容易理解。

超级马丽与怪物水平碰撞和跳起上下碰撞检测

标签:style   blog   http   io   color   os   sp   strong   on   

原文地址:http://www.cnblogs.com/ycclmy/p/4172626.html

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