关于cocos2d-x的精灵touch消息监听Demo.
//
// TSSpirte.h
// TSSpiriteTouch
//
// Created by TSEnel on 13-2-23.
//
//
#ifndef __TSSpiriteTouch__TSSpirte__
#define __TSSpiriteTouch__TSSpirte__
#include <iostream>
#include "cocos2d.h"
using namespace cocos2d;
class TSSprite : public CCSprite ,public CCTouchDelegate
{
public:
static TSSprite* create(const char *pszFileName);
public:
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 /* defined(__TSSpiriteTouch__TSSpirte__) */
//
// TSSpirte.cpp
// TSSpiriteTouch
//
// Created by TSEnel on 13-2-23.
//
//
#include "TSSpirte.h"
TSSprite* TSSprite::create(const char *pszFileName)
{
TSSprite *pobSprite = new TSSprite();
if (pobSprite && pobSprite->initWithFile(pszFileName))
{
pobSprite->autorelease();
return pobSprite;
}
CC_SAFE_DELETE(pobSprite);
return NULL;
}
void TSSprite::onEnter()
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
CCSprite::onEnter();
}
void TSSprite::onExit()
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->removeDelegate(this);
CCSprite::onExit();
}
bool TSSprite::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
CCPoint sLocalPos = convertToNodeSpace(touch->getLocation());
CCRect sRC = CCRect(getPositionX() - getContentSize().width * getAnchorPoint().x,
getPositionY() - getContentSize().height * getAnchorPoint().y,
getContentSize().width, getContentSize().height);
sRC.origin = CCPointZero;
bool isTouched = sRC.containsPoint(sLocalPos);
if(isTouched)
{
CCLog("点中了!! x:%d y:%d", (int)sLocalPos.x, (int)sLocalPos.y);
return true;
}
else
{
CCLog("没点中了!! x:%d y:%d", (int)sLocalPos.x, (int)sLocalPos.y);
return false;
}
}
void TSSprite::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
}
void TSSprite::ccTouchEnded(CCTouch* pTouch, CCEvent* event)
{
}
代码已经上传GitHub. 可以取来玩:
https://github.com/spiritring/TSSpiriteTouch.git 点击打开链接