Dave3of5
Dave3of5

Reputation: 730

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.

Upvotes: 4

Views: 3001

Answers (2)

Jen
Jen

Reputation: 156

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.

Upvotes: 7

davbryn
davbryn

Reputation: 7176

You can use UIKit in a SpriteKit application.

If you use a UIScrollView you are going to end up having to get your hands dirty regarding re-use of cells, layout, user-interaction etc. You can actually make a cell look pretty good (and a cell is a UIView so you can sack a bunch of subviews in there) if you just use (and customise) a UITableView

Your hierarchy would be:

Scene: TableView

Upvotes: 1

Related Questions