Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit and Scrollable List

I've been trying to figure out an "easy" way to have a scrollable vertical list for my SpriteKit game.

I'm going to use it to implement the shop / upgrades part of my game so I would like the user to be able to scroll down the list to see the different upgrades that are possible.

I've seen quite a few posts on integrating a UIScrollView into SpriteKit but most of them seem to scroll the whole screen.

What I have so far is:

-(void) createSceneContents
{
    [super createSceneContents];




    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10,100, 250, 200)];
    _scrollView.contentSize                     = CGSizeMake(2000, 120);
    _scrollView.scrollEnabled                   = YES;
    _scrollView.showsHorizontalScrollIndicator  = YES;
    _scrollView.backgroundColor                 = [UIColor brownColor];

    [self.view addSubview:_scrollView];

    //Test sprites for scrolling and zooming
    SKSpriteNode *greenTestSprite = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor]
                                                                 size:CGSizeMake(25, 25)];
    [greenTestSprite setName:@"greenTestSprite"];
    greenTestSprite.position = CGPointMake(25, 125);



    [self addChild:greenTestSprite];

    //[self addChild: [self newMenuNode]];
}

I'm trying to think what the easiest way is for me to be able to add the green test sprite into that UIScrollView. can't get my head round the way that other people have done this as they seem to be connecting the scrolling of the UIScrollView with the SKView to make the screen seem like it's scrolling.

I'm sure there is a much easier way of doing this but been searching for a while now.

Any help would be appreciated.

Thanks allot in advance.

like image 856
Dave3of5 Avatar asked Feb 12 '23 19:02

Dave3of5


1 Answers

I would avoid using UIKit components if possible. When you have a UIView that is parented to UIKit components, it makes it difficult to interact with your SpriteKit components that live in the SKView. It's pretty easy to subclass SKNode and make a "ScrollingNode" that uses gesture recognizers on the SKView to do the scrolling. Here's an example:

https://github.com/JenDobson/SpriteKitScrollingNode

Another great tutorial to take a look at is http://www.raywenderlich.com/44270/sprite-kit-tutorial-how-to-drag-and-drop-sprites. Go to the part at the end on gesture recognizers.

like image 164
Jen Avatar answered Feb 16 '23 04:02

Jen