0

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];
}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Carl Hine
  • 1,817
  • 18
  • 24
  • I have no problem having 3 scroll views simultaneously on-screen, all three functional and well confined gesture recognizers, so this is probably not about " ... the last one added to the scene". – YvesLeBorg Oct 24 '14 at 10:34
  • regarding paging, I found an issue with vertical scrolling when the last page is a 'fragment' of the scroll view size. I am currently trying to figure out if this error is mine or not, but am about to post a bug report on github. Maybe horizontal scrolling suffers from similar woes. – YvesLeBorg Oct 24 '14 at 10:37
  • 1
    Be sure that the contentSize of the scroll view fits exactly the area where it needs to respond to touches. That and the scroll view's position should define where it will respond to touches. – CodeSmile Oct 24 '14 at 10:47
  • also, add scrollView to a parent before setting content size, position, etc. Normalized can be a source of surprises ! – YvesLeBorg Oct 24 '14 at 10:59
  • Cheers for the all for the quick responses. So, in terms of, being sure about making the contentSize of the scroll view exactly where I want it to respond to touches, I'm currently specifying the contentSizeType as being normalized? Would you not do this? Would you be screen-coordinate specific instead? Is that perhaps where I'm going wrong? And in terms of specifying the scroll view position, is it the CCScrollView.position that I alter, not the CCNode that represents the content, as is in my case, above, CCNode *scrollViewContents = [CCNode node]; – Carl Hine Oct 24 '14 at 11:57
  • Also, there does seem to be very little in tutorial terms out there for the CCScrollView. This sort've tells me I'm missing something completely. Most of what I can find discusses setting the width of the CCSprite as the width of the screen. And then to add each CCSprite to the contentView. But before doing so, create contentView that's all widths of the CCSprite added together. How then would I create a contentView that's full of sprites that are say a third width of the screen and have them page across the screen horizontally with perhaps a modest gap between each? – Carl Hine Oct 24 '14 at 12:03
  • @CarlHine : i gave a working example code sample here : http://stackoverflow.com/questions/26521173/ccscrollview-scroll-and-touch-events-never-firing/26532544#26532544 . That is (inPoints, Vertical), but 3 of those cohabit nicely on screen. – YvesLeBorg Oct 24 '14 at 12:03
  • @YvesLeBorg. Great!!! I'll take a look. – Carl Hine Oct 24 '14 at 12:04

0 Answers0