tôi đã cố gắng sử dụng ClippingNode Steffen Itterheim, nhưng đã không thể bắt đầu làm việc trong một đủ mạnh mẽ đủ thời trang cho nhu cầu của tôi.
Tin hay không, mã bên dưới hoạt động khá tốt và phải hoàn thành mã. Nó xử lý các thay đổi định hướng thiết bị, anchorPoint, vị trí, tỷ lệ (scaleX, scaleY). Đối với cocos2d v2, bạn có thể chỉ cần nhận xét các cuộc gọi glPushMatrix và glPopMatrix ..
Để sử dụng, chỉ cần đặt vị trí và contentSize thuộc tính và thêm con/con bạn muốn cắt bớt vào thể hiện ClippingNode này. Thuộc tính contentSize được sử dụng để xác định kích thước của vùng cắt.
example of usage:
ClippingNode *clipNode = [[ClippingNode alloc] init];
clipNode.anchorPoint = ccp(0.5f, 0);
clipNode.position = ccp(100, 25);
clipNode.contentSize = CGSizeMake(120, 120);
// add clipNode to your node hierarchy.
[parentNode addChild:clipNode];
// add one or more children to your clipNode:
[clipNode addChild:child1];
// ClippingNode.h
// CC0 - (public domain. Use in anyway you see fit.)
// No warranty of any kind is expressed or implied.
//
// by UChin Kim.
//
// the caller can simply set the regular cocos2d
// properties: position and contentSize to define the clipping region implicitly (i.e. the
// position and contentSize of the ClippingNode is the clipping region to be used).
// as an added bonus, position seems to work as expected (relative to parent node, instead of
// requiring absolute positioning).
//
// also, anchorPoint and scale properties seem to work as expected as well..
// no special code is neeed to handle device orientation changes correctly..
//
// To visually see exactly what is being clipped, set the following #define
// #define SHOW_CLIPPED_REGION_IN_LIGHT_RED 1
//
#import "cocos2d.h"
@interface ClippingNode : CCNode
@end
//
// ClippingNode.m
//
#import "ClippingNode.h"
@implementation ClippingNode
-(void) visit
{
CGPoint worldOrg = [self convertToWorldSpace:ccp(0, 0)];
CGPoint dest = [self convertToWorldSpace:ccp(self.contentSize.width, self.contentSize.height)];
CGPoint dims = ccpSub(dest, worldOrg);
glPushMatrix();
glEnable(GL_SCISSOR_TEST);
glScissor(worldOrg.x, worldOrg.y, dims.x, dims.y);
#if SHOW_CLIPPED_REGION_IN_LIGHT_RED
glColor4ub(64, 0, 0, 128);
ccDrawSolidRect(ccp(0, 0), ccp(1024, 1024));
#endif
[super visit];
glDisable(GL_SCISSOR_TEST);
glPopMatrix();
}
@end
Một lưu ý khác - vùng cắt không liên quan đến sprite, nó dựa trên thế giới. Vì vậy, nó sẽ vẫn ở vị trí không có vấn đề làm thế nào bạn chuyển đổi các sprite. Bạn sẽ phải di chuyển/mở rộng thủ công nó cùng với sprite nếu bạn cần mặt nạ để di chuyển. – jtalarico
Vâng, bạn nói đúng. Tôi không nhận ra điều đó lúc đầu. Bây giờ tôi đã thực hiện x và y so với vị trí của chế độ xem của tôi để chúng luôn di chuyển cùng nhau. – Bach
@jtalarico, bạn có biết nơi để có được biến đổi sprite trong khi chuyển đổi bằng cách sử dụng: 'CCDirector :: sharedDirector() -> pushScene (CCTransitionSlideInL :: create (10.0f, pScene));' Cảm ơn! – Zennichimaro