Как встряхнуть экран в cocos2dx 3?

Пытаюсь раскачать весь экран. Раньше я использовал кодировку ниже:

Shaky3D *shake = Shaky3D::create(0.2f, Size(1,1), 10, false);
this->runAction(Sequence::create(shake, NULL));

Но теперь я использую Cocos2d-x 3.2 и пробовал следующее, но это не работает. Как я должен кодировать правильно? Спасибо.

NodeGrid* nodeGrid = NodeGrid::create();
this->addChild(nodeGrid);

auto shake = Shaky3D::create(0.2f, Size(1,1), 20, false);
nodeGrid->runAction(Sequence::create(shake, NULL));

person RockBaby    schedule 24.10.2014    source источник


Ответы (2)


Ну, я нашел другой способ сделать это.

void GameScene::shakeScreen(float dt)
{
    float randx = rangeRandom( -50.0f, 50.0 );
    float randy = rangeRandom( -50.0f, 50.0 );

    this->setPosition(Point(randx, randy));
    this->setPosition(Point(initialPos.x + randx, initialPos.y + randy));

    SET_SHAKE_DURATION -= 1;

    if (SET_SHAKE_DURATION <= 0)
    {
        this->setPosition(Point(initialPos.x, initialPos.y));
        this->unschedule(schedule_selector(GameScene::shakeScreen));
    }
}

float GameScene::rangeRandom( float min, float max )
{
    float rnd = ((float)rand()/(float)RAND_MAX);
    return rnd*(max-min)+min;
}
person RockBaby    schedule 26.10.2014

ну вот еще лучше (от единства)

float noise(int x, int y) {
    int n = x + y * 57;
    n = (n << 13) ^ n;
    return (1.0 - ((n * ((n * n * 15731) + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
}

bool TestScene::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) {

//experiment more with these four values to get a rough or smooth effect!
    float interval = 0.f;
    float duration = 0.5f;
    float speed = 2.0f;
    float magnitude = 1.0f;

    static float elapsed = 0.f;

    this->schedule([=](float dt) {
        float randomStart = random(-1000.0f, 1000.0f);
        elapsed += dt;

        float percentComplete = elapsed / duration;

// We want to reduce the shake from full power to 0 starting half way through
        float damper = 1.0f - clampf(2.0f * percentComplete - 1.0f, 0.0f, 1.0f);

// Calculate the noise parameter starting randomly and going as fast as speed allows
        float alpha = randomStart + speed * percentComplete;

        // map noise to [-1, 1]
        float x = noise(alpha, 0.0f) * 2.0f - 1.0f;
        float y = noise(0.0f, alpha) * 2.0f - 1.0f;

        x *= magnitude * damper;
        y *= magnitude * damper;
        this->setPosition(x, y);

        if (elapsed >= duration)
        {
            elapsed = 0;
            this->unschedule("Shake");
            this->setPosition(Vec2::ZERO);
        }

    }, interval, CC_REPEAT_FOREVER, 0.f, "Shake");

    return true;
}
person Joseph    schedule 24.02.2016