0

Currently, when running this code, a colored ball runs from the top of the screen to the bottom. When it reaches the bottom it respawns at the top and drops again. I need the ball to change to a random color each time it spawns again. I have a colorChange action but it still doesn't change the color of the ball when it runs.

import SpriteKit

class GameScene: SKScene {



    let ball = SKShapeNode(circleOfRadius: 20)
    let label = SKLabelNode(fontNamed: "Futura")

    let colorChange = SKAction.colorizeWithColor(.blueColor(), colorBlendFactor: 1, duration: 1)


    let randomRed = Int(arc4random_uniform(255))
    let randomGreen = Int(arc4random_uniform(255))
    let randomBlue = Int(arc4random_uniform(255))

    override func didMoveToView(view: SKView) {

        let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        self.physicsBody = sceneBody

        //Ball Transition
        let ballTransition = SKAction.sequence([SKAction.fadeInWithDuration(1)])
        ball.runAction(ballTransition)

        ball.fillColor = UIColor(red: 100, green: 100, blue: 0, alpha: 1)
        ball.physicsBody = SKPhysicsBody(circleOfRadius: 25)
        ball.physicsBody?.affectedByGravity = false

        ball.position = CGPoint(x: self.frame.size.width/2, y: CGFloat(self.frame.size.height*1))

        ballMovement()

        self.addChild(ball)

        self.addChild(label)
    }

    func ballMovement() {
        let moveBall = SKAction.moveToY(0, duration: 3)
        let goBackUp = SKAction.moveToY(self.frame.size.height, duration:0)
        let keepFalling = SKAction.sequence([moveBall, goBackUp, colorChange])
        ball.runAction(SKAction.repeatActionForever(keepFalling))

        //Label Sprite
        label.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
        label.fontColor = SKColor.redColor()
        label.fontSize = 30
    }




    override func update(currentTime: CFTimeInterval) {

        label.text = "\(ball.position.y)"

        if ball.position.y < 26 {
            ball.position = CGPoint(x: self.frame.size.width/2, y: CGFloat(self.frame.size.height*1))
        }
    }


}
Bono
  • 4,757
  • 6
  • 48
  • 77
  • Your fill color blue is 0. When you multiply anything by 0, it remains zero, even with a color blend factor of 1. If you plan on using colorize, you need to start with white (1,1,1,1). I am not sure if the action sets the color when complete, so instead just change the fill color to your desired color. Use `customActionWithDuration` if you want to make a transition effect – Knight0fDragon Aug 15 '16 at 18:42

2 Answers2

1

Instead of using an SKAction to change the color, just do

ball.color = UIColor...

To generate a random color check this out: How to make a random background color with Swift

and use that to generate a random color

ball.color = .randomColor()
Community
  • 1
  • 1
Entitize
  • 4,553
  • 3
  • 20
  • 28
1

I suggest you don't use an SKShape for an important piece that moves because I find that they can slow down the app and cause problems. I would use an SKSpritenode instead for the ball. If you then make the ball default as all white, you can change the colour by doing this:

ball.colorBlendFactor = 1

ball.color = SKColor(hue: randomNumberFunctionBetweenZeroAndOne(), saturation: 0.75, contrast: 0.5) 

The hue should be a random value between 0 and 1.

If you put this inside a function, you could call the function to change the colour of the ball inside and SKAction or anywhere else.

Nik
  • 1,664
  • 2
  • 14
  • 27