2010-11-18 14:50:28 +08:00
|
|
|
#include "Paddle.h"
|
|
|
|
|
|
|
|
Paddle::Paddle(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Paddle::~Paddle(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCRect Paddle::rect()
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize s = getTexture()->getContentSize();
|
|
|
|
return CCRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Paddle* Paddle::paddleWithTexture(CCTexture2D* aTexture)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool Paddle::initWithTexture(CCTexture2D* aTexture)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if( CCSprite::initWithTexture(aTexture) )
|
|
|
|
{
|
|
|
|
m_state = kPaddleStateUngrabbed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Paddle::onEnter()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDirector* pDirector = CCDirector::sharedDirector();
|
2012-03-21 17:35:20 +08:00
|
|
|
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSprite::onEnter();
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Paddle::onExit()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDirector* pDirector = CCDirector::sharedDirector();
|
|
|
|
pDirector->getTouchDispatcher()->removeDelegate(this);
|
|
|
|
CCSprite::onExit();
|
|
|
|
}
|
2010-08-27 11:53:35 +08:00
|
|
|
|
|
|
|
bool Paddle::containsTouchLocation(CCTouch* touch)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return CCRect::CCRectContainsPoint(rect(), convertTouchToNodeSpaceAR(touch));
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2011-03-18 09:39:34 +08:00
|
|
|
bool Paddle::ccTouchBegan(CCTouch* touch, CCEvent* event)
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (m_state != kPaddleStateUngrabbed) return false;
|
|
|
|
if ( !containsTouchLocation(touch) ) return false;
|
|
|
|
|
|
|
|
m_state = kPaddleStateGrabbed;
|
|
|
|
return true;
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2011-03-18 09:39:34 +08:00
|
|
|
void Paddle::ccTouchMoved(CCTouch* touch, CCEvent* 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
|
|
|
|
// you get CCSets instead of 1 UITouch, so you'd need to loop through the set
|
|
|
|
// in each touchXXX method.
|
|
|
|
|
|
|
|
CCAssert(m_state == kPaddleStateGrabbed, L"Paddle - Unexpected state!");
|
|
|
|
|
|
|
|
CCPoint touchPoint = touch->locationInView();
|
2010-11-11 11:18:58 +08:00
|
|
|
touchPoint = CCDirector::sharedDirector()->convertToGL( touchPoint );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
setPosition( CCPointMake(touchPoint.x, getPosition().y) );
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2012-04-13 09:45:31 +08:00
|
|
|
CCObject* Paddle::copyWithZone(CCZone *pZone)
|
|
|
|
{
|
|
|
|
this->retain();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2011-03-18 09:39:34 +08:00
|
|
|
void Paddle::ccTouchEnded(CCTouch* touch, CCEvent* event)
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(m_state == kPaddleStateGrabbed, L"Paddle - Unexpected state!");
|
|
|
|
|
|
|
|
m_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
|
|
|
}
|