试图在函数中创建精灵

时间:2015-09-16 02:14:29

标签: swift xcode6 sprite-kit

我正在尝试生成两个矩形并使它们从左向右移动,颜色为红色蓝色或橙色(随机),但只创建其中一个。

import SpriteKit

class GameScene: SKScene {

    override func didMoveToView(view: SKView) {

        var randomeNumber = arc4random_uniform(3)
        var randomeNumber1 = arc4random_uniform(3)
        var randomeNumber2 = arc4random_uniform(3)

        //Actions
        var platform1Action = SKAction.moveTo(CGPoint(x: 400, y: 200), duration: 4)
        var platform1action = platform1Action

        var platform2Action = SKAction.moveTo(CGPoint(x: 0, y: 200), duration: 6)
        var platform2action = platform2Action

        //Create color blue if randomeNumber = 0
        if randomeNumber == 0{
            createPlatformOne(SKColor.blueColor(), action: platform1action)
        }

        //Create color red if randomeNumber = 1
        if randomeNumber == 1{
            createPlatformOne(SKColor.redColor(),action: platform1action)
        }

        //Create color orange if randomeNumber = 2
        if randomeNumber == 2{
            createPlatformOne(SKColor.orangeColor(),action: platform1action)
        }

        var ball = SKShapeNode(circleOfRadius: 10)
        ball.name = "ball"
        ball.fillColor = SKColor.redColor()
        ball.strokeColor = SKColor.redColor()
        ball.position = CGPoint(x: self.frame.width/2, y: 400)
        self.addChild(ball)

        //Create color blue if randomeNumber = 0
        if randomeNumber1 == 0{
            createPlatformTwo(SKColor.blueColor(), action1: platform2action)
        }

        //Create color red if randomeNumber = 1
        if randomeNumber1 == 1{
            createPlatformTwo(SKColor.redColor(),action1: platform2action)
        }

        //Create color orange if randomeNumber = 2
        if randomeNumber1 == 2{
            createPlatformTwo(SKColor.orangeColor(),action1: platform2action)
        }
    }

    func createPlatformOne( platformColor:SKColor, action:SKAction){

        var platform1 = SKShapeNode(rectOfSize: CGSize(width: 200, height: 10))
        platform1.name = "platform1"
        platform1.fillColor = platformColor
        platform1.strokeColor = platformColor
        platform1.position = CGPoint(x: 820, y:200)

        self.addChild(platform1)

        platform1.runAction(action)
    }

    func createPlatformTwo( platformColor:SKColor, action1:SKAction){

        var platform2 = SKShapeNode(rectOfSize: CGSize(width: 200, height: 10))
        platform2.name = "platform1"
        platform2.fillColor = platformColor
        platform2.strokeColor = platformColor
        platform2.position = CGPoint(x: 820, y:200)

        self.addChild(platform2)

        platform2.runAction(action1)
    }
}

我知道我的班级createPlatformTwo正在被召唤。请帮忙。

0 个答案:

没有答案