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

时间:2017-04-17 17:08:21

标签: ios swift3 sprite-kit game-development

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

另外请注意,球最初并不在场景中,我希望在调用该功能时将其添加到场景中。

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,但它并没有显示在屏幕上

2 个答案:

答案 0 :(得分:2)

您需要指定要使用的节点或纹理/图像的大小,否则它将无法显示。如果你指定颜色也会很棒。

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

此外,您的操作会强制节点快速移动(y = 600太多,您可以将其更改为1),因此在上述更改后您只会看到闪烁。

答案 1 :(得分:0)

已解决(感谢每个人的输入我终于开始工作了

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 = SKSpriteNode(imageNamed: "greenBall")
    greenBall.size = CGSize(width: 30, height: 30)
    greenBall.position = CGPoint(x: 0, y: 0)
    greenBall.zPosition = 4



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



    spawnRedAndGreen()

}

func spawnRedAndGreen () {




    let upwardsMotion = SKAction.moveTo(y: 1000, 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])


    self.addChild(redBall)

    redBall.run(redSequence)

    greenBall.run(greenSequence)

}