Using CCScrollView in Cocos2d V3

1k Views Asked by At

I'm wondering if anyone can help me with this. I want to add 4 CCScrollViews, that will scroll horizontally, on a CCNode. The CCNode, positioned and held on the device in portrait mode, will fill the entire screen with a normalized contentSize set to cover the entire screen.

So in essence the scroll views will pile on top of each other with the 4th being at the bottom of the screen and the 1st being at the top. Now, I have managed to add the 4 CCScrollViews but only one responds to touches. The others are flat an unmovable. It's almost as though the last CCScrollView added to the node is overlaying the other three and it is the only thing responding to touch requests. All 4 CCScrollViews have their delegate property set to self.

I'm someone coming at Cocos2d with a fair bit of UIKit experience. So, i'm trying to apply my UIScrollView mode of thinking to all of this. Having had a good Google about and coming up with little, I'm wondering if SO can help. I've even been considering winding UIScrollView into Cocos2d.

I guess my main issues here are two-fold. One, I have an issue with touch response. Two, I have an issue with the paging aspect and control of content-size. Via trial and error, I'm sort of getting along but if someone could perhaps write up a bit of a best-practive guide to CCScrollView implementation, specifically where one does not set the contentSize of the CCNode or CCspriteFrame that's added to larger contentView does not fill the entire width of the screen.

Thanks in advance.

#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];
}
0

There are 0 best solutions below