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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值