3.10 調(diào)通了
G:\Mycocos310Cpp\touchAlpha\Classes\HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : 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();
bool onTouchBegan(cocos2d::Touch* pTouch, cocos2d::Event* pEvent);
//void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchEnded(cocos2d::Touch* pTouch, cocos2d::Event* pEvent);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
private:
cocos2d::CCSprite* m_imgMan;
cocos2d::CCRenderTexture* m_pRenderTexture;
cocos2d::LabelTTF* m_pLabTips;
cocos2d::EventListenerTouchOneByOne* _touchListener;
};
#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
using namespace cocostudio::timeline;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
m_imgMan = NULL;
m_pRenderTexture = NULL;
m_pLabTips = NULL;
m_imgMan = CCSprite::create("ui_force_20001.png");
m_imgMan->setPosition(ccp(100, 100));
m_imgMan->setAnchorPoint(Vec2(0.5f, 0.5f));
addChild(m_imgMan, 1);
//這里參數(shù)很固定 沒(méi)有什么可改的余地
//參數(shù)1 你需要渲染出來(lái)區(qū)域的寬
//參數(shù)2 你需要渲染出來(lái)區(qū)域的高
//參數(shù)3 必須是這個(gè)模式 否則后面復(fù)制內(nèi)存好像會(huì)有問(wèn)題Texture2D::PixelFormat::RGBA8888
m_pRenderTexture = RenderTexture::create(m_imgMan->getContentSize().width, m_imgMan->getContentSize().height, Texture2D::PixelFormat::RGBA8888);
m_pRenderTexture->setPosition(ccp(100, 100));
//m_pRenderTexture->setAnchorPoint(Vec2(0, 0));//設(shè)置錨點(diǎn)無(wú)效 錨點(diǎn)默認(rèn)0.5f, 0.5f
addChild(m_pRenderTexture, 2);
// Adds touch event listener
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
_eventDispatcher->addEventListenerWithFixedPriority(listener, 1);
_touchListener = listener;
return true;
}
bool HelloWorld::onTouchBegan(cocos2d::Touch *pTouch, cocos2d::Event *pEvent) {
//CCLOG("123");
bool isTouched = false;
CCPoint touchPoint = pTouch->getLocationInView();
CCPoint glPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);
if (m_imgMan->boundingBox().containsPoint(glPoint)) {
Color4B color4B = { 0, 0, 0, 0 };
CCPoint nodePos = m_imgMan->convertTouchToNodeSpace(pTouch);
unsigned int x = nodePos.x;
unsigned int y = m_imgMan->getContentSize().height - nodePos.y;
CCPoint point = m_imgMan->getPosition();
//開(kāi)始準(zhǔn)備繪制
m_pRenderTexture->begin();
//繪制使用的臨時(shí)精靈床绪,與原圖是同一圖片
CCSprite* pTempSpr = CCSprite::createWithSpriteFrame(m_imgMan->displayFrame());
pTempSpr->setPosition(ccp(pTempSpr->getContentSize().width / 2, pTempSpr->getContentSize().height / 2));
//繪制
pTempSpr->visit();
//結(jié)束繪制
m_pRenderTexture->end();
//解決方案原文 找了一下午原因 竟是這樣 http://blog.csdn.net/super_level/article/details/41707687
Director::getInstance()->getRenderer()->render();//在3.0此處必須寫(xiě)上這個(gè),否則newImage整張圖片都為黑色,或者在下一幀獲取
//通過(guò)畫(huà)布拿到這張畫(huà)布上每個(gè)像素點(diǎn)的信息,封裝到CCImage中
Image* pImage = m_pRenderTexture->newImage();
//獲取像素?cái)?shù)據(jù)
unsigned char* data_ = pImage->getData();
unsigned int *pixel = (unsigned int *)data_;
pixel = pixel + (y * (int)pTempSpr->getContentSize().width) * 1 + x * 1;
//R通道
color4B.r = *pixel & 0xff;
//G通道
color4B.g = (*pixel >> 8) & 0xff;
//B通過(guò)
color4B.b = (*pixel >> 16) & 0xff;
//Alpha通道,我們有用的就是Alpha
color4B.a = (*pixel >> 24) & 0xff;
CCLOG("cur click color: alpha = %d", color4B.a);
if (color4B.a > 50) {
isTouched = true;
}
else {
isTouched = false;
}
//繪制完成后清理畫(huà)布的內(nèi)容
m_pRenderTexture->clear(0, 0, 0, 0);
}
if (m_pLabTips) {
m_pLabTips->removeFromParentAndCleanup(true);
m_pLabTips = NULL;
}
return isTouched;
}
void HelloWorld::onTouchEnded(cocos2d::Touch *pTouch, cocos2d::Event *pEvent) {
if (m_pLabTips) {
m_pLabTips->removeFromParentAndCleanup(true);
m_pLabTips = NULL;
}
m_pLabTips = CCLabelTTF::create("點(diǎn)擊到非透明的像素點(diǎn)", "Courier", 30);
m_pLabTips->setAnchorPoint(Vec2(0.5f, 0.5f));
m_pLabTips->setPosition(ccp(300.0f, 100.0f));
m_pLabTips->setColor(ccc3(150, 150, 100));
addChild(m_pLabTips, 1);
}
點(diǎn)擊白色圖片 以及邊緣區(qū)域 測(cè)試結(jié)果ui_force_20001.png
image.png
image.png