gpt4 book ai didi

swift - 检测两个独立 Sprite 节点上的触摸

转载 作者:搜寻专家 更新时间:2023-11-01 07:05:03 26 4
gpt4 key购买 nike

所以我在这里尝试做的是能够移动四个 Sprite 节点中的两个,彼此独立。我使用枚举来检测触摸了哪张幻灯片,然后使用该信息移动正确的节点,我知道我可以使用节点的名称,但似乎更适合我正在尝试做的事情。到目前为止一切正常,除了我一次只能移动一个节点。我希望能够移动...比如说,左滑向上,同时右滑向下。

感谢您的任何建议或意见,欢迎任何反馈..

var selectedSlides: [SKSpriteNode] = []
var theSlideTouched: SlideTouched = .nothing

enum SlideTouched
{
case left
case right
case bottom
case top
case nothing
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
for touch in touches
{

if leftSlide.contains((touch.location(in: self)))
{
selectedSlides.append(leftSlide)
theSlideTouched = .left

}

if rightSlide.contains((touch.location(in: self)))
{
selectedSlides.append(rightSlide)
theSlideTouched = .right

}

if bottomSlide.contains((touch.location(in: self)))
{
selectedSlides.append(bottomSlide)
theSlideTouched = .bottom
}

if topSlide.contains((touch.location(in: self)))
{
selectedSlides.append(topSlide)
theSlideTouched = .top
}
}
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)
{
for touch in touches
{
let location = touch.location(in: self)

switch theSlideTouched
{
case .bottom:
if selectedSlides.contains(bottomSlide)
{
bottomSlide.color = UIColor.white
bottomSlide.position.x = location.x
}
case .top:
if selectedSlides.contains(topSlide)
{
topSlide.color = UIColor.white
topSlide.position.x = location.x
}
case .left:
if selectedSlides.contains(leftSlide)
{
leftSlide.color = UIColor.white
leftSlide.position.y = location.y
}
case .right:
if selectedSlides.contains(rightSlide)
{
rightSlide.color = UIColor.white
rightSlide.position.y = location.y
}
case .nothing: break
}
}

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
{
switch theSlideTouched
{
case .bottom: bottomSlide.color = UIColor.clear
case .top: topSlide.color = UIColor.clear
case .left: leftSlide.color = UIColor.clear
case .right: rightSlide.color = UIColor.clear
case .nothing: break
}
theSlideTouched = .nothing
selectedSlides.removeAll()
}

最佳答案

我会将 slider 对象移动到它们自己的类中。这样您就可以独立处理它们的移动,而不会用这些对象弄乱您的 GameScene。现在,当我创建这个示例时,我将对象可视化地放置在整个场景编辑器的 GameScene.sks 文件中,因此如果您在代码中创建这些对象,我的初始化可能与您的不同。

在 GameScene 中...

var topSlide: DragObject!
var rightSlide: DragObject!
var bottomSlide: DragObject!
var leftSlide: DragObject!

override func didMove(to view: SKView) {

self.view!.isMultipleTouchEnabled = true

if let topSlide = self.childNode(withName: "topSlide") as? DragObject {
self.topSlide = topSlide
topSlide.type = .top
}

//if you were to create this in code you could do
//topSlide = DragObject(color: .red, size: CGSize(width: 100, height: 100)
//topSlide.type = .top
//topSlide.position = CGPoint(x: 500, y: 500)
//topSlide.zPosition = 1
//addChild(topSlide)

if let rightSlide = self.childNode(withName: "rightSlide") as? DragObject {
self.rightSlide = rightSlide
rightSlide.type = .right
}

if let bottomSlide = self.childNode(withName: "bottomSlide") as? DragObject {
self.bottomSlide = bottomSlide
bottomSlide.type = .bottom
}

if let leftSlide = self.childNode(withName: "leftSlide") as? DragObject {
self.leftSlide = leftSlide
leftSlide.type = .left
}
}

在 DragObject 类中...

class DragObject: SKSpriteNode {

enum SlideType {
case left
case right
case bottom
case top
case nothing
}

var type: SlideType = .nothing

init(color: SKColor, size: CGSize) {

super.init(texture: nil, color: color, size: size)

setup()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

setup()
}

func setup() {
self.isUserInteractionEnabled = true
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

for touch in touches {
let location = touch.location(in: self.parent!)

self.color = .white
if type == .bottom || type == .top {
position.x = location.x
}
else if type == .left || type == .right {
position.y = location.y
}
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.color = .red
}
}

关于swift - 检测两个独立 Sprite 节点上的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48569693/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com