swift中的二维数组(与1比较)



我正在尝试为我的Breakout游戏创建一个关卡系统,但由于一个错误而无法完成。

let LEVEL_1 : [[Int]] = [
    [0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0],
    [0,0,0,1,1,0,0,0],
    [0,0,0,1,1,0,0,0],
    [0,1,1,1,1,1,1,0],
    [0,1,1,1,1,1,1,0],
    [0,0,0,1,1,0,0,0],
    [0,0,0,1,1,0,0,0],
    [0,0,0,0,0,0,0,0],]

是级别,然后是方法

   func buildLevel(level: [Int])
{
    let offset:Float = (Float(self.frame.size.width) - (Float(brickWidth) * Float(numberOfBricks) + padding * (Float(numberOfBricks) - 1) ) ) / 2
    var len = level.count
    for(var i = 0; i < len; i++){
        var yOffset:CGFloat{
            switch i {
            case 1:
                return self.frame.size.height * 0.8
            case 2:
                return self.frame.size.height * 0.6
            case 3:
                return self.frame.size.height * 0.4
            default:
                return 0
            }
        }
        for(var j = 0; j < W_LEN; j++){
            if level[i][j] == 1{
                let brick = SKSpriteNode(imageNamed: "brick")
                let calc1:Float = Float(i) - 0.5
                let calc2:Float = Float(j) - 1
                brick.position = CGPointMake(CGFloat(calc1 * Float(brick.frame.size.width) + calc2 * padding + offset), yOffset)
                self.addChild(brick)
                brick.physicsBody = SKPhysicsBody(rectangleOfSize: brick.frame.size)
                brick.physicsBody?.allowsRotation = false
                brick.physicsBody?.friction = 0
                brick.name = brickCategoryName
                brick.physicsBody?.categoryBitMask = ColliderCategory.brick.rawValue


            }
        }
    }
}

错误:"不能将int类型的值下标为int类型的索引"如果level[i][j] == 1{

任何想法我可以解决这个错误,让我的水平正常工作吗?

看看你的代码。你在说:

func buildLevel(level: [Int])

所以level是一个维的Int数组。因此,后来说level[i][j]是没有意义的。level[1]是一个Int,你不能在Int后面说[j](一个下标)(这正是错误信息告诉你的)。

相关内容

  • 没有找到相关文章

最新更新