我目前正在制作一款游戏,我想在它们从屏幕顶部向下滑动时向左和向右滑动精灵。但是,我只能与最近生成的精灵进行交互。我无法触及的所有其他精灵。我不确定问题是什么,但我会感激任何帮助。谢谢!如果有人有兴趣了解我正在采取的措施,我会把下面的代码放在下面。
import SpriteKit
class GameScene: SKScene {
var sprite: SKSpriteNode!
var touchPoint: CGPoint = CGPoint()
var touching: Bool = false
override func didMoveToView(view: SKView) {
physicsWorld.gravity = CGVectorMake(0, -0.999)
sprite = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 60, height: 60))
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
sprite.position = CGPoint(x: frame.size.width/2.0, y: frame.size.height)
runAction(SKAction.repeatActionForever(
SKAction.sequence([SKAction.runBlock(addMonster), SKAction.waitForDuration(1.0)])))
}
func addMonster() {
sprite = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 60, height: 60))
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
sprite.position = CGPoint(x: frame.size.width/2.0, y: frame.size.height)
addChild(sprite)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first! as UITouch
let location = touch.locationInNode(self)
if sprite.frame.contains(location) {
touchPoint = location
touching = true
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {let touch = touches.first! as UITouch
let location = touch.locationInNode(self)
touchPoint = location}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
touching = false
}
override func update(currentTime: CFTimeInterval) {
if touching {
let dt:CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-sprite.position.x, dy: touchPoint.y-sprite.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
sprite.physicsBody!.velocity=velocity
}
}
}