我试图撼动整个屏幕。 以前我使用下面的代码:
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));
答案 0 :(得分:0)
好吧,我找到了另一种方法。
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;
}
答案 1 :(得分:0)
这里有一个更好的(来自团结)
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;
}