gpt4 book ai didi

ios - 我想知道为什么我的节点不在我的 GameScene 中

转载 作者:行者123 更新时间:2023-11-28 21:10:49 25 4
gpt4 key购买 nike

下面是我的 didMove(toView:) 和我用来生成 Sprite 节点的函数。忽略代码的彩色 block 部分,但我试图让球从原点产生并上升,我没有收到代码错误,但球没有显示在 View 中。如果您能抽出时间查看我的问题,我将非常感谢。如果您需要更清楚地解释我的问题,请提出问题。

另外请考虑到球最初不在场景中,我希望在调用函数时将球添加到场景中。

class GameScene: SKScene {


var greenBlock = SKSpriteNode ()
var redBlock = SKSpriteNode ()
var blueBlock = SKSpriteNode ()
var yellowBlock = SKSpriteNode ()

var greenBall = SKSpriteNode()
var redBall = SKSpriteNode ()

var ballSpeed: TimeInterval = 4

override func didMove(to view: SKView) {

greenBlock = self.childNode(withName: "greenBlock") as! SKSpriteNode
redBlock = self.childNode(withName: "redBlock") as! SKSpriteNode
blueBlock = self.childNode(withName: "blueBlock") as! SKSpriteNode
yellowBlock = self.childNode(withName: "yellowBlock") as! SKSpriteNode

redBall.name = "redBall"
greenBall.name = "greenBall"

spawnRedAndGreen()




}

func spawnRedAndGreen () {

redBall.position = CGPoint(x: 0, y: -1)
redBall.zPosition = 4
addChild(redBall)


greenBall.position = CGPoint(x: 0, y: 0)
greenBall.zPosition = 4
addChild(greenBall)




let upwardsMotion = SKAction.moveTo(y: 600, duration: ballSpeed)
let redDelay = SKAction.wait(forDuration: TimeInterval(randomBetweenNumbers(firstNum: 1, secondNum: 4)))
let greenDelay = SKAction.wait(forDuration: TimeInterval(randomBetweenNumbers(firstNum: 1, secondNum: 3)))

let redSequence = SKAction.sequence([upwardsMotion, redDelay])
let greenSequence = SKAction.sequence([upwardsMotion, greenDelay])



redBall.run(redSequence)
greenBall.run(greenSequence)

}

这是我用来生成随机数的函数...

func randomBetweenNumbers(firstNum: CGFloat, secondNum: CGFloat) -> CGFloat {

return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(firstNum - secondNum) + min(firstNum, secondNum)

}

更新的代码(注意仍然不起作用)。

class GameScene: SKScene {


var greenBlock = SKSpriteNode ()
var redBlock = SKSpriteNode ()
var blueBlock = SKSpriteNode ()
var yellowBlock = SKSpriteNode ()

var greenBall = SKSpriteNode()
var redBall = SKSpriteNode ()

var ballSpeed: TimeInterval = 4

override func didMove(to view: SKView) {

greenBlock = self.childNode(withName: "greenBlock") as! SKSpriteNode
redBlock = self.childNode(withName: "redBlock") as! SKSpriteNode
blueBlock = self.childNode(withName: "blueBlock") as! SKSpriteNode
yellowBlock = self.childNode(withName: "yellowBlock") as! SKSpriteNode


greenBall.size = CGSize(width: 30, height: 30)
greenBall.position = CGPoint(x: 0, y: 0)
greenBall.zPosition = 4



redBall.size = CGSize(width: 30, height: 30)
redBall.position = CGPoint(x: 0, y: 0)
redBall.zPosition = 4

spawnRedAndGreen()

}

func spawnRedAndGreen () {

self.addChild(greenBall)
self.addChild(redBall)


let upwardsMotion = SKAction.moveTo(y: 1, duration: ballSpeed)
let redDelay = SKAction.wait(forDuration: TimeInterval(randomBetweenNumbers(firstNum: 1, secondNum: 4)))
let greenDelay = SKAction.wait(forDuration: TimeInterval(randomBetweenNumbers(firstNum: 1, secondNum: 3)))

let redSequence = SKAction.sequence([upwardsMotion, redDelay])
let greenSequence = SKAction.sequence([upwardsMotion, greenDelay])



redBall.run(redSequence)
greenBall.run(greenSequence)

}

我还意识到每次调用该函数时节点数都会增加 2,但它不会显示在屏幕上

最佳答案

您需要指定要使用的节点或纹理/图像的大小,否则它将不可见。如果您指定颜色也很好。

redBall.size = CGSize(width: 0.1, height: 0.1)
redBall.color = .red

此外,您的操作还迫使节点快速移动(y = 600 太多了,您可以将其更改为 1),因此在进行上述更改后,您只会眨眼。

关于ios - 我想知道为什么我的节点不在我的 GameScene 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43455995/

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