模拟屏幕震动效果,使用cocos2d-x 3.x

前端之家收集整理的这篇文章主要介绍了模拟屏幕震动效果,使用cocos2d-x 3.x前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. void shakeNode(cocos2d::Node *node,float duration,float rate)
  2. {
  3. Vec2 pos = node->getPosition();
  4. float tmp =0;
  5. float zs = node->getScale();
  6. schedule([=](float dt) mutable
  7. {
  8. tmp += dt;
  9. if (tmp>=duration)
  10. {
  11. unschedule("updateShake");
  12. node->setPosition(pos);
  13. node->setScale(zs);
  14. return ;
  15. }
  16. else
  17. {
  18. float z = (arc4random()%5+98) * 0.01f;
  19. CCLOG("z=%f",z);
  20. float x = arc4random() % 3 + 1;
  21. float y = arc4random() % 4 + 1;
  22. int r = arc4random() % 2;
  23. if (r>0) {
  24. x *= -1;
  25. r = arc4random() % 2;
  26. if (r>0) {
  27. y *= -1;
  28. }
  29. }
  30. node->setPosition(x,y);
  31. node->setScale(z);
  32. }
  33. },rate,"updateShake");
  34. }

猜你在找的Cocos2d-x相关文章