cocos2d-x 自定义精灵类

这个博客介绍了如何创建一个自定义的cocos2d-x精灵类,该类继承自CCSprite并实现了触控事件处理。内容包括精灵的初始化、动画创建、触控开始、移动和结束时的行为,以及判断触摸点是否在精灵范围内。
摘要由CSDN通过智能技术生成
#ifndef _NEWSPRITE_H_
#define _NEWSPRITE_H_

#include "cocos2d.h"

using namespace cocos2d;

class MySprite : public CCSprite, public CCTargetedTouchDelegate
{
private:		
	bool isContainsTouchPoint(CCTouch * touch);
	CCRect rect();
	CCPoint edge(CCTouch * touch);

public:
	MySprite(void);
	~MySprite(void);
	
	static MySprite * createWithFileName(const char* name);	
	void createAnimation();
	virtual void onEnter();
	virtual void onExit();

	virtual bool ccTouchBegan(CCTouch * touch, CCEvent * event);
	virtual void ccTouchMoved(CCTouch * touch, CCEvent * event);
	virtual void ccTouchEnded(CCTouch * touch, CCEvent * event);
};
#endif
#include "NewSprite.h"

MySprite::MySprite(void)
{
}

MySprite::~MySprite(void)
{}

MySprite * MySprite::createWithFileName(const char * name)
{
	MySprite * pobSprite = new MySprite();
	if(pobSprite && pobSprite->initWithFile(name))
	{
		pobSprite->autorelease();
		return pobSprite;
	}
	CC_SAFE_DELETE(pobSprite);
	return NULL;
}

void MySprite::createAnimation()
{
	CCAnimation * anim =CCAnimation::create();
	char str[50] = {0};
	for(int i = 1; i < 9; i++)
	{
		sprintf(str, "grossini_dance_%02d.png", i);
		anim->addSpriteFrameWithFileName(str);
	}
	anim->setDelayPerUnit(0.2f);
	anim->setRestoreOriginalFrame(true);
	this->runAction(CCAnimate::create(anim));
}

bool MySprite::isContainsTouchPoint(CCTouch * touch)
{
	CCPoint touchLocation = touch->getLocationInView();
	touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);
	if(rect().containsPoint(touchLocation))
		return true;
	else
		false;
}

CCRect MySprite::rect()
{
	CCPoint thisPoint = this->getPosition();
	CCSize size = this->getContentSize();
	float x = thisPoint.x - size.width/2;
	float y = thisPoint.y - size.height/2;
	return CCRectMake(x, y, size.width, size.height);
}

CCPoint MySprite::edge(CCTouch * touch)
{
	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCSize thisSize = this->getContentSize();
	CCPoint touchLocation = touch->getLocationInView();
	touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);
	if(touchLocation.x < origin.x + thisSize.width/2)
		touchLocation.x = origin.x + thisSize.width/2;
	if(touchLocation.x > origin.x + visibleSize.width - thisSize.width/2)
		touchLocation.x = origin.x + visibleSize.width - thisSize.width/2;
	if(touchLocation.y < origin.y + thisSize.height/2)
		touchLocation.y = origin.y + thisSize.height/2;
	if(touchLocation.y > origin.y + visibleSize.height - thisSize.height/2)
		touchLocation.y = origin.y + visibleSize.height - thisSize.height/2;
	return touchLocation;
}

void MySprite::onEnter()
{
	CCSprite::onEnter();
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}

void MySprite::onExit()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
	CCSprite::onExit();
}

bool MySprite::ccTouchBegan(CCTouch * touch, CCEvent * event)
{
	if(isContainsTouchPoint(touch) && this->numberOfRunningActions() == 0)
	{
		createAnimation();
		return true;
	}
	else return false;
}

void MySprite::ccTouchMoved(CCTouch * touch, CCEvent * event)
{
	
	if(isContainsTouchPoint(touch))
		this->setPosition(edge(touch));

}

void MySprite::ccTouchEnded(CCTouch * touch, CCEvent * event)
{}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值