2010-08-27 11:53:35 +08:00
|
|
|
#include "Paddle.h"
|
|
|
|
|
|
|
|
Paddle::Paddle(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Paddle::~Paddle(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CGRect Paddle::rect()
|
|
|
|
{
|
|
|
|
CGSize s = getTexture()->getContentSize();
|
|
|
|
return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
Paddle* Paddle::paddleWithTexture(CCTexture2D* aTexture)
|
|
|
|
{
|
|
|
|
Paddle* pPaddle = new Paddle();
|
|
|
|
pPaddle->initWithTexture( aTexture );
|
|
|
|
pPaddle->autorelease();
|
|
|
|
|
|
|
|
return pPaddle;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Paddle::initWithTexture(CCTexture2D* aTexture)
|
|
|
|
{
|
2010-09-02 15:15:45 +08:00
|
|
|
if( CCSprite::initWithTexture(aTexture) )
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
|
|
|
m_state = kPaddleStateUngrabbed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Paddle::onEnter()
|
|
|
|
{
|
|
|
|
CCTouchDispatcher::getSharedDispatcher()->addTargetedDelegate(this, 0, true);
|
2010-09-02 15:31:18 +08:00
|
|
|
CCSprite::onEnter();
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Paddle::onExit()
|
|
|
|
{
|
|
|
|
CCTouchDispatcher::getSharedDispatcher()->removeDelegate(this);
|
2010-09-02 15:31:18 +08:00
|
|
|
CCSprite::onExit();
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Paddle::containsTouchLocation(CCTouch* touch)
|
|
|
|
{
|
|
|
|
return CGRect::CGRectContainsPoint(rect(), convertTouchToNodeSpaceAR(touch));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Paddle::ccTouchBegan(CCTouch* touch, UIEvent* event)
|
|
|
|
{
|
|
|
|
if (m_state != kPaddleStateUngrabbed) return false;
|
|
|
|
if ( !containsTouchLocation(touch) ) return false;
|
|
|
|
|
|
|
|
m_state = kPaddleStateGrabbed;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Paddle::ccTouchMoved(CCTouch* touch, UIEvent* event)
|
|
|
|
{
|
|
|
|
// 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 NSSets instead of 1 UITouch, so you'd need to loop through the set
|
|
|
|
// in each touchXXX method.
|
|
|
|
|
|
|
|
NSAssert(m_state == kPaddleStateGrabbed, L"Paddle - Unexpected state!");
|
|
|
|
|
|
|
|
CGPoint touchPoint = touch->locationInView( touch->view() );
|
|
|
|
touchPoint = CCDirector::getSharedDirector()->convertToGL( touchPoint );
|
|
|
|
|
|
|
|
setPosition( CGPointMake(touchPoint.x, getPosition().y) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Paddle::ccTouchEnded(CCTouch* touch, UIEvent* event)
|
|
|
|
{
|
|
|
|
NSAssert(m_state == kPaddleStateGrabbed, L"Paddle - Unexpected state!");
|
|
|
|
|
|
|
|
m_state = kPaddleStateUngrabbed;
|
|
|
|
}
|