SpriteKit中的ScrollView [英] ScrollView in SpriteKit

查看:42
本文介绍了SpriteKit中的ScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了其他一些与此类似的问题和答案,但是没有一个问题对我来说很完美.

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.

无论如何,我还是XCode和Swift的新手,所以我很难接受自己发现的内容并进行更改.但是,我希望能快速学习.

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.

到目前为止,我了解的是UIScrollView可以覆盖场景,并且可以使用可移动的spritenode滚动视图.然后,视图将以某种方式将坐标转换到SpriteKit场景,以移动看起来像它们在视图中的精灵.我认为就是这样.任何帮助将是巨大的.我很困.< 3

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

推荐答案

您必须使用全局/成员变量在 touchesMoved()中进行自己的逻辑处理.

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

不幸的是,很多gamedev和SK都是数学和逻辑..您必须提出自己的问题和解决方案..没有手册,因为编程和Swift的可能性是无限的:)

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 :)

基本上,您将每个 touch 位置与最后一个位置进行比较,这将成为可用于执行操作的增量值".

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

例如,如果我触摸屏幕中央,则我的触摸位置是0,0(或您的定位点设置为的任何位置).如果我将手指向右移动,那么我现在说25、0 ...这将产生+ 25x的增量值".

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.

有了该增量值,您可以对所有卡执行各种操作,例如 moveBy ...因此,如果deltaX为+25,则需要移动所有卡节点右边(根据您的喜好确定的一定数量).如果deltaX为-25,则将卡向左移动一定量.

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.

实际移动取决于您自己-您可以在 update() touchesMoved()中放置一个函数,该函数不断将卡移动到某个方向以一定的deltaX值速率.

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)

-您可以搜索如何制作Delta函数,但这实际上与Algebra完全相同.
- myMoveFunc 就像遍历所有卡节点然后在它们上同时运行 .moveBy 一样简单.

- 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.

要检测哪个卡在中间,可以在 touchesEnded() update()中进行调用,以检查节点中的节点名称/身份.屏幕中心...类似

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)

您显然想要将centerX和centerY更改为您想要中间卡的位置:)现在,这只是在屏幕的死点.

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.

一旦有了 centerNode ,则只需执行创建的所有功能来选择"它即可.

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)))

希望这对您有所帮助!

这篇关于SpriteKit中的ScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆