2010-11-18 14:50:28 +08:00
|
|
|
#include "Paddle.h"
|
|
|
|
|
|
|
|
Paddle::Paddle(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Paddle::~Paddle(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Rect Paddle::rect()
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Size s = getTexture()->getContentSize();
|
2012-04-19 14:35:52 +08:00
|
|
|
return CCRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Paddle* Paddle::paddleWithTexture(Texture2D* aTexture)
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
Paddle* pPaddle = new Paddle();
|
|
|
|
pPaddle->initWithTexture( aTexture );
|
|
|
|
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-06-20 14:17:10 +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
|
|
|
Director* pDirector = Director::sharedDirector();
|
2012-03-21 17:35:20 +08:00
|
|
|
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
|
2013-06-20 14:17:10 +08:00
|
|
|
Sprite::onEnter();
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Paddle::onExit()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Director* pDirector = Director::sharedDirector();
|
2012-04-19 14:35:52 +08:00
|
|
|
pDirector->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
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
return rect().containsPoint(convertTouchToNodeSpaceAR(touch));
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
bool Paddle::ccTouchBegan(Touch* touch, Event* event)
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
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;
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void Paddle::ccTouchMoved(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-06-15 14:03:30 +08:00
|
|
|
CCAssert(_state == kPaddleStateGrabbed, "Paddle - Unexpected state!");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Point touchPoint = touch->getLocation();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-10-23 17:48:50 +08:00
|
|
|
setPosition( ccp(touchPoint.x, getPosition().y) );
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Object* Paddle::copyWithZone(Zone *pZone)
|
2012-04-13 09:45:31 +08:00
|
|
|
{
|
|
|
|
this->retain();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void Paddle::ccTouchEnded(Touch* touch, Event* event)
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +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
|
|
|
}
|
|
|
|
|
|
|
|
void Paddle::touchDelegateRetain()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
this->retain();
|
2011-12-28 15:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Paddle::touchDelegateRelease()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
this->release();
|
2011-12-28 15:13:02 +08:00
|
|
|
}
|