超级马丽与怪物水平碰撞和跳起上下碰撞检测
超级马丽与怪物的碰撞检测,利用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; }
画一张图,看起来容易理解。