Lua 使用Cocos2d-x剪辑精灵

Lua 使用Cocos2d-x剪辑精灵,lua,cocos2d-x,Lua,Cocos2d X,我不能剪辑一个简单的精灵。我正在使用Cocos2d-x3.1rc0。这是代码 local sprite = cc.Sprite:create("red-box.png") local shape = cc.DrawNode:create() shape:drawPolygon({cc.p(0, 0), cc.p(0, 100), cc.p(100, 100), cc.p(100, 0), cc.p(0, 0)}, 5, cc.c4b(0, 0, 0, 1), 0, cc.c4b(0, 0, 0,

我不能剪辑一个简单的精灵。我正在使用Cocos2d-x3.1rc0。这是代码

local sprite = cc.Sprite:create("red-box.png")
local shape = cc.DrawNode:create()
shape:drawPolygon({cc.p(0, 0), cc.p(0, 100), cc.p(100, 100), cc.p(100, 0), cc.p(0, 0)}, 5, cc.c4b(0, 0, 0, 1), 0, cc.c4b(0, 0, 0, 1))
local clipper = cc.ClippingNode:create(shape)
clipper:setContentSize(sprite:getContentSize())
clipper:setScale(0.25)
clipper:setGlobalZOrder(20)
clipper:setPosition(10, 10)
clipper:setInverted(true)
clipper:addChild(sprite)
gameLayer:addChild(clipper)
这与问题中使用的模式相同:

  • (提供了一个很好的例子)
下面是一张描述问题的图片:

我尝试了多种不同的组合,例如设置clipper:setAlphaThreshold(0)。我甚至尝试实现此人的代码:

顺便说一下,depthFormat设置为GL_DEPTH24_STENCIL8_OES。所以这不是问题所在

P.>为什么模版的绘制在中间开始而不是左下角?我设置了剪刀的固定点和形状,但无法让它移动。困惑

以下是我的AppController.mm代码:

window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
                                 pixelFormat: kEAGLColorFormatRGBA8
                                 depthFormat: GL_DEPTH24_STENCIL8_OES
                          preserveBackbuffer: NO
                                  sharegroup: nil
                               multiSampling: NO
                             numberOfSamples: 0 ];

[eaglView setMultipleTouchEnabled:YES];
// Use RootViewController manage CCEAGLView
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
viewController.view = eaglView;

// Set RootViewController to window
if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
{
    // warning: addSubView doesn't work on iOS6
    [window addSubview: viewController.view];
}
else
{
    // use this method on ios6
    [window setRootViewController:viewController];
}

[window makeKeyAndVisible];

[[UIApplication sharedApplication] setStatusBarHidden: YES];

// IMPORTANT: Setting the GLView should be done after creating the RootViewController
cocos2d::GLView *glview = cocos2d::GLView::createWithEAGLView(eaglView);
cocos2d::Director::getInstance()->setOpenGLView(glview);

cocos2d::Application::getInstance()->run();
return YES;
window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen]bounds];
CCEAGLView*eaglView=[CCEAGLView视图带边框:[窗口边界]
像素格式:kEAGLColorFormatRGBA8
深度格式:GL_DEPTH24_模具8_OES
巴弗:没有
共享组:无
多重取样:否
样本数:0];
[eaglView SETMULTIPLETOUCHEBLED:是];
//使用RootViewController管理视图
viewController=[[RootViewController alloc]initWithNibName:nil bundle:nil];
viewController.WantFullScreenLayout=是;
viewController.view=eaglView;
//将RootViewController设置为Windows
如果([[UIDevice currentDevice].systemVersion floatValue]<6.0)
{
//警告:addSubView在iOS6上不工作
[窗口添加子视图:viewController.view];
}
其他的
{
//在ios6上使用此方法
[窗口设置RootViewController:viewController];
}
[WindowMakeKeyandVisible];
[[UIApplication sharedApplication]setStatusBarHidden:是];
//重要提示:应在创建RootViewController后设置GLView
cocos2d::GLView*GLView=cocos2d::GLView::createWithEAGLView(eaglView);
cocos2d::Director::getInstance()->setOpenGLView(glview);
cocos2d::Application::getInstance()->run();
返回YES;

这是我的解决方案。我使用此代码填充进度条

    local size = cc.Director:getInstance():getVisibleSize()
    local origin = cc.Director:getInstance():getVisibleOrigin()
    local empty = cc.Sprite:createWithSpriteFrame(cc.SpriteFrameCache:getInstance():getSpriteFrame(_frameName))
    -- Put the frame of the progress bar completely in view. 
    local bbox = empty:getBoundingBox()
    empty:setPosition(bbox.width/2, bbox.height/2)

    -- Background layer of frame (a nice transparent background)
    local bg = createSpriteWithOffset(_bgName, bbox)
    -- The layer that will fill the progress bar
    local fill = createSpriteWithOffset(_fillName, bbox)
    -- Text texture displayed over the progress bar
    local text = cc.Sprite:createWithSpriteFrame(cc.SpriteFrameCache:getInstance():getSpriteFrame(_textName))
    local tbbox = text:getBoundingBox()
    text:setPosition(tbbox.width+70+bbox.x, (tbbox.height*0.5)+bbox.y+bbox.height) -- Left adjust on top

    -- Stencil (masking layer)
    local mask = cc.c4b(0, 0, 0, 1)
    -- Number of fill pixels to show.
    local fillAmt = bbox.width * percent
    local polygon = {cc.p(0, 0), cc.p(0, bbox.height), cc.p(fillAmt, bbox.height), cc.p(fillAmt, 0), cc.p(0, 0)}
    local stencil = cc.DrawNode:create()
    stencil:drawPolygon(polygon, 5, mask, 0, mask)
    local clipper = cc.ClippingNode:create(stencil)
    local paper = cc.ClippingNode:create(stencil)
    paper:addChild(fill)
    paper:addChild(clipper)

    local render = cc.RenderTexture:create(bbox.width, bbox.height+(tbbox.height*0.5), cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888, gl.DEPTH24_STENCIL8_OES)
    render:begin()
    render:addChild(bg)
    render:addChild(paper)
    render:addChild(empty)
    render:addChild(text)
    bg:visit()
    paper:visit()
    empty:visit()
    text:visit()
    render:endToLua()
    local sprite = cc.Sprite:createWithTexture(render:getSprite():getTexture())
    sprite:setFlippedY(true)
    return sprite
下面是最终结果的图片

诀窍在于如何创建模具

始终记住,绘图从0,0开始;这有道理,对吗?还有,几点建议;最好缩放渲染纹理,而不是在渲染图像之前缩放正在渲染的纹理。如果在渲染之前将纹理缩放到最小大小,渲染质量将很低。最后,我发现在0,0处渲染图形要容易得多,而不是在渲染发生后将元素定位到您想要的位置。它使定位最终结果变得更加容易