【问题标题】:collision detect don't get the collisions碰撞检测没有得到碰撞
【发布时间】:2014-05-16 05:24:57
【问题描述】:

我正在尝试开发 2 个精灵之间的碰撞,但我没有完成它。 我正在安排一个选择器来验证碰撞,并安排另一个选择器来生成敌人的精灵。问题是它们没有碰撞。

this->schedule(schedule_selector(HelloWorld::addenemy),2);
this->schedule(schedule_selector(HelloWorld::collisionEnemy),2);

我正在按上述方式安排两个选择器,但是如果我尝试使用小于 2 的值(小于 addEnemy 计时器)来安排 collisionEnemy,则会出现错误:

libpng warning: iCCP: known incorrect sRGB profile

如果我输入 2 或更大的值,则标识符不会发生冲突

这是碰撞敌人选择器:

void HelloWorld::collisionEnemy(float dt)
{
    CCSprite *player = (CCSprite*)getChildByTag(1);
    CCSprite *enemy = (CCSprite*)getChildByTag(3);

    CCRect playerRect = CCRectMake(
                                   player->getPosition().x - (player->getContentSize().width/2),
                                   player->getPosition().y - (player->getContentSize().height/2),
                                   player->getContentSize().width,
                                   player->getContentSize().height);

    CCRect miniBarrierRect = CCRectMake(
                                   enemy->getPosition().x - (enemy->getContentSize().width/2),
                                   enemy->getPosition().y - (enemy->getContentSize().height/2),
                                   enemy->getContentSize().width,
                                   enemy->getContentSize().height);

    if (playerRect.intersectsRect(enemyRect)) {

        CCLog("enemy Collision");

    } else {

        CCLog("not enemy Collision");
        }



    }

【问题讨论】:

  • 是碰撞检测不行还是调度不行?
  • 可能是调度,如果值大于2,则运行,但不会发生冲突,因为它没有在正确的帧中运行
  • 你试过boundingbox()吗?我主要使用它,它对我有用。

标签: c++ cocos2d-x


【解决方案1】:

如果你想要真正的碰撞检测,我认为你应该研究 Box2D

非常简单的碰撞检测:

cocos2d::Vector2 p = touch->getLocation();

cocos2d::Rect rect = _sprite->getBoundingBox();

if(rect.containsPoint(p))
{
    // collision
}
else
{
    // no collision
}

您的警告libpng warning: iCCP: known incorrect sRGB profile 就是这样。像 Photoshop 这样的一些工具会嵌入错误的 ICCP 块。如果你有 ImageMagick,你可以轻松摆脱警告

find . -type f -name "*.png" -exec convert {} -strip {} \;

在你运行上面的命令之前make确保你知道你从哪里运行它。仅限于您的游戏资源所在的位置。始终先备份。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-05
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多