gpt4 book ai didi

ios - 如何使一个节点堆叠在另一个节点之上并跟随该对象?

转载 作者:行者123 更新时间:2023-11-30 13:34:00 25 4
gpt4 key购买 nike

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

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)))
)

但是这个没有被使用。相反,怪物被设置为遵循与玩家相同的圆形路径。所以我不确定你到底想实现什么目标。 (顺便说一句,让怪物“堆叠在玩家顶部”的最简单方法是使其成为玩家的子节点......)

希望这些猜测能以某种方式帮助您(如果没有别的办法,可以改进您的问题和代码示例)。

关于ios - 如何使一个节点堆叠在另一个节点之上并跟随该对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36288282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com