gpt4 book ai didi

swift SpriteKit : How to generate objects and make the game view move upwards as my player moves up the screen?

转载 作者:行者123 更新时间:2023-11-30 14:05:27 24 4
gpt4 key购买 nike

我正在创建一款类似于 Doodle Jump 的游戏,我的玩家在得分的同时从物体上弹起并向上移动。

如何使 Y 轴无限向上移动并生成相同的对象,但随着玩家向上移动而处于随机位置?

我没有任何相关代码,因为我确实需要一些关于如何执行此操作的参数,而且我对此有点陌生。

最佳答案

您创建了 SKSpriteNode 的子类,我们将其命名为 Bar

class Bar: SKSpriteNode {
init(position p: CGPoint){
super.init(texture: nil, color: SKColor.greenColor(), size: CGSizeMake(50, 30))
position = p
name = "bar"
}
}

首先,您可以使用 arc4random_uniform() 生成放置条形元素的 x 和 y 坐标。 (在场景的初始化程序中)

class Scene: SKScene {

init(size: CGSize){
super.init(size: size)
placeBarAtRandomPositions();
}

func randomPoint(scene: CGRect) -> CGPoint {
let x = CGFloat(arc4random_uniform(UInt32(scene.width)))
let y = CGFloat(arc4random_uniform(UInt32(scene.height)))
return CGPoint(x: x, y: y)
}


}

您可以通过向下移动条来无限地设置 Y 轴。例如,当玩家向上移动时,您会向下移动栏。如果底部的 Bar 移出场景,您会在顶部生成一个新的随机位置,在其中插入新 Bar。示例:

override func didSimulatePhysics() {
enumerateChildNodesWithName("bar") { (node, _) -> () in
if(node.position.y < -node.size.height/2){ //Because a node’s origin is the center
node.remove()
self.spawnBarNodeAtTop()
}
}
}

关于 swift SpriteKit : How to generate objects and make the game view move upwards as my player moves up the screen?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32484585/

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