简体   繁体   中英

I want to know why my nodes aren't in my GameScene

Below is my didMove(toView:) and the function I'm using to spawn my sprite nodes. Ignore the colored block portion of the code but I'm trying to get the balls to spawn and go up from the origin, I received no errors from the code but the ball isn't showing up in the view. I thank you very much if you are able to take the time out of your day to review my problem. Please ask questions if you need a clearer explanation of my problem.

Also please consider that the balls aren't in the scene initially and i want the added to the scene upon calling the function.

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)

}

this is the function i used to generate the random numbers...

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

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

}

UPDATED CODE (note still doesnt work).

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)

}

I have also realized that the node count goes up by 2 everytime i call the function but it doesn't show up on the screen

You need to specify the size of your node or a texture/image to use otherwise it won't be visible. It would be also great if you specify the color.

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

Also your action forces the node to move insanely fast ( y = 600 is too much, you can change it to 1 ) so after the change above you will only see a blink.

SOLVED (thanks for everyones input i finally got it to work

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)

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM