您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x 使用lua实现像素点击检测

2015-08-28 10:42 507 查看
在做一些类似QQ农场游戏的时候,农田排列比较紧凑,每块田矩形透明区域难免会有重叠,那这种情况下如何正确区分开玩家点击了哪块田,这种需求就可以用像素检测来实现了。主要原理就是先判断玩家是否点击到一张图片的矩形区域,再判断玩家点击到的地方是否为该图片的透明区域。
楼主使用的cocos2d-x 3.2, 实现语言是C++和lua。
具体实现如下:
--@param node 用于判断是否被点击的节点--@param x      屏幕X坐标--@param y    屏幕Y坐标--@return     是否被点击function checkIfTouch(node, x, y )local nsp = node:convertToNodeSpace(cc.p(x, y))local contentSize = node:getContentSize()local rect = cc.rect(0, 0, contentSize.width, contentSize.height)return cc.rectContainsPoint(rect, nsp)end--@param sprite 用于判断是否被点击的图片(Sprite,ImageView测试可用)--@param x      屏幕X坐标--@param y    屏幕Y坐标--@return     非透明区域是否被点击function checkIfTouchWithTransparent(sprite, x, y)if not checkIfTouch(sprite, x, y) then return false endlocal oldPos = cc.p(sprite:getPosition())local size = sprite:getContentSize()local pos = sprite:convertToNodeSpace(cc.p(x, y))--以下三行代码主要在ScaleFactor不为1时有效(比如高清资源)local scale = cc.Director:getInstance():getContentScaleFactor()pos.x = pos.x * scalepos.y = pos.y * scalelocal render = cc.RenderTexture:create(size.width, size.height)sprite:setPosition(cc.p(size.width/2, size.height/2))render:begin()sprite:visit()render:endToLua()--该接口为自定义导出,原始cocos没有提供该接口(主要原因是Director没有对Lua暴露getRenderer接口)cc.Director:getInstance():flushScene()sprite:setPosition(oldPos)local image = render:newImage()pos.y = image:getHeight() - pos.y--该接口为自定义导出,原始cocos没有提供该接口(主要原因是Image没有对Lua暴露getData接口)local rgba = image:getPixelColor(pos)image:release()return (rgba.a ~= 0)end<pre name="code" class="cpp">//CCDirector.cpp接口实现void Director::flushScene(){_renderer->render();}//CCImage.cpp接口实现Color4B Image::getPixelColor( const Vec2& pos ){Color4B color = {0, 0, 0, 0};if ( pos.x < 0 || pos.x >= this->getWidth() || pos.y < 0 || pos.y >= this->getHeight() ) {return color;}auto data = this->getData();auto pixel = (unsigned int*) data;auto x = (int)pos.x;auto y = (int)pos.y;pixel = pixel + (y * this->getWidth() ) * 1 + x * 1;//R通道color.r = *pixel & 0xff;//G通道color.g = (*pixel >> 8) & 0xff;//B通过color.b = (*pixel >> 16) & 0xff;//Alpha通道,我们有用的就是Alphacolor.a = (*pixel >> 24) & 0xff;return color;}
这里新增了两个C++接口,主要是因为部分原始引擎接口没有对lua暴露,如果用C++语言开发的小伙伴就不需要新增这两个接口了

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: