Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Test ///
- auto contactListener = EventListenerPhysicsContact::create();
- contactListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
- _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
- // _paddle->_controller->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);
- // PaddleController::getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);
- schedule(CC_SCHEDULE_SELECTOR(GameScene::update));
- return true;
- }
- bool GameScene::onContactBegin(PhysicsContact& contact)
- {
- Node *paddle = nullptr;
- Node *ball = nullptr;
- if (contact.getShapeA()->getBody()->getName() == "paddle" &&
- contact.getShapeB()->getBody()->getName() == "ball")
- {
- paddle = contact.getShapeA()->getBody()->getNode();
- ball = contact.getShapeB()->getBody()->getNode();
- }
- else if (contact.getShapeA()->getBody()->getName() == "ball" &&
- contact.getShapeB()->getBody()->getName() == "paddle")
- {
- paddle = contact.getShapeB()->getBody()->getNode();
- ball = contact.getShapeA()->getBody()->getNode();
- }
- if (ball && paddle)
- {
- int minSpeed = 600;
- int maxSpeed = 800;
- float hitDisplacement =
- (ball->getPosition().x - paddle->getPosition().x) / (paddle->getContentSize().width * paddle->getScaleX()/ 2);
- auto velocity = ball->getPhysicsBody()->getVelocity();
- float x = hitDisplacement * maxSpeed;
- float y = minSpeed + (maxSpeed - minSpeed)* fabsf(hitDisplacement);
- ball->getPhysicsBody()->setVelocity(Vec2(x,y));
- AudioPlayer::playEffect(AudioPlayer::hitPaddle);
- CCLOG("Ball collide with Paddle at %f ", hitDisplacement);
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement