0

Ok, so I implemented a scrollview by using this method:

How to create a vertical scrolling menu in spritekit?

Now when I launch the scene it doesn't display anything except a black/ grey background

Here is the code I have used in the menu scene:

import Foundation
import SpriteKit
import UIKit

//let kMargin: CGFloat = 40

//var backButton = SKSpriteNode()
//var selectButton = SKSpriteNode()
var moveableNode = SKNode()
var scrollView: CustomScrollView!
private var imageSize = CGSize.zero

var sprite = SKSpriteNode()

class Menu: SKScene {

func didMoveToView() {

    addChild(moveableNode)

    scrollView = CustomScrollView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height), scene: self, moveableNode: moveableNode)
    scrollView.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height * 2)
    view!.addSubview(scrollView)

    sprite =  SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 50, height: 44))
    sprite.position.y = CGRectGetMidY(self.frame) + self.frame.size.height
    moveableNode.addChild(sprite)

  }

}

It might be really obvious but I can't seem to see it.

Community
  • 1
  • 1
Astrum
  • 375
  • 6
  • 21

2 Answers2

0

Try this for the sprite, I made a mistake in the other question, it should be - self.frame.size.height, not +.

 sprite = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 50, height: 44))
 sprite.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - self.frame.size.height)
 moveableNode.addChild(sprite)

Check my gitHub project

https://github.com/crashoverride777/Swift-2-SpriteKit-UIScrollView-Helper

crashoverride777
  • 10,581
  • 2
  • 32
  • 56
  • That didn't work. If it is any help I'm trying to make the scrollview horizontal instead of vertical and I didn't really get how I was supposed to use content offset so I just skipped it. But I did change the things you said to change such as the delegate part and the flip for spritekit. – Astrum Jan 09 '16 at 21:18
  • That didn't seem to work either. Also the node count when I launch the menu scene says it only has 1 node – Astrum Jan 09 '16 at 22:22
  • this defiantly works, I just tried the code in a sample project. I just put it on github, have a look. https://github.com/crashoverride777/Swift-2-CustomScrollView-SpriteKit – crashoverride777 Jan 09 '16 at 22:24
  • ok I looked at your project and it was very useful in seeing where i went wrong. but now i have another problem with the scrolling side of it. when I scroll to the right it seems to only scroll a little and stop, but when i scroll left it scrolls really far. I want to start the scene at the far left and scroll to the right. (Should I do another question?) – Astrum Jan 10 '16 at 12:24
  • Please mark my answer as correct. For horizontal scrolling its more tricky because you need to position stuff the opposite way. UIKit has different coordinates than Spritekit. You need to read my gitHub project where I explain this. Otherwise if you are still stuck ask another question. – crashoverride777 Jan 10 '16 at 12:26
  • here is my new question: http://stackoverflow.com/questions/34712427/spritekit-swift-2-0-scrollview-in-reverse – Astrum Jan 11 '16 at 00:26
  • I'll check it tomorrow – crashoverride777 Jan 11 '16 at 21:30
  • Thanks, I really appreciate that you're helping me :) – Astrum Jan 11 '16 at 21:37
  • did you have time to look at the new question? – Astrum Jan 12 '16 at 21:42
  • Hey, sorry been travelling yesterday. I'll check later today – crashoverride777 Jan 13 '16 at 12:05
  • So what did you think of the new question? – Astrum Jan 14 '16 at 05:02
  • I will check in the next few hours, sorry been busy. – crashoverride777 Jan 14 '16 at 11:51
  • Could you check this question if you get the time please. I would be very grateful. Thanks http://stackoverflow.com/questions/34889359/making-a-scrollview-lock-on-a-sprite-and-enlarging-it – Astrum Jan 22 '16 at 05:29
0

The sprite position is not right, not sure where exactly you want it. I do the following and the sprite show up on the view.

sprite.position = CGPoint(x: 200, y: 200)

or

// To make it a center of the view
sprite.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
Ken W
  • 999
  • 7
  • 10