cocos2d-x3.x 像素碰撞
2014-08-20 17:30
85 查看
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" using namespace cocos2d; 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(); // a selector callback void menuCloseCallback(cocos2d::Ref* pSender); // implement the "static create()" method manually CREATE_FUNC(HelloWorld); cocos2d::EventListenerTouchOneByOne *_listener; cocos2d::Sprite *bulletForTower12; cocos2d::Sprite *m_imgMan; cocos2d::RenderTexture *m_pRenderTexture; cocos2d::Label *m_pLabTips; virtual bool onTouchBegan(Touch *touch, Event *unused_event); virtual void onTouchEnded(Touch *touch, Event *unused_event); }; #endif // __HELLOWORLD_SCENE_H__
<pre name="code" class="cpp">#include "HelloWorldScene.h"USING_NS_CC;Scene* HelloWorld::createScene(){// 'scene' is an autorelease objectauto scene = Scene::create();// 'layer' is an autorelease objectauto layer = HelloWorld::create();// add layer as a child to scenescene->addChild(layer);// return the scenereturn scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){//////////////////////////////// 1. super init firstif ( !Layer::init() ){return false;}m_imgMan = Sprite::create("tangfa09/tafang09-1-zidan0002.png");m_imgMan->setPosition(Vec2(400, 200));this->addChild(m_imgMan, 1);m_pRenderTexture = RenderTexture::create(m_imgMan->getContentSize().width, m_imgMan->getContentSize().height, Texture2D::PixelFormat::RGBA8888);m_pRenderTexture->ignoreAnchorPointForPosition(true);m_pRenderTexture->setPosition(Vec2(400, 200));m_pRenderTexture->setAnchorPoint(Vec2::ZERO);this->addChild(m_pRenderTexture, 0, 1);m_pRenderTexture->setVisible(false); //将绘制出来的图片隐藏_listener=EventListenerTouchOneByOne::create();_listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);_listener->onTouchEnded=CC_CALLBACK_2(HelloWorld::onTouchEnded, this);Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_listener, this);return true;}bool HelloWorld::onTouchBegan(Touch *touch, Event *unused_event){bool isTouched = false;auto touchPoint = touch->getLocationInView();auto glPoint = Director::getInstance()->convertToGL(touchPoint);if (m_imgMan->boundingBox().containsPoint(glPoint)) {Color4B color4B = {0, 0, 0, 0};Point nodePos = m_imgMan->convertTouchToNodeSpace(touch);unsigned int x = nodePos.x;unsigned int y = m_imgMan->getContentSize().height - nodePos.y;Point point = m_imgMan->getPosition();//开始准备绘制//绘制完成后清理画布的内容m_pRenderTexture->beginWithClear(0, 0, 0, 0);//绘制使用的临时精灵,与原图是同一图片auto pTempSpr = Sprite::createWithSpriteFrame(m_imgMan->displayFrame());pTempSpr->setPosition(Vec2(pTempSpr->getContentSize().width / 2, pTempSpr->getContentSize().height / 2));//绘制pTempSpr->visit();//结束绘制m_pRenderTexture->end();//通过画布拿到这张画布上每个像素点的信息,封装到CCImage中Image* pImage = m_pRenderTexture->newImage();//获取像素数据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通道color4B.b = (*pixel >> 16) & 0xff;//Alpha通道,我们有用的就是Alphacolor4B.a = (*pixel >> 24) & 0xff;CCLOG("当前点击的点的: alpha = %d", color4B.a);if (color4B.a > 50) {isTouched = true;} else {isTouched = false;}}if (m_pLabTips) {m_pLabTips->removeFromParentAndCleanup(true);m_pLabTips = NULL;}return isTouched;}void HelloWorld::onTouchEnded(Touch *touch, Event *unused_event){if (m_pLabTips) {m_pLabTips->removeFromParentAndCleanup(true);m_pLabTips = NULL;}m_pLabTips = Label::createWithTTF("NO", "fonts/Marker Felt.ttf", 30);m_pLabTips->setAnchorPoint(Vec2::ZERO);m_pLabTips->setPosition(Vec2(300.0f, 100.0f));m_pLabTips->setColor(Color3B::YELLOW);this->addChild(m_pLabTips, 1);}
相关文章推荐
- (转)cocos2d-x 不规则图形碰撞检测(像素级碰撞检测)
- cocos2d-x快乐的做让人快乐的游戏4:像素碰撞
- 像素完美碰撞检测(使用cocos2d-x)
- cocos2d-x3.x 像素碰撞
- 为什么cocos2d-x3.x版本的碰撞监听不起作用?因为没有设置掩码
- cocos2d-x像素碰撞检测
- COCOS2D-X 像素级碰撞检测
- (转)像素完美碰撞检测(使用cocos2d-x)
- cocos2d-x检测碰撞,并显示标记
- 基于cocos2d-x对俯视角游戏碰撞检测及碰撞处理的探究
- cocos2d-x 碰撞
- 基于cocos2d-x的跑酷游戏,不同高度地面的碰撞检測demo,有兴趣能够看一看
- Cocos2d-x 3.0中 物理碰撞检測中onContactBegin回调函数不响应问题
- cocos2d纹理像素格式
- Cocos2d-x教程(35)-三维拾取Ray-AABB碰撞检测算法
- cocos2d-x 2.0.1 的根据例子写个小球碰撞板块反弹例子
- cocos2d-x3.x实现屏幕画线并添加刚体属性
- cocos2d-x 3.2 物理碰撞机制
- cocos2d-x像素级触摸处理
- 在cocos2d里面如何使用Texture Packer和像素格式来优化spritesheet