#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)
{}