gpt4 book ai didi

ios - Swift TouchBegan 导致 Xcode 崩溃,同时仍在 TouchEnded 中执行功能

转载 作者:行者123 更新时间:2023-12-03 02:37:11 25 4
gpt4 key购买 nike

我是编程新手,所以我确信这是一个简单的修复。然而,我拥有它,当我触摸特定的 Sprite 节点时,它会添加一个球和目标。一旦我松开手指,球就会飞向目标。但是,如果我尝试在第一个球完成之前发送另一个球,Xcode 就会崩溃。

请帮忙!!

错误=原因:'试图添加一个已经有父节点的SKNode:

代码:

//
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

let touch = touches.anyObject() as UITouch
let touchLocation = touch.locationInNode(self)

if self.nodeAtPoint(touchLocation) == self.slingshot {

addBall()
addTarget()

isFingerTouching = true

}
}




override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
/* Update to new touch location */

if isFingerTouching {

var touch = touches.anyObject() as UITouch!
var touchLocation = touch.locationInNode(self)
var previousLocation = touch.previousLocationInNode(self)


var slingholderY = slingholder.position.y + (touchLocation.y - previousLocation.y)
var slingholderX = slingholder.position.x + (touchLocation.x - previousLocation.x)

slingholderY = min(slingholderY, 155)
slingholderY = max(slingholderY, 89)

slingholderX = min(slingholderX, CGRectGetMidX(self.frame) + 40)
slingholderX = max(slingholderX, CGRectGetMidX(self.frame) - 40)

slingholder.position = CGPointMake(slingholderX, slingholderY)

leftsling.yScale = (155 / slingholder.position.y)*0.6
leftsling.xScale = (slingholder.position.x / 383)*0.6

rightsling.yScale = (155 / slingholder.position.y)*0.6
rightsling.xScale = (383 / slingholder.position.x)*0.6

slingholder.yScale = (89 / slingholder.position.y)
slingholder.xScale = (89 / slingholder.position.y)

ball.yScale = 2*(89 / slingholder.position.y)
ball.xScale = 2*(89 / slingholder.position.y)
ball.position = slingholder.position

slingholder.zRotation = 0.3*((slingholder.position.x - 383)/40)

target.position.x = 768 - (9.57 * (slingholder.position.x - 343))
target.position.y = max(10, 300)
target.position.y = (self.frame.size.height - self.scoreboard.size.height - 300) + (9.85 * (89 - slingholder.position.y) + 150)

}
}


override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {

sendBall()
isFingerTouching = false

}

//Check if bird reaches right or left side
func checkIfBirdReachesOtherSide () {
for child in self.children {
if(child.position.x > self.frame.width+20) {
self.removeChildrenInArray([child])
}
if(child.position.x < -20) {
self.removeChildrenInArray([child])
}
}

}

func addBall () {

ball.position = CGPointMake(CGRectGetMidX(self.frame), 155)
ball.xScale = 1.0
ball.yScale = 1.0
ball.zPosition = 4
self.addChild(ball)

}

func addTarget () {


target.position = CGPoint(x: CGRectGetMidX(self.frame), y: slingholder.position.y + 200)
target.zPosition = 4
self.addChild(target)
}

func sendBall () {

let moveToTarget = SKAction.moveTo(CGPointMake(target.position.x, target.position.y), duration: 0.5)
let scaleBallUp = SKAction.scaleBy(1.0, duration: 0.2)
let scaleBallDown = SKAction.scaleBy(0.3, duration: 0.3)

let delay = SKAction.waitForDuration(2)

let remove = SKAction.removeFromParent()

let sequence = SKAction.sequence([scaleBallUp, scaleBallDown, remove])

let ballGroupAction = SKAction.group([moveToTarget, sequence])
ball.runAction(ballGroupAction)

target.runAction(remove)

let moveSlingholder = SKAction.moveTo(CGPointMake(CGRectGetMidX(self.frame), 155), duration:1, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 10.0)

let scaleSling = SKAction.scaleXTo(0.6, y: 0.6, duration: 1, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 10.0)

let scaleSlingHolder = SKAction.scaleXTo(0.6, y: 0.6, duration: 1, delay: 0.2, usingSpringWithDamping: 0.1, initialSpringVelocity: 10.0)

let rotateSlingholder = SKAction.rotateToAngle(0, duration: 1, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 10.0)

let slingHolderBundledMovement = SKAction.group([moveSlingholder, rotateSlingholder, scaleSlingHolder])


slingholder.runAction(slingHolderBundledMovement)
leftsling.runAction(scaleSling)
rightsling.runAction(scaleSling)

}

}
//

最佳答案

您的问题出在 addTargetaddBall 方法中。

您只能将 SKNode 添加到一个父节点。每次触摸屏幕时,您都会添加相同的目标和相同的球。所以你应该只添加一次球和目标并每次更新位置。

因此您可以在 didMoveToView 方法中添加两个节点:

addChild(ball)
addChild(target)

并且仅更新方法中的位置等,而无需在每次调用方法时再次添加。

关于ios - Swift TouchBegan 导致 Xcode 崩溃,同时仍在 TouchEnded 中执行功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28518431/

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