2014-10-24 2 views
0

전 화면을 흔들려고합니다. 이전에 나는 아래의 코드를 사용하고 있습니다 :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)); 

답변

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; 
} 
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; 
} 
입니다