cocos2dx 3.x中利用RenderTexture检测触摸点的alpha透明度的值,可用于不规则图形的触控判断。
折腾了一两天,终于搞定,搜到的文章都没有详细解释,下面给出自己写的详细的代码和注释。
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
USING_NS_CC;
class HelloWorldScene : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorldScene);
Sprite* clickableSprite;
bool onTouchBegan2(Touch* touch, Event* event);
CustomCommand checkAlphaCommand;
void onCheckAlphaCommand(Vec2 pos);
RenderTexture* renderTex;
};
#endif // __HELLOWORLD_SCE