代码还是放在HelloWorldScene中,主要是监听触摸事件的设置
声明文件
public:
//监听手势识别
virtual bool onTouchBegin(Touch *touch,Event *event);
private:
//点击得到pop对象
PopSprite *getPopStarSprite(Size size,Point touch);
实现文件
bool HelloWorld::onTouchBegin(Touch *touch,Event *event){
//获取触摸的X Y值
Point touchPO = touch->getLocation();
//获取屏幕大小
Size visibleSize = Director::getInstance()->getVisibleSize();
//得到了每个格子的位置,可以用来判读格子周围的一些
PopSprite *pops = getPopStarSprite(visibleSize,touchPO);
log("%i %i",pops->getPOPX(),pops->getPOPY());
return true;
}
//点击得到pop对象
PopSprite* HelloWorld::getPopStarSprite(Size size,Point touch){
//求出每个PopSprite的宽度和高度,正方形,宽度和高度相等
int len = (size.width - 28) / 10;
//求出点击X的位置
float x = (touch.x - 28) / len;
//求出点击Y的位置
float y = (touch.y - (size.height / 6 + 20)) / len;
if(x < 10 && y < 10 && x >= 0 && y >= 0){
int x1 = (touch.x - 28) / len;
int y1 = (touch.y - size.height / 6) / len;
return popStarSprite[x1][y1];
}else{
return NULL;
}
}
实现的效果图