【问题标题】:How to shake screen in cocos2dx 3?cocos2dx 3如何摇屏?
【发布时间】:2014-10-24 15:38:43
【问题描述】:

我正在尝试摇晃整个屏幕。 以前我使用下面的编码:

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));

【问题讨论】:

    标签: cocos2d-x cocos2d-x-3.0


    【解决方案1】:

    嗯,我找到了另一种方法。

    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;
    }
    

    【讨论】:

      【解决方案2】:

      这里有一个更好的(来自统一)

      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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多