我创建了一个函数,它生成一个名为Ball的SKSpriteNode,我创建了另一个函数,用2种不同的颜色设置动画。在它们产生之后,我需要将一个Ball移动到我的touch.location,但是当我创建它时,它总是需要最后生成的Node来移动该位置。所以我只需要移动当前在场景中的几个球中的一个而不是最后添加的球。
有人可以帮助我。提前致谢
// Spawn forever
let action = SKAction.sequence([SKAction.run(spawnRandomBall), SKAction.wait(forDuration: 1.2)])
run(SKAction.repeatForever(action))
}
//-------------------------------------------------------------------------------------------------//
func randomLocation (){
let randomNumber = Int(arc4random() % 2) // 0 and 1
if randomNumber == 0 {
roodball.physicsBody?.applyImpulse(CGVector(dx: 20, dy: 20))
blauwball.physicsBody?.applyImpulse(CGVector(dx: 20, dy: 20))
} else {
roodball.physicsBody?.applyImpulse(CGVector(dx: -20, dy: -20))
blauwball.physicsBody?.applyImpulse(CGVector(dx: -20, dy: -20))
}
}
//.....................................................................................................//
func spawnRandomBall (){
let randomNumber = Int(arc4random() % 2) // 0 and 1
if randomNumber == 0 {
spawnRoodBall()
} else {
spawnBlauwBall()
}
roodball.physicsBody = SKPhysicsBody(circleOfRadius: roodball.size.height / 2)
roodball.physicsBody?.allowsRotation = false
roodball.physicsBody?.affectedByGravity = false
roodball.physicsBody?.friction = 0
roodball.physicsBody?.restitution = 1
roodball.physicsBody?.linearDamping = 0
roodball.physicsBody?.angularDamping = 0
roodball.physicsBody?.velocity = CGVector(dx: 1, dy: 1)
blauwball.physicsBody = SKPhysicsBody(circleOfRadius: blauwball.size.height / 2)
blauwball.physicsBody?.allowsRotation = false
blauwball.physicsBody?.affectedByGravity = false
blauwball.physicsBody?.friction = 0
blauwball.physicsBody?.restitution = 1
blauwball.physicsBody?.linearDamping = 0
blauwball.physicsBody?.angularDamping = 0
blauwball.physicsBody?.velocity = CGVector(dx: 1, dy: 1)
randomLocation()
}
//.....................................................................................................//
func spawnRoodBall () {
roodball = SKSpriteNode(imageNamed: "BalRood")
roodball.position = CGPoint(x: 360, y: self.frame.midY - 38)
roodball.anchorPoint = CGPoint(x: 0.5, y: 0.5)
roodball.size = CGSize(width: 50, height: 50)
roodball.run(SKAction.animate(with: roodTextureArray, timePerFrame: 1.3))
roodball.zPosition = 40
let wait = SKAction.wait(forDuration: 6.5)
let remove = SKAction.removeFromParent()
roodball.run(SKAction.sequence([wait, remove]), withKey: "waitRemove")
self.addChild(roodball)
}
//...................................................................................................//
func spawnBlauwBall () {
blauwball = SKSpriteNode(imageNamed: "BalBlauw")
blauwball.position = CGPoint(x: 360, y: self.frame.midY - 38)
blauwball.anchorPoint = CGPoint(x: 0.5, y: 0.5)
blauwball.size = CGSize(width: 50, height: 50)
blauwball.run(SKAction.animate(with: blauwTextureArray, timePerFrame: 1.3))
blauwball.zPosition = 40
let wait = SKAction.wait(forDuration: 6.5)
let remove = SKAction.removeFromParent()
blauwball.run(SKAction.sequence([wait, remove]), withKey: "waitRemove")
self.addChild(blauwball)
}