iPad应用程序设置:场景A包含层A-1024x768。按下图层a中的按钮,图层B使用CCMoveTo操作从顶部向下移动。层B只有800x600,所以你可以看到它后面的层A(想想重叠的暂停屏幕类型的效果)。LayerB包含一个800x600的精灵,用户可以通过按下按钮放大它。缩放效果只是CCScaleTo和CCMoveTo的组合,以使其集中在放大的部分上。然而,当精灵缩放时,B层也会覆盖a层。有没有办法在包含的窗口中缩放精灵?
LayerB应该使用GL_SCISSOR_TEST
来修剪其外部。你可以很容易地在谷歌上搜索更多关于它的信息,它基本上定义了一个rect
,然后在上面使用glScissor
来去除外部。我有一个类,当我需要这样做时,我会扩展它,如下所示:
//
// CCNodeClip.h
//
// Created by Ignacio Orlandoni on 7/29/11.
//
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface CCNodeClip : CCLayer {
}
-(void)preVisit;
-(void)postVisit;
@end
-
//
// CCNodeClip.m
//
// Created by Ignacio Orlandoni on 7/29/11.
//
#import "CCNodeClip.h"
@implementation CCNodeClip
-(void)visit {
[self preVisit];
[super visit];
[self postVisit];
}
-(void)preVisit {
if (!self.visible)
return;
glEnable(GL_SCISSOR_TEST);
CGPoint position = [self position];
//I don't remember if this rect really serves for both orientations, so you may need to change the order of the values here.
CGRect scissorRect = CGRectMake(position.x, position.y, [self contentSize].width, [self contentSize].height);
// CCLOG(@"Scrissor Rect: X: %02f, Y:%02f, W: %02f, H: %02f", scissorRect.origin.x, scissorRect.origin.y, scissorRect.size.width, scissorRect.size.height);
// Handle Retina
scissorRect = CC_RECT_POINTS_TO_PIXELS(scissorRect);
glScissor((GLint) scissorRect.origin.x, (GLint) scissorRect.origin.y,
(GLint) scissorRect.size.width, (GLint) scissorRect.size.height);
}
-(void)postVisit {
glDisable(GL_SCISSOR_TEST);
}
@end
将其导入LayerB
后,现在可以将其定义为CCNodeClip
而不是CCLayer
。
一些链接
gl剪刀<lt;cocos2d论坛
在cocos2d<lt;StackOverflow
Cocos2d iPhone-雪碧剪辑/面具/相框<lt;StackOverflow
另一个Cocos2D宝石:ClippingNode<lt;Learn-Coocos2d.com
附带说明
如果精灵的锚点居中,则可以避免CCScaleTo+CCMoveTo,因此图像在缩放时保持在容器中居中。(.anchorPoint = ccp(0.5, 0.5);
)