为什么setPosition在某些情况下可以工作而在其他情况下却不能工作?

时间:2019-02-12 02:02:29

标签: c++ cocos2d-x cocos2d-x-3.17

我做一个游戏,比如说ThrowBall,玩家可以捡起生成的球并将其扔到目标中,获得得分,然后球返回其位置并重复。问题是当Ball被Player拖动(我将Ball作为Player的子代)拖动到目标中时,Ball将其位置正确地返回到所需位置,但是当我通过施加脉冲将其发送到目标中并将其发送到目标中后发生碰撞时,发生了奇怪的情况不能将球放回理想位置,为什么会发生这种情况?

我正在为Android运行此游戏,我正在使用cocos2d-x-3.17在VS'17中运行此代码。我试图将冲动转变为动力,即步即逝。我试图使球停下来(在setPosition之前setVelocity为零)。我尝试将Point更改为Vect vec2。我试图进行断点调试,该代码确实读取了setPosition,但什么也不做。

GameScene.cpp
bool GameScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

auto pickupListener = EventListenerPhysicsContact::create();
pickupListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(pickupListener, this);
return true;
}

bool GameScene::onContactBegin(cocos2d::PhysicsContact &contact)
{
PhysicsBody *a = contact.getShapeA()->getBody();
PhysicsBody *b = contact.getShapeB()->getBody();

if (
    (BALL_COLLISION_BITMASK == a->getCollisionBitmask() && PLAYER_COLLISION_BITMASK == b->getCollisionBitmask()) ||
    (BALL_COLLISION_BITMASK == b->getCollisionBitmask() && PLAYER_COLLISION_BITMASK == a->getCollisionBitmask())
    )
{
    // make the ball follow Player
    isfollow = true;
}
else if (
    (BALL_COLLISION_BITMASK == a->getCollisionBitmask() && TARGET_COLLISION_BITMASK == b->getCollisionBitmask()) ||
    (BALL_COLLISION_BITMASK == b->getCollisionBitmask() && TARGET_COLLISION_BITMASK == a->getCollisionBitmask())
    )
{
    isfollow = false;
    // add score
    score++;
    __String *tempScore = __String::createWithFormat("%i", score);

    scoreLabel->setString(tempScore->getCString());

    // return spawn ball
    ball->returnPos();
    return false;
}
void GameScene::throwBall() {
if (isfollow == true) {
    isfollow = false;
    ball->getSprite()->getPhysicsBody()->applyImpulse(Vec2(100000, 100000));
}                                                                                                                                                                                                                                                                                                         
}

Ball.h
class Ball
{
public:
Ball(cocos2d::Layer *layer);
cocos2d::Sprite *getSprite() { return randomSpawn; };
void returnPos();

private:
cocos2d::Size visibleSize;
cocos2d::Vec2 origin;
cocos2d::Sprite *randomSpawn;
};

Ball.cpp
Ball::Ball(cocos2d::Layer *layer)
{
visibleSize = Director::getInstance()->getVisibleSize();
origin = Director::getInstance()->getVisibleOrigin();
randomSpawn = Sprite::create("res/ball.png");
randomSpawn->setPosition(Vec2(400, 80));

auto randomBallBody = PhysicsBody::createCircle(randomSpawn->getContentSize().width / 2);
randomBallBody->setCollisionBitmask(BALL_COLLISION_BITMASK);
randomBallBody->setContactTestBitmask(true);
randomBallBody->setGravityEnable(false);
randomSpawn->setPhysicsBody(randomBallBody);

layer->addChild(randomSpawn);
}

void Ball::returnPos()
{
randomSpawn->setPosition(Vec2(400, 80));
}

当碰撞到目标并重复时,我想使对象(球)返回位置。我很抱歉,如果格式是一团糟,我是这里的新手,也不是我的完整代码,我可以运行它,但实际上工作得很好,但是只有setPosition无法正常工作

0 个答案:

没有答案