axmol/samples/Cpp/TestCpp/Classes/TouchesTest/Paddle.cpp

95 lines
2.2 KiB
C++
Raw Normal View History

2010-11-18 14:50:28 +08:00
#include "Paddle.h"
Paddle::Paddle(void)
{
}
Paddle::~Paddle(void)
{
}
Rect Paddle::getRect()
2010-08-27 11:53:35 +08:00
{
Size s = getTexture()->getContentSize();
return Rect(-s.width / 2, -s.height / 2, s.width, s.height);
2010-08-27 11:53:35 +08:00
}
Paddle* Paddle::createWithTexture(Texture2D* aTexture)
2010-08-27 11:53:35 +08:00
{
Paddle* pPaddle = new Paddle();
pPaddle->initWithTexture( aTexture );
pPaddle->autorelease();
2010-08-27 11:53:35 +08:00
return pPaddle;
2010-08-27 11:53:35 +08:00
}
bool Paddle::initWithTexture(Texture2D* aTexture)
2010-08-27 11:53:35 +08:00
{
if( Sprite::initWithTexture(aTexture) )
{
_state = kPaddleStateUngrabbed;
}
return true;
2010-08-27 11:53:35 +08:00
}
void Paddle::onEnter()
{
Director* director = Director::getInstance();
director->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
Sprite::onEnter();
2010-08-27 11:53:35 +08:00
}
void Paddle::onExit()
{
Director* director = Director::getInstance();
director->getTouchDispatcher()->removeDelegate(this);
Sprite::onExit();
}
2010-08-27 11:53:35 +08:00
bool Paddle::containsTouchLocation(Touch* touch)
2010-08-27 11:53:35 +08:00
{
return getRect().containsPoint(convertTouchToNodeSpaceAR(touch));
2010-08-27 11:53:35 +08:00
}
bool Paddle::ccTouchBegan(Touch* touch, Event* event)
2010-08-27 11:53:35 +08:00
{
if (_state != kPaddleStateUngrabbed) return false;
if ( !containsTouchLocation(touch) ) return false;
_state = kPaddleStateGrabbed;
return true;
2010-08-27 11:53:35 +08:00
}
void Paddle::ccTouchMoved(Touch* touch, Event* event)
2010-08-27 11:53:35 +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 Sets instead of 1 UITouch, so you'd need to loop through the set
// in each touchXXX method.
CCASSERT(_state == kPaddleStateGrabbed, "Paddle - Unexpected state!");
Point touchPoint = touch->getLocation();
2013-07-12 14:11:55 +08:00
setPosition( Point(touchPoint.x, getPosition().y) );
2010-08-27 11:53:35 +08:00
}
Paddle* Paddle::clone() const
{
Paddle* ret = Paddle::createWithTexture(_texture);
ret->_state = _state;
ret->setPosition(getPosition());
ret->setAnchorPoint(getAnchorPoint());
return ret;
}
void Paddle::ccTouchEnded(Touch* touch, Event* event)
2010-08-27 11:53:35 +08:00
{
CCASSERT(_state == kPaddleStateGrabbed, "Paddle - Unexpected state!");
_state = kPaddleStateUngrabbed;
2011-12-28 15:13:02 +08:00
}