标签:cocos2d-x 碰撞检测 通过矩形区域是否相交判断
原文同步发布于我的wiki,查看原文或更新请移步:
点击打开链接
scheduleUpdate();
schedule(schedule_selector(HelloWorld::updateCustom), 5.0f,
kRepeatForever, 0);
void HelloWorld::buildBullet(int startX, int endX, int actualY) {
log("buildBullet");
std::string name = "bullet1.png";
auto sp = Sprite::createWithSpriteFrameName(name);
Point startPos = Point(startX, actualY);
Point endPos = Point(endX, actualY);
sp->setPosition(startPos);
sp->runAction(
Sequence::create(MoveTo::create(3.0, endPos),
CallFuncN::create(this,
callfuncN_selector(HelloWorld::spriteDone)), NULL));
sp->setRotation(-90);
sp->setTag(TAG_BULLET);
mBullets.pushBack(sp);//放入数组
this->addChild(sp);
}
void HelloWorld::update(float dt) {
// log("update, %d", mPlanes.size());
// log("update, %d", mBullets.size());
//碰撞检测
Vector<Sprite*> bulletsToBeRemoved;
Vector<Sprite*> planeToBeRemoved;
for (auto bullet : mBullets) {
for (auto plane : mPlanes) {
if (bullet->boundingBox().intersectsRect(plane->getBoundingBox())) {
log("碰撞了。。。。。。。。");
bulletsToBeRemoved.pushBack(bullet);
planeToBeRemoved.pushBack(plane);
}
}
}
// do remove
for (auto bullet : bulletsToBeRemoved) {
mBullets.eraseObject(bullet);
log("removeChild(bullet)");
this->removeChild(bullet);
}
bulletsToBeRemoved.clear();
for (auto plane : planeToBeRemoved) {
mPlanes.eraseObject(plane);
planeBlowup(plane);
}
planeToBeRemoved.clear();
}
https://github.com/cheyiliu/All-in-One/raw/master/res/cocos2d/CollisionDetection-way1-use-intersectsRect.gif
cocos2d-x-3.3-019-碰撞检测1-矩形区域是否相交
标签:cocos2d-x 碰撞检测 通过矩形区域是否相交判断
原文地址:http://blog.csdn.net/cheyiliu/article/details/43560623