cocos2dx 可拖动的精灵类

实现的是3d效果的, 2d实现起来也是一样的


#pragma once
#include "cocos2d.h"
USING_NS_CC;
typedef enum tagPaddleState 
{
	kPaddleStateGrabbed,
	kPaddleStateUngrabbed
} PaddleState; 
class TouchSprite3D:public Sprite3D
{
	PaddleState        _state;
	Point m_oldPoint;
public:
	TouchSprite3D(void);
	virtual ~TouchSprite3D(void);

	static TouchSprite3D* create3D(const std::string &modelPath);
	Rect getRect();
	virtual void onEnter() override;
	virtual void onExit() override;
	bool containsTouchLocation(Touch* touch);
	bool onTouchBegan(Touch* touch, Event* event);
	void onTouchMoved(Touch* touch, Event* event);
	void onTouchEnded(Touch* touch, Event* event);
};

#include "TouchSprite3D.h"


TouchSprite3D::TouchSprite3D(void)
{
	//onEnter();
}


TouchSprite3D::~TouchSprite3D(void)
{
}

Rect TouchSprite3D::getRect()
{
	auto s = this->getContentSize();
	return Rect(-s.width / 2, -s.height / 2, s.width, s.height);
}

void TouchSprite3D::onEnter()
{
	Sprite3D::onEnter();

	// Register Touch Event
	auto listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);

	listener->onTouchBegan = CC_CALLBACK_2(TouchSprite3D::onTouchBegan, this);
	listener->onTouchMoved = CC_CALLBACK_2(TouchSprite3D::onTouchMoved, this);
	listener->onTouchEnded = CC_CALLBACK_2(TouchSprite3D::onTouchEnded, this);

	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
	_state = kPaddleStateUngrabbed;
}

void TouchSprite3D::onExit()
{
	//    auto director = Director::getInstance();
	//    director->getTouchDispatcher()->removeDelegate(this);
	Sprite3D::onExit();
}    

bool TouchSprite3D::containsTouchLocation(Touch* touch)
{
	//return getRect().containsPoint(convertTouchToNodeSpaceAR(touch));
	float len = (this->getPosition() - touch->getLocation()).length();
	if (len < 150)
	{
		return true;
	}
	return false;
}

bool TouchSprite3D::onTouchBegan(Touch* touch, Event* event)
{
	CCLOG("Paddle::onTouchBegan id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y);

	if (_state != kPaddleStateUngrabbed) return false;
	if ( !containsTouchLocation(touch) ) return false;
	m_oldPoint = touch->getLocation();
	_state = kPaddleStateGrabbed;
	CCLOG("return true");
	return true;
}

void TouchSprite3D::onTouchMoved(Touch* touch, Event* 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 Sets instead of 1 UITouch, so you'd need to loop through the set
	// in each touchXXX method.

	CCLOG("Paddle::onTouchMoved id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y);

	CCASSERT(_state == kPaddleStateGrabbed, "Paddle - Unexpected state!");    

	auto touchPoint = touch->getLocation();

	//setPosition( Vec2(touchPoint.x, getPosition().y) );
	setPosition(this->getPosition() + touchPoint - m_oldPoint);
	m_oldPoint = touchPoint;
}


void TouchSprite3D::onTouchEnded(Touch* touch, Event* event)
{
	CCASSERT(_state == kPaddleStateGrabbed, "Paddle - Unexpected state!");    

	_state = kPaddleStateUngrabbed;
} 

TouchSprite3D* TouchSprite3D::create3D( const std::string &modelPath )
{
    TouchSprite3D* _obj = new TouchSprite3D;
    _obj->autorelease();
	_obj->initWithFile(modelPath);

	return _obj;
}


你可能感兴趣的:(cocos2dx 可拖动的精灵类)