【问题标题】:how to make a node stack on top of another node and follow the object?如何在另一个节点之上制作节点堆栈并跟随对象?
【发布时间】:2016-03-29 15:13:19
【问题描述】:

我正在尝试创建一个游戏,其中我有一个对象 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))
}

【问题讨论】:

标签: ios iphone swift sprite-kit


【解决方案1】:

您的口头描述似乎包含以下内容(您可以将其复制并粘贴到 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)))
)

但这是未使用的。相反,怪物被设置为与玩家遵循相同的循环路径。所以我不确定你到底想要达到什么目的。 (顺便说一句,让怪物“堆叠”到玩家的最简单的方法是让它成为玩家的子节点......)

希望这些猜测能以某种方式帮助您(如果没有其他问题,请完善您的问题和代码示例)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 2018-02-21
    • 2016-05-29
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 2015-10-22
    相关资源
    最近更新 更多