gpt4 book ai didi

ios - nodeAtPoint 错误节点

转载 作者:行者123 更新时间:2023-11-30 14:15:16 24 4
gpt4 key购买 nike

我正在使用 swift 和 spritekit 制作一个游戏,并且我有一个功能,可以让多个 Sprite 从屏幕顶部掉落。我还做到了这一点,以便我能够使用 nodeAtPoint 检测 Sprite 上的触摸,并使得轻弹 Sprite 成为可能。我的问题是,由于 nodeAtPoint 拖动树中最深的节点,当我单击并拖动 Sprite 时,场景中最新的 Sprite 会被拉向我的触摸位置,而不是我最初触摸的位置。如果有人对如何仅影响我接触的节点有一些建议,我将非常感激。

    class GameScene: SKScene {

var ball = SKSpriteNode(imagedNamed: "blueBlue")

var touchedNode: SKNode? = SKNode()

override func didMoveToView(view: SKView) {

var create = SKAction.runBlock({() in self.createTargets()})
var wait = SKAction.waitForDuration(2)
var waitAndCreateForever = SKAction.repeatActionForever(SKAction.sequence([create, wait]))
self.runAction(waitAndCreateForever)
}


func createTargets() {
ball = SKSpriteNode(imageNamed: "blueBlue")
let randomx = Int(arc4random_uniform(170) + 290)
ball.zPosition = 0
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width / 11)

ball.physicsBody?.dynamic = true
ball.size = CGSize(width: ball.size.width / 1.5, height: ball.size.height / 1.5)
let random = Int(arc4random_uniform(35))

let textures = [texture1, texture2, texture3, texture4, texture5, texture6, texture7, texture8, texture9, texture10, texture11, texture12, texture13, texture14, texture15, texture16, texture17, texture18, texture19, texture20, texture21, texture22, texture23, texture24, texture25, texture1, texture7, texture18, texture24, texture25, texture1, texture7, texture18, texture24, texture25]

ball.texture = textures[random]
ball.position = CGPoint(x: randomx, y: 1400)
addChild(ball)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
touchedNode = self.nodeAtPoint(touchLocation)
if touchedNode.frame.contains(touchLocation) {
touching = true
touchPoint = touchLocation
}
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
touching = true
touchPoint = touchLocation

}
override func update(currentTime: NSTimeInterval) {

if touching {

if touchPoint != touchedNode.position {

let dt: CGFloat = 0.1

let distance = CGVector(dx: touchPoint.x - touchedNode.position.x, dy: touchPoint.y - touchedNode.position.y)

let vel = CGVector(dx: distance.dx / dt, dy: distance.dy / dt)

if touchedNode.parent != nil {

touchedNode.physicsBody?.velocity = vel

}
}
}
}
}

最佳答案

我建议您对代码进行以下更改:

  1. 添加检查以查看 Sprite 是否已被移动(如果是,则不执行任何操作)
  2. 关闭正在移动的 Sprite 的 affectedByGravity 属性
  3. 触摸结束时重置 touchedNodetouching 变量

这是经过上述更改的实现...

touchesBegantouchesMoved 替换为以下内容

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
if (!touching) {
let node = self.nodeAtPoint(touchLocation)
if node is SKSpriteNode {
touchedNode = node
touching = true
touchPoint = touchLocation
touchedNode!.physicsBody?.affectedByGravity = false
}
}
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if (touching) {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
touchPoint = touchLocation
}
}

并添加这个方法

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
if (touchedNode != nil) {
touchedNode!.physicsBody!.affectedByGravity = true
}
touchedNode = nil
touching = false
}

关于ios - nodeAtPoint 错误节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31262315/

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