2010-11-18 14:50:28 +08:00
|
|
|
#include "Paddle.h"
|
|
|
|
|
|
|
|
Paddle::Paddle(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Paddle::~Paddle(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
Rect Paddle::getRect()
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = getTexture()->getContentSize();
|
2013-07-12 14:30:26 +08:00
|
|
|
return Rect(-s.width / 2, -s.height / 2, s.width, s.height);
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
Paddle* Paddle::createWithTexture(Texture2D* aTexture)
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Paddle* pPaddle = new (std::nothrow) Paddle();
|
2013-09-13 11:46:46 +08:00
|
|
|
pPaddle->initWithTexture(aTexture);
|
2012-04-19 14:35:52 +08:00
|
|
|
pPaddle->autorelease();
|
2010-08-27 11:53:35 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pPaddle;
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
bool Paddle::initWithTexture(Texture2D* aTexture)
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2013-09-13 11:46:46 +08:00
|
|
|
if( Sprite::initWithTexture(aTexture) )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = kPaddleStateUngrabbed;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Paddle::onEnter()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Sprite::onEnter();
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
// Register Touch Event
|
2013-10-23 11:27:24 +08:00
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
2013-09-03 18:22:03 +08:00
|
|
|
listener->setSwallowTouches(true);
|
|
|
|
|
|
|
|
listener->onTouchBegan = CC_CALLBACK_2(Paddle::onTouchBegan, this);
|
|
|
|
listener->onTouchMoved = CC_CALLBACK_2(Paddle::onTouchMoved, this);
|
|
|
|
listener->onTouchEnded = CC_CALLBACK_2(Paddle::onTouchEnded, this);
|
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Paddle::onExit()
|
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
// auto director = Director::getInstance();
|
|
|
|
// director->getTouchDispatcher()->removeDelegate(this);
|
2013-06-20 14:17:10 +08:00
|
|
|
Sprite::onExit();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-08-27 11:53:35 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
bool Paddle::containsTouchLocation(Touch* touch)
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2013-07-08 23:05:47 +08:00
|
|
|
return getRect().containsPoint(convertTouchToNodeSpaceAR(touch));
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
bool Paddle::onTouchBegan(Touch* touch, Event* event)
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
CCLOG("Paddle::onTouchBegan id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_state != kPaddleStateUngrabbed) return false;
|
2012-04-19 14:35:52 +08:00
|
|
|
if ( !containsTouchLocation(touch) ) return false;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = kPaddleStateGrabbed;
|
2013-09-03 18:22:03 +08:00
|
|
|
CCLOG("return true");
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void Paddle::onTouchMoved(Touch* touch, Event* event)
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// If it weren't for the TouchDispatcher, you would need to keep a reference
|
|
|
|
// to the touch from touchBegan and check that the current touch is the same
|
|
|
|
// as that one.
|
|
|
|
// Actually, it would be even more complicated since in the Cocos dispatcher
|
2013-06-20 14:17:10 +08:00
|
|
|
// you get Sets instead of 1 UITouch, so you'd need to loop through the set
|
2012-04-19 14:35:52 +08:00
|
|
|
// in each touchXXX method.
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
CCLOG("Paddle::onTouchMoved id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y);
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(_state == kPaddleStateGrabbed, "Paddle - Unexpected state!");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto touchPoint = touch->getLocation();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
setPosition( Vec2(touchPoint.x, getPosition().y) );
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
Paddle* Paddle::clone() const
|
2012-04-13 09:45:31 +08:00
|
|
|
{
|
2013-07-08 23:05:47 +08:00
|
|
|
Paddle* ret = Paddle::createWithTexture(_texture);
|
|
|
|
ret->_state = _state;
|
|
|
|
ret->setPosition(getPosition());
|
|
|
|
ret->setAnchorPoint(getAnchorPoint());
|
|
|
|
return ret;
|
2012-04-13 09:45:31 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void Paddle::onTouchEnded(Touch* touch, Event* event)
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(_state == kPaddleStateGrabbed, "Paddle - Unexpected state!");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = kPaddleStateUngrabbed;
|
2011-12-28 15:13:02 +08:00
|
|
|
}
|