如何让一个 SKSpriteNode 跟随另一个 SKSpriteNode
how to make an SKSpriteNode follow another SKSpriteNode
运行 我的代码出现了一些问题。我正在尝试使用以下代码让僵尸跟随我的玩家:
class GameScene: SKScene, SKPhysicsContactDelegate {
func Enemies() {
Enemy = SKSpriteNode(imageNamed: "Spaceship")
Enemy.size = CGSize(width: 50, height: 50)
Enemy.color = UIColor(red: 0.9, green: 0.1, blue: 0.1, alpha: 1.0)
Enemy.colorBlendFactor = 1.0
//physics
Enemy.physicsBody = SKPhysicsBody(rectangleOf: Enemy.size)
Enemy.physicsBody?.isDynamic = true
Enemy.physicsBody?.affectedByGravity = false
Enemy.name = "Enemy"
Enemy.position.y = -frame.size.height/2
let positionX = arc4random_uniform(UInt32(frame.size.width))
Enemy.position.x = CGFloat(positionX)
addChild(Enemy)
}
override func didMove(to view: SKView) {
enemyTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(GameScene.Enemies), userInfo: nil, repeats: true)
override func update(_ currentTime: TimeInterval) {
Enemy.run(SKAction.move(to: ship.position, duration: 3))
}
如果我 运行 这段代码,我可以生成僵尸,但它们不会跟随我的主要玩家,它们只会去它们生成时他所在的位置(即僵尸生成在time = 0 将在 time = 0 时前往船位,在 time = 1 时生成的僵尸将在 time = 1 时前往船位,依此类推)。但是,如果我 运行 这段代码只生成一个僵尸,就像这样:
override func didMove(to view: SKView) {
Enemies()
}
孤独的僵尸会跟着我的玩家。知道为什么代码适用于一个僵尸,但不适用于多个僵尸吗?
也许您应该 removeAllActions()
在 Enemy
上阅读一个动作。看起来你有一个动作需要3秒,但是你每帧添加一个动作,所以它一次最多有180个节点的动作。
我不建议在您的更新周期中不断添加操作,您将因幕后发生的所有事情而遭受性能损失。相反,使用一个 SKAction.customAction
你只会添加到你的精灵一次。
这是一个自定义操作的示例,它将执行您想要的操作,请记住只分配一次。 (代码未经测试,因此可能需要编辑)
let customActionBlock =
{
(node,elapsedTime) in
let dx = ship.x - node.position.x
let dy = ship.y - node.position.y
let angle = atan2(dx,dy)
node.position.x += sin(angle) * speedPerFrame
node.position.y += cos(angle) * speedPerFrame
}
let duration = TimeInterval(Int.max) //want the action to run infinitely
let followPlayer = SKAction.customAction(withDuration:duration,actionBlock:customActionBlock)
Enemy.run(action:followPlayer,withKey:"follow")
运行 我的代码出现了一些问题。我正在尝试使用以下代码让僵尸跟随我的玩家:
class GameScene: SKScene, SKPhysicsContactDelegate {
func Enemies() {
Enemy = SKSpriteNode(imageNamed: "Spaceship")
Enemy.size = CGSize(width: 50, height: 50)
Enemy.color = UIColor(red: 0.9, green: 0.1, blue: 0.1, alpha: 1.0)
Enemy.colorBlendFactor = 1.0
//physics
Enemy.physicsBody = SKPhysicsBody(rectangleOf: Enemy.size)
Enemy.physicsBody?.isDynamic = true
Enemy.physicsBody?.affectedByGravity = false
Enemy.name = "Enemy"
Enemy.position.y = -frame.size.height/2
let positionX = arc4random_uniform(UInt32(frame.size.width))
Enemy.position.x = CGFloat(positionX)
addChild(Enemy)
}
override func didMove(to view: SKView) {
enemyTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(GameScene.Enemies), userInfo: nil, repeats: true)
override func update(_ currentTime: TimeInterval) {
Enemy.run(SKAction.move(to: ship.position, duration: 3))
}
如果我 运行 这段代码,我可以生成僵尸,但它们不会跟随我的主要玩家,它们只会去它们生成时他所在的位置(即僵尸生成在time = 0 将在 time = 0 时前往船位,在 time = 1 时生成的僵尸将在 time = 1 时前往船位,依此类推)。但是,如果我 运行 这段代码只生成一个僵尸,就像这样:
override func didMove(to view: SKView) {
Enemies()
}
孤独的僵尸会跟着我的玩家。知道为什么代码适用于一个僵尸,但不适用于多个僵尸吗?
也许您应该 removeAllActions()
在 Enemy
上阅读一个动作。看起来你有一个动作需要3秒,但是你每帧添加一个动作,所以它一次最多有180个节点的动作。
我不建议在您的更新周期中不断添加操作,您将因幕后发生的所有事情而遭受性能损失。相反,使用一个 SKAction.customAction
你只会添加到你的精灵一次。
这是一个自定义操作的示例,它将执行您想要的操作,请记住只分配一次。 (代码未经测试,因此可能需要编辑)
let customActionBlock =
{
(node,elapsedTime) in
let dx = ship.x - node.position.x
let dy = ship.y - node.position.y
let angle = atan2(dx,dy)
node.position.x += sin(angle) * speedPerFrame
node.position.y += cos(angle) * speedPerFrame
}
let duration = TimeInterval(Int.max) //want the action to run infinitely
let followPlayer = SKAction.customAction(withDuration:duration,actionBlock:customActionBlock)
Enemy.run(action:followPlayer,withKey:"follow")