cocos2dx rect着色(cocos2d-x 和cocos2d 中CCRectContainsPoint的不同)

本文目录
- cocos2d-x 和cocos2d 中CCRectContainsPoint的不同
- cocos2dx精灵帧加载出问题
- 关于cocos2d中的intersectsRect问题
- cocos2dx怎么得到一个指定layer
- cocos2dx怎么过滤掉spritesheet的背景色
cocos2d-x 和cocos2d 中CCRectContainsPoint的不同
cocos2d是OC写的,cocos2dx是c++写的
cocos2d只能在ios下运行,cocos2dx是跨平台的,ios和android平台都可以运行
cocos2d是外国人搞的,cocos2dx是中国人搞的。
cocos2dx是cocos2d的C++写法,但是游戏架构是一样的,都包含了精灵,导演,场景,动作等概念,他们是一脉相承的东西。你可以直接研究cocos2dx,没有什么障碍。虽然是有了cocos2d才有的cocos2dx,但是cocos2dx包含了cocos2d的主要思想,因此可以直接研究cocos2dx。
cocos2dx精灵帧加载出问题
先CCTexture2D* EntityTexture=CCTextureCache::sharedTextureCache()-》addImage(addr);
再frame=CCSpriteFrame::createWithTexture(EntityTexture,cocos2d::CCRectMake(0,0,x,y));
加载资源的问题一般都会在输出中有错误提示的
关于cocos2d中的intersectsRect问题
你自己选中以后,按F12 ,追踪进去看一下,他不是Layer也是sprite ,就相当于是一个矩形检测是否碰撞
cocos2dx怎么得到一个指定layer
--创建一个touch的半透明layer
--priority : touch 权限级别,默认为-1024
--touchRect: 在touchRect 区域会放行touch事件 若touchRect = nil 则全屏吃touch
--touchCallback: 屏蔽层touch 回调
function createMaskLayer( priority,touchRect ,touchCallback, layerOpacity,highRect)
local layer = CCLayer:create()
layer:setPosition(ccp(0, 0))
layer:setAnchorPoint(ccp(0, 0))
layer:setTouchEnabled(true)
layer:setTouchPriority(priority or -1024)
layer:registerScriptTouchHandler(function ( eventType,x,y )
if(eventType == "began") then
if(touchRect == nil) then
if(touchCallback ~= nil) then
touchCallback()
end
return true
else
if(touchRect:containsPoint(ccp(x,y))) then
return false
else
if(touchCallback ~= nil) then
touchCallback()
end
return true
end
end
end
print(eventType)
end,false, priority or -1024, true)
local gw,gh = g_winSize.width, g_winSize.height
if(touchRect == nil) then
local layerColor = CCLayerColor:create(ccc4(0,0,0,layerOpacity or 150),gw,gh)
layerColor:setPosition(ccp(0,0))
layerColor:setAnchorPoint(ccp(0,0))
layer:addChild(layerColor)
return layer
else
local ox,oy,ow,oh = touchRect.origin.x, touchRect.origin.y, touchRect.size.width, touchRect.size.height
local layerColor = CCLayerColor:create(ccc4(0, 0, 0, layerOpacity or 150 ), gw, gh)
local clipNode = CCClippingNode:create();
clipNode:setInverted(true)
clipNode:addChild(layerColor)
local stencilNode = CCNode:create()
-- stencilNode:retain()
local node = CCScale9Sprite:create("bg.png");
node:setContentSize(CCSizeMake(ow, oh))
node:setAnchorPoint(ccp(0, 0))
node:setPosition(ccp(ox, oy))
stencilNode:addChild(node)
if(highRect ~= nil) then
local highNode = CCScale9Sprite:create("bg.png");
highNode:setContentSize(CCSizeMake(highRect.size.width, highRect.size.height))
highNode:setAnchorPoint(ccp(0, 0))
highNode:setPosition(ccp(highRect.origin.x, highRect.origin.y))
stencilNode:addChild(highNode)
end
clipNode:setStencil(stencilNode)
clipNode:setAlphaThreshold(0.5)
layer:addChild(clipNode)
end
return layer
end
cocos2dx怎么过滤掉spritesheet的背景色
Here’s an explanation of the keys.
frame
The sprite location within the sprite-sheet as position and size values.
offset
Most of the current texture packing tools remove transparent areas of the image before they pack it. Imagine you got a sprite with the size of 100x100, but on the left and right there are 4 columns of totally transparent pixels. The packing tool will remove these to save space, but store offset andsourceSize, so that your sprite will be 100x100 pixels in your game. Note that the offset is always relative to the center of the sprite (comparing the center of the cropped and the center of the original sprite).
rotated
Whether or not the sprite has been rotated within the sprite-sheet (if this is true, the sprite is rotated 90 degrees clockwise).
sourceColorRect
The rectangle with actual color information inside your source sprite. If you take the 100x100 image described above (with 4px transparent pixels on the left and on the right), the sourceColorRect will be {{4,0},{92,100}}
sourceSize
This is the size of the original sprite (beware that some texture-packers can add padding to this if you chose to).
个人理解:
sourceSize:
动画的尺寸,如果一个动画有很多帧的话,所有帧的sourceSize是不变的,基本上可以理解为这么多帧中最大显示范围的那个尺寸
frame:
标示这一帧的截图,在整个spritesheet中的位置和尺寸
offset:
标示这个截图的中心点,和整个动画的中心点(以sourceSize这个方形的中心点),的xy差值,记得这个是以cocos2d的坐标系为准的,y轴向上为正
sourceColorRect
这个截图在sourceSize这个方形的位置和尺寸(均以左上角来算)
值得注意的是:
搜索一下realTextureFileName和textureFileName,这两个key下面的string属性必须是对应spritesheet的文件名!有时候改了plist和png的文件名,忘记改这俩个值,则会加载不了图片

更多文章:
方舟生存进化新物种图鉴中英文对照图鉴 庞马资料图鉴 庞马(Equus magnus)?植物大战僵尸英文版图鉴
2026年9月22日 00:10
html document对象(载入浏览器的html文档都会成为document的对象吗)
2026年9月21日 16:00
安装包怎样下载mysql(如何在官网上下载可安装版的MySQL数据库)
2026年9月21日 15:30
威廉尖叫音效在线(渴求《勇敢的心》的配乐a gift of a thistle下载)
2026年9月21日 15:20
wordpress建站教程视频(WordPress建站的基本流程)
2026年9月21日 14:00





