gpt4 book ai didi

ios - 带有游戏套件的移动平台

转载 作者:可可西里 更新时间:2023-11-01 02:00:52 27 4
gpt4 key购买 nike

创建一个 2d 游戏,并希望添加移动平台,我可以控制平台的模式和旋转。

例子:

Moving Platform
我希望这个平台顺时针运动,当它到达顶部和底部时我希望它相应地旋转,就好像它面向它前进的方向(所以平台基本上会旋转 180 度,因为它顶部和底部呈拱形)。

我不能使用 SKActions,因为我需要物理才能正常工作。

我的想法是,我可以使用具有行为和目标的代理来执行此操作。不确定我是否也需要寻路。

我正在研究如何使用这些功能,但文档和缺乏教程很难破译。我希望有人可以通过提供一个示例来说明这是如何工作的,从而节省我一些反复试验的时间。

提前致谢!

最佳答案

使用 SKActions 或手动调整位置很难按照你想要的方式工作,但尝试一下总是值得的,因为模拟它需要 2 分钟,看看...

我建议做一些类似 Path 类的事情,每帧向平台发送速度命令 ...

实际上,这可能是学习状态机的一个很好的练习。

MOVE RIGHT STATE: if X position > starting position X + 200, enter state "move down"

MOVE DOWN STATE: if Y position < starting position Y - 200, enter state "move left"

MOVE LEFT STATE: if X position < starting position X, enter state "move up"

MOVE UP STATE: if Y position > starting position Y, enter state "move right"

.. 你可以想办法给它更多的曲率,而不仅仅是直角(当改变方向时)

否则,您必须将其转换为类/结构/组件,并为每个平台提供它自己的实例。

///

另一种选择是将物理问题排除在等式之外,并创建一个 playerIsOnPlatform 属性...然后您在每一帧手动调整玩家的位置...(或者可能是 SKConstraint)

这将需要更多关于跳跃等的代码,并很快将事情变成意大利面条(我上次尝试过)

但是,我能够成功地克隆它,使用正确的命中检测沿着这条路线:

https://www.youtube.com/watch?v=cnhlFZeIR7Q

更新:

这是一个工作项目:
https://github.com/fluidityt/exo2/tree/master

样板快速的东西:

import SpriteKit
import GameplayKit


// Workaround to not having any references to the scene off top my head..
// Simply add `gScene = self` in your didMoveToViews... or add a base scene and `super.didMoveToView()`
var gScene = GameScene()

class GameScene: SKScene {

override func didMove(to view: SKView) {
gScene = self
}

var entities = [GKEntity]()
var graphs = [String : GKGraph]()

private var lastUpdateTime : TimeInterval = 0

func gkUpdate(_ currentTime: TimeInterval) -> TimeInterval {
if (self.lastUpdateTime == 0) {
self.lastUpdateTime = currentTime
}

let dt = currentTime - self.lastUpdateTime

for entity in self.entities {
entity.update(deltaTime: dt)
}

return currentTime
}

override func update(_ currentTime: TimeInterval) {
self.lastUpdateTime = gkUpdate(currentTime)
}
}

这是非常基本的组件:

class Platforms_BoxPathComponent: GKComponent {

private enum MovingDirection: String { case up, down, left, right }

private var node: SKSpriteNode!

private var state: MovingDirection = .right

private lazy var startingPos: CGPoint = { self.node.position }()

@GKInspectable var startingDirection: String = "down"
@GKInspectable var uniqueName: String = "platform"
@GKInspectable var xPathSize: CGFloat = 400
@GKInspectable var yPathSize: CGFloat = 400

// Moves in clockwise:
private var isTooFarRight: Bool { return self.node.position.x > (self.startingPos.x + self.xPathSize) }
private var isTooFarDown: Bool { return self.node.position.y < (self.startingPos.y - self.yPathSize) }
private var isTooFarLeft: Bool { return self.node.position.x < self.startingPos.x }
private var isTooFarUp: Bool { return self.node.position.y > self.startingPos.y }

override func didAddToEntity() {
print("adding component")
// can't add node here because nodes aren't part of scene yet :(
// possibly do a thread?
}

override func update(deltaTime seconds: TimeInterval) {
if node == nil {
node = gScene.childNode(withName: uniqueName) as! SKSpriteNode

// for some reason this is glitching out and needed to be redeclared..
// showing 0 despite clearly not being 0, both here and in the SKS editor:
xPathSize = 300
}

let amount: CGFloat = 2 // Amount to move platform (could also be for for velocity)

// Moves in clockwise:
switch state {

case .up:
if isTooFarUp {
state = .right
fallthrough
} else { node.position.y += amount }

case .right:
if isTooFarRight {
state = .down
fallthrough
} else { node.position.x += amount }

case .down:
if isTooFarDown {
state = .left
fallthrough
} else { node.position.y -= amount }

case .left:
if isTooFarLeft {
state = .up
fallthrough
} else { node.position.x -= amount }

default:
print("this is not really a default, just a restarting of the loop :)")
node.position.y += amount
}
}
}

这是您在 sks 编辑器中执行的操作:

enter image description here enter image description here

关于ios - 带有游戏套件的移动平台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46431953/

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