Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. /// Test ///
  2.  
  3. auto contactListener = EventListenerPhysicsContact::create();
  4. contactListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
  5. _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
  6.  
  7. // _paddle->_controller->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);
  8. // PaddleController::getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);
  9. schedule(CC_SCHEDULE_SELECTOR(GameScene::update));
  10.  
  11. return true;
  12. }
  13.  
  14. bool GameScene::onContactBegin(PhysicsContact& contact)
  15. {
  16. Node *paddle = nullptr;
  17. Node *ball = nullptr;
  18.  
  19. if (contact.getShapeA()->getBody()->getName() == "paddle" &&
  20. contact.getShapeB()->getBody()->getName() == "ball")
  21. {
  22. paddle = contact.getShapeA()->getBody()->getNode();
  23. ball = contact.getShapeB()->getBody()->getNode();
  24. }
  25. else if (contact.getShapeA()->getBody()->getName() == "ball" &&
  26. contact.getShapeB()->getBody()->getName() == "paddle")
  27. {
  28. paddle = contact.getShapeB()->getBody()->getNode();
  29. ball = contact.getShapeA()->getBody()->getNode();
  30. }
  31.  
  32. if (ball && paddle)
  33. {
  34. int minSpeed = 600;
  35. int maxSpeed = 800;
  36. float hitDisplacement =
  37. (ball->getPosition().x - paddle->getPosition().x) / (paddle->getContentSize().width * paddle->getScaleX()/ 2);
  38.  
  39. auto velocity = ball->getPhysicsBody()->getVelocity();
  40.  
  41. float x = hitDisplacement * maxSpeed;
  42. float y = minSpeed + (maxSpeed - minSpeed)* fabsf(hitDisplacement);
  43. ball->getPhysicsBody()->setVelocity(Vec2(x,y));
  44.  
  45. AudioPlayer::playEffect(AudioPlayer::hitPaddle);
  46. CCLOG("Ball collide with Paddle at %f ", hitDisplacement);
  47.  
  48. return true;
  49. }
  50. return false;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement