gpt4 book ai didi

swift - 拖放同一 Sprite 的子级

转载 作者:行者123 更新时间:2023-11-30 10:08:37 24 4
gpt4 key购买 nike

我正在使用 Sprite Kit 制作游戏,我希望能够在盒子沿着屏幕移动时拖放它们。

代码要点如下:我在计时器上生成盒子,然后它们在屏幕上向下移动。

override func didMoveToView(view: SKView) {
let timer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: Selector("spawnBox"), userInfo: nil, repeats: true)
}
func spawnBox() {
/* Set up the box */

addChild(box)

let boxMoveDown = SKAction.moveToY(-100, duration: 5.0)
let actionDone = SKAction.removeFromParent()
box.runAction(SKAction.sequence([boxMoveDown, actionDone]))
}

但问题是我如何移动我正在触摸的特定 child 而不影响所有其他“ child ”?据我了解,目前,每次我生成一个盒子时,它都是完全相同的,因此当我设置单个 child 的位置时,我无法具体说明。

这是我的touchesBegan和touchesMoved函数的内容

if let touch = touches.first {
let location = touch.locationInNode(self)
let objects = nodesAtPoint(location) as [SKNode]

if objects.contains(nodeAtPoint(location)) && nodeAtPoint(location).name == "box" {
box.position = location
box.removeAllActions()
}
}

- box.position = location 是什么需要改变。

希望你能理解我的想法。我试图将包含的代码保留在必要的范围内。我对 Sprite Kit 还很陌生,你可能知道这一点。

最佳答案

如果我是你,我会这样处理:

为您的盒子节点创建一个扩展SKSpriteNode的自定义类。

在此自定义类中,覆盖触摸属性。

然后根据位置设置该函数内的位置。

现在您需要担心的是您的 zPosition,任何具有最高 zPosition 的 child 都将是在触摸时被调用的 child 。

您无需再担心 nodesAtPoint 或其他问题,API 将为您处理所有这些问题。

关于swift - 拖放同一 Sprite 的子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34548285/

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