0

我想知道是否有人可以帮助我解决这个问题。我想在 CCNode 上添加 4 个 CCScrollView,它们将水平滚动。CCNode 以纵向模式定位并保持在设备上,将使用标准化的 contentSize 设置来填充整个屏幕以覆盖整个屏幕。

因此,本质上,滚动视图将堆叠在一起,第 4 个位于屏幕底部,第 1 个位于顶部。现在,我设法添加了 4 个 CCScrollView,但只有一个响应触摸。其他的都是平的,不动的。就好像最后一个添加到节点的 CCScrollView 覆盖了其他三个,并且它是唯一响应触摸请求的东西。所有 4 个 CCScrollView 都将其委托属性设置为 self。

我是来 Cocos2d 的,有相当多的 UIKit 经验。所以,我正在尝试将我的 UIScrollView 思维模式应用于所有这些。有一个很好的谷歌并提出了一点点,我想知道是否可以提供帮助。我什至一直在考虑将 UIScrollView 缠绕到 Cocos2d 中。

我想我的主要问题有两个。一,我有触摸响应的问题。第二,我对分页方面和内容大小的控制有疑问。通过反复试验,我有点相处,但如果有人可能会写一些关于 CCScrollView 实现的最佳实践指南,特别是没有设置添加到更大 contentView 的 CCNode 或 CCspriteFrame 的 contentSize 的地方不填满屏幕的整个宽度。

提前致谢。

#import "CCToolKit.h"
#import "GameShip.h"

typedef enum ShipPart
{
    ShipPartHead,
    ShipPartBody,
    ShipPartWings,
    ShipPartBoosters
} ShipPart;

@implementation GameShip

-(instancetype)init {
    if (self = [super init]) {
        [self addBackground];
        [self addScrollTo:ShipPartHead forQtyOfParts:3];
        [self addScrollTo:ShipPartBody forQtyOfParts:4];
        [self addScrollTo:ShipPartWings forQtyOfParts:3];
        [self addScrollTo:ShipPartBoosters forQtyOfParts:3];

    }

    return self;

}

-(void)addBackground {

    CCSprite *bg = [CCSprite spriteWithImageNamed:kGameMainBackGround];
    bg.positionType = CCPositionTypeNormalized;
    bg.position = ccp(0.5f,0.5f);
    [self addChild:bg];

}

-(void)addScrollTo:(ShipPart)shipPart forQtyOfParts:(int)partQty {

    NSString *imageFileNameSegment;
    switch (shipPart) {
        case ShipPartHead:
            imageFileNameSegment = @"head";
            break;

        case ShipPartBody:
            imageFileNameSegment = @"body";
            break;

        case ShipPartWings:
            imageFileNameSegment = @"wings";
            break;

        case ShipPartBoosters:
            imageFileNameSegment = @"boosters";
            break;

        default:
            break;
    }

    CCNode *scrollViewContents = [CCNode node];
    scrollViewContents.contentSizeType = CCSizeTypeNormalized;
    scrollViewContents.contentSize = CGSizeMake(partQty * 0.65, 0.25f);
    NSLog(@"scrollView,height %f", scrollViewContents.boundingBox.size.height);

    for (int i = 1; i <= partQty; i++) {
        NSString *imageFileName = [NSString stringWithFormat:@"%@%d.png", imageFileNameSegment, i];

        CCSprite *shipPartSprite = [CCSprite spriteWithImageNamed:imageFileName];
        shipPartSprite.positionType = CCPositionTypeNormalized;
        shipPartSprite.position = ccp((i + 0.5f) / partQty, 0.5f);

        [scrollViewContents addChild:shipPartSprite];
    }

    CCScrollView *scrollView = [[CCScrollView alloc] initWithContentNode:scrollViewContents];
    scrollView.pagingEnabled = YES;
    scrollView.horizontalScrollEnabled = YES;
    scrollView.verticalScrollEnabled = NO;
    scrollView.color = [CCColor redColor];
    scrollView.contentSize = CGSizeMake(0.5f, 0.25f);
    scrollView.positionType = CCPositionTypeNormalized;
    scrollView.position = ccp(-1.0f, ((shipPart * 0.25f) -0.1f));
    scrollView.delegate = self;
    //scrollView.description = [NSString stringWithFormat:@"%d", shipPart];

    [self addChild:scrollView];
    //[scrollView setHorizontalPage:1];
}
4

0 回答 0