5

I have found a few other questions and answers similar to this, but none of them quite work perfect for me.

My vision is to have a horizontal scrollable view at the bottom of the screen where I can scroll through what will look like cards in a hand. Ideally, I could eventually make the card in the middle scaled up a bit and give it a highlighted look to show the user which card is selected. Even better would be if I could figure out how to keep the scroll view resizing to fir the number of sprites (cards) in the view.

Anyways, I am still very new to XCode and Swift, so it is hard for me to take what I find and change it. But, I am hoping to learn fast.

What I understand so far is that a UIScrollView could overlay the scene and with a moveable spritenode I could scroll through the view. The view would then translate the coordinates somehow to the SpriteKit Scene to move the sprites that will look like they are in the view. I think that's how it works. Any help would be great. I am pretty stuck. <3

Bezz
  • 108
  • 9
  • Possible duplicate of [How to create a vertical scrolling menu in spritekit?](http://stackoverflow.com/questions/34229839/how-to-create-a-vertical-scrolling-menu-in-spritekit) – Nik Nov 09 '16 at 00:51
  • If you're new to Swift and Xcode. **Do not** start with this. It's much more difficult than you think. Get at least some basic knowledge and experience before attempting this – Nik Nov 09 '16 at 00:52
  • I am hoping to make it a horizontal view (I'll edit my post), like the view is your hand and you can swipe left or right to view your cards. Is there an easier way to simulate this effect then? – Bezz Nov 09 '16 at 01:02
  • This is just what came to mind when I saw your question. You can zoom in and scroll, which is what you want, right? – Nik Nov 09 '16 at 01:27
  • Yeah. Thank you for responding. I am looking for a way to scroll through sprites(cards) horizontally and have the one in the middle be "selected". It would look neat if i could enlarge the middle one a bit more to show that it is selected, but the user wouldn't zoom manually. Maybe I could make a separate scene in the bottom part of the screen and scroll through that? – Bezz Nov 09 '16 at 03:14
  • 1
    Have a look here, see what you can glean from his answer and github: http://stackoverflow.com/questions/34229839/how-to-create-a-vertical-scrolling-menu-in-spritekit – Confused Nov 09 '16 at 07:16

1 Answers1

2

You have to make your own logic that takes place in touchesMoved() using a global/member variable.

Unfortunately, a lot of gamedev and SK is math and logic.. You have to come up with your own problems and solutions.. There is no manual because the possibilities in programming and Swift are endless :)


Moving the cards:

Basically, you compare each touch location to the last one, and this becomes a "delta value" that you can use to perform actions.

Example, if I touch in the center of the screen, my touch location is 0,0 (or whatever your anchorpoint is set to). If I move my finger right, then I'm now at say 25, 0... This creates a "delta value" of +25x.

With that delta value, you can perform various actions such as moveBy for all the cards... so if I have a deltaX of +25, then I need to move all of the card nodes to the right (by a certain amount that you will determine according to your preferences). If I have a deltaX of -25, I move the cards to the left by a certain amount.

Where you do the actual moving is up to you--you could put a function in update() or touchesMoved() that constantly moves the cards a certain direction at a certain rate of that deltaX value..

Ok that was a mouthful... Maybe this will help:

for touch in touches {
   myGlobalDeltaX = myDeltaXFunc(currentTouch: touch)
   myMoveFunc(cards: allTheCards, byDeltaX: myGlobalDeltaX)

- You can search on how to make a Delta function, but it really is just the same thing from Algebra.
- myMoveFunc can be something as simple as iterating through all of your card nodes then running .moveBy on them at the same time.


Middle detection:

To detect which card is in the center, you would put in touchesEnded() or update() a call to check the name / identity of the node in the center of the screen... so something like

// `self` here refers to your GameScene class' instance, which is just an `SKScene` object
let centerX = self.frame.midX
let centerY = self.frame.midY
let center = CGPoint(x: centerX, y: centerY)

let centerNode = self.nodes(at: center)

You would obviously want to change centerX and centerY to wherever it is you want the middle card to be :) Right now, this is just in the dead-center of the screen.

Once you have a centerNode, you would then just need to do whatever function you have created to "select" it.

let selectedCard = centerNode
mySelectionFunc(middleCard: selectedCard)

This may look like a lot, but I drew out the steps to make understanding it a bit easier.. You can do all of this in one line if desired.

mySelectionFunc(middleCard: self.nodes(at: CGPoint(x: self.frame.x, y: self.frame.y)))

Hope this helps some!

Fluidity
  • 3,985
  • 1
  • 13
  • 34