6

I need a way to display a leaderboard - best user scores (local user, local scores).

Do I need to create my "own" node for this kind of thing, or its better to use UITableView above SKView with transparent background?

AndrewShmig
  • 4,843
  • 6
  • 39
  • 68

2 Answers2

10

An approach I use is:

  1. In the scene's didMoveToView: method, set up a UIScrollView instance. Set all the properties you need, and make sure to also set its delegate, set hidden=YES and add its panGestureREcognizer to the view gesture recognizer collection.

  2. In willMoveFromView: tear it down and clean it up.

  3. In the scrollview delegate, implement scrollViewDidScroll:. Here you can use the scroll view's contentOffset point to adjust a node's position.

I have implemented an offsetable node that I use as a root node for things I need to scroll. All it does is implement a contentOffset property - you can see it here: CATOffsetNode Gist.

So in scrollViewDidScroll: all I need to do is:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // I don't need to flip scrollView.contentOffset.Y because my scrollview
    // Y coordinate is flipped. See below.
    self->_gameListNode.contentOffset = scrollView.contentOffset;
}    

Then I add all the items I need to scroll as children for _gameListNode.

The result is a scrollable game list, which offers a native scrollview experience, including bounciness and swipe acceleration etc.

Here is an example implementation you can check out: ScrollKit on GitHub.

Edit: note about scrollview contentOffset Y direction:

UIView and SpriteKit Y coordinates go in opposite directions (UIView's origin is top left, and Y increases downwards), and I prefer to stick to one coordinate system. So I've made a custom scrollview class that flips the Y direction in the initializer. E.g.:

-(id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        CGAffineTransform verticalFlip = CGAffineTransformMakeScale(1,-1);
        self.transform = verticalFlip;
    }
    return self;
}

This is not necessary (and you don't even need to subclass UIScrollView to achieve this) but without this you would have to manually flip contentOffset.Y in the scrollview delegate.

Janis Kirsteins
  • 2,128
  • 16
  • 17
  • Interesting, thank you. But why are you using UIScrollView instead of UITableView? Lets assume that you dont need to scroll horizontally. Hmmm... just thought that using UISrcollView brings more power to scrolling in SpriteKit. – AndrewShmig Mar 09 '14 at 12:32
  • UIScrollView suits my needs atm, so I haven't even considered using anything else. I do use horizontal scrolling as well, but I use nested scrollviews for that. I updated my answer with a comment about UIView Y coordinates, btw. I forgot to mention that initially. – Janis Kirsteins Mar 09 '14 at 13:57
  • And the last question: why do you configure UIScrollView in didMoveToView:? Why not in init methods or somewhere else? Just curious. – AndrewShmig Mar 09 '14 at 16:23
  • Scenes get initialized before adding them to views, so scene.view will be nil when initWithSize: is invoked. didMoveToView: is the earliest guaranteed place where the scene has a reference to its view. – Janis Kirsteins Mar 09 '14 at 16:59
  • so, what is the right way to configure scene before its being presented? ( https://stackoverflow.com/questions/22284211/where-is-the-right-place-to-configure-skscene-content-in-spritekit ) – AndrewShmig Mar 09 '14 at 17:06
0

Game center offers leaderboards. Maybe you can use those? Game Center Programming Guide

Amandir
  • 679
  • 1
  • 7
  • 20