如何在另一个节点之上创建一个节点堆栈并跟随该对象?

时间:2016-03-29 15:13:19

标签: ios iphone swift sprite-kit

我正在尝试创建一个游戏,其中我有一个对象1旋转在一个圆圈中,另一个对象出现并将自己置于对象1的顶部。目前,对象只是围绕对象1旋转而没有堆叠在它上面。如何让对象堆叠在顶部然后跟随它的轨道?现在这是我的代码。

let player = SKSpriteNode(imageNamed: "Light")

override func didMoveToView(view: SKView) {
    // 2
    backgroundColor = SKColor.whiteColor()
    // 3
    player.position = CGPoint(x: size.width * 0.5, y: size.height * 0.5)
    // 4
    player.size = CGSize(width:70, height:60 )
    addChild(player)

    let dx = player.position.x - self.frame.width / 2
    let dy = player.position.y - self.frame.height / 2

    let rad = atan2(dy, dx)

    circle = UIBezierPath(arcCenter: CGPoint(x: size.width / 2, y: size.height / 2), radius: 120, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)
    let follow = SKAction.followPath(circle.CGPath, asOffset: false, orientToPath: true, speed: 100)
    player.runAction(SKAction.repeatActionForever(follow))
}    
func addMonster() {
    let monster = SKSpriteNode(imageNamed: "plate")

    // Determine where to spawn the monster along the Y axis
    let actualY = random(min: monster.size.height/1, max: size.height - monster.size.height/1)

    // Position the monster slightly off-screen along the right edge,
    // and along a random position along the Y axis as calculated above
    monster.position = CGPoint(x: size.width * 0.5 + monster.size.width/2, y: actualY)

    // Add the monster to the scene
    addChild(monster)

    // Determine speed of the monster
    let actualDuration = random(min: CGFloat(2.0), max: CGFloat(3.0))

    // Create the actions
    let actionMove = SKAction.moveTo(CGPoint(x: -monster.size.width/2, y: actualY), duration: NSTimeInterval(actualDuration))

    let follow = SKAction.followPath(circle.CGPath, asOffset: false, orientToPath:true, speed: 100)
    monster.runAction(SKAction.repeatActionForever(follow))
}

1 个答案:

答案 0 :(得分:0)

您的口头描述似乎有以下内容(您可以将其复制并粘贴到iOS游乐场):

import UIKit
import SpriteKit
import XCPlayground

let scene = SKScene(size: CGSize(width: 400, height: 400))
let view = SKView(frame: CGRect(origin: .zero, size: scene.size))
view.presentScene(scene)
XCPlaygroundPage.currentPage.liveView = view

scene.backgroundColor = .darkGrayColor()
scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)

let player = SKShapeNode(rectOfSize: CGSize(width: 70, height: 60), cornerRadius: 5)

let radius: CGFloat = 120
let circle = CGPathCreateWithEllipseInRect(CGRect(x: -radius, y: -radius, width: radius * 2, height: radius * 2), nil)

let followCircle = SKAction.followPath(circle, asOffset: false, orientToPath: true, speed: 100)
player.runAction(.repeatActionForever(followCircle))

let monsterSize = CGSize(width: 35, height: 30)
let monster = SKShapeNode(rectOfSize: monsterSize, cornerRadius: 4)
monster.fillColor = .redColor()

monster.runAction(.repeatActionForever(followCircle))

scene.addChild(player)
scene.addChild(monster)

您使用的random(min:max:)函数可能是按照以下方式实现的:

public func random(min min: CGFloat, max: CGFloat) -> CGFloat {
    return min + (CGFloat(arc4random()) / CGFloat(UInt32.max)) * (max - min)
}

但你也有代码似乎在说:

let y = random(
    min: scene.size.height / -2 + monsterSize.height / 2,
    max: scene.size.height / 2 - monsterSize.height / 2
)
let xFrom = scene.size.width / 2
let xTo = scene.size.width / -2
monster.position = CGPoint(x: xFrom, y: y)
monster.runAction(
    .moveToX(xTo, duration: NSTimeInterval(random(min: 2, max: 3)))
)

但这是未使用的。相反,怪物被设置为遵循与玩家相同的圆圈路径。因此,我不确定您到底想要实现的目标。 (顺便说一下,让玩家“叠加在玩家顶部”的最简单方法是将其作为玩家的子节点......)

希望这些猜测能够以某种方式帮助你(如果没有别的话,可以优化你的问题和代码示例)。