cocos2dx 我所不知的技巧

前端之家收集整理的这篇文章主要介绍了cocos2dx 我所不知的技巧前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、实现图片的平铺复制

local visibleSize = VisibleRect:getVisibleSize()
local texture = cc.Director:getInstance():getTextureCache():addImage("repeat.jpg")
texture:setTexParameters(gl.LINEAR,gl.LINEAR,gl.REPEAT,gl.REPEAT)
local sprite = cc.Sprite:createWithTexture(texture,cc.rect(0,visibleSize.width,visibleSize.height))
sprite:setPosition(cc.p(visibleSize.width/2,visibleSize.height/2))

实现的效果,将如下的图片实现重复拼接,直到覆盖满整个visiblesize

这里需要注意的是,图片的宽和高都必须是2的次幂,否则会报错

CCASSERT((_pixelsWide == ccNextPOT(_pixelsWide) || texParams.wrapS == GL_CLAMP_TO_EDGE) &&
        (_pixelsHigh == ccNextPOT(_pixelsHigh) || texParams.wrapT == GL_CLAMP_TO_EDGE),"GL_CLAMP_TO_EDGE should be used in NPOT dimensions");
int ccNextPOT(int x)
{
    x = x - 1;
    x = x | (x >> 1);
    x = x | (x >> 2);
    x = x | (x >> 4);
    x = x | (x >> 8);
    x = x | (x >>16);
    return x + 1;
}

猜你在找的Cocos2d-x相关文章