不在函数错误时的实例框架类型

时间:2022-10-19 04:26:26

I am making a game in which objects spawn on the map in a certain location and I have multiple functions for different objects and needed a reference so I put my code outside of the function to widen its scope but now I get an error that says

我正在制作一个游戏,其中对象在某个位置在地图上产生,并且我有不同对象的多个函数,需要一个引用,所以我将我的代码放在函数之外以扩大其范围,但现在我得到一个错误

Instance type 'frame' cannot be used on type 'Levelunit'

If i put it back into the function it is completely fine but it seems to have a problem when I put it outside the function. Here is my code:

如果我把它放回到函数中它完全没问题,但是当我把它放在函数之外时似乎有问题。这是我的代码:

let yAxisSpawnLocations: [CGFloat] = [0]
let xAxisSpawnLocations: [CGFloat] = {

    var spawnLocations:[CGFloat] = []   //Create 5 possible spawn locations
    let numberOfNodes = 5

    for i in 0...numberOfNodes - 1 {

        var xPosition = (frame.maxX) / CGFloat((numberOfNodes - 1)) * CGFloat(i)

        xPosition += thePlayer.size.width/2

        xPosition -= frame.maxX/1.6
        spawnLocations.append( xPosition )

    }

    return spawnLocations

}()

I looked it up and didn't quite understand the answer given. Could someone tell me what i'm doing wrong please.

我查了一下,并不太明白给出的答案。有人能告诉我,我做错了。

BTW, LevelUnit is the name of my class.

BTW,LevelUnit是我班级的名字。

1 个解决方案

#1


1  

You are trying to use self before Levelunit has be fully initialized. For example, this is one place where you use self implicitly:

在Levelunit完全初始化之前,您正试图使用​​self。例如,这是您隐式使用self的地方:

var xPosition = (frame.maxX) / CGFloat((numberOfNodes - 1)) * CGFloat(i)

it is the same as:

它是一样的:

var xPosition = (self.frame.maxX) / CGFloat((numberOfNodes - 1)) * CGFloat(i)

So, what you can do is to initialize this property at Levelunit initializer (which I would skip if you don't want to have a dance with automatic initializer inheritance rules), or to initialize it in didMoveToView, which will work fine. If I am not wrong, Levelunit is probably subclass of a SKScene, right? If so, just do this:

所以,你可以做的是在Levelunit初始化器初始化这个属性(如果你不想与自动初始化器继承规则跳舞,我会跳过它),或者在didMoveToView中初始化它,这将正常工作。如果我没错,Levelunit可能是SKScene的子类,对吧?如果是这样,请执行以下操作:

//Define a property of Levelunit like this

 var xAxisSpawnLocations:[CGFloat] = []

Then just initialize it inside of didMoveToView:

然后在didMoveToView中初始化它:

let numberOfNodes = 5

for i in 0...numberOfNodes - 1 {

    var xPosition = (self.frame.maxX) / CGFloat((numberOfNodes - 1)) * CGFloat(i)

    xPosition += thePlayer.size.width/2
    xPosition -= frame.maxX/1.6
    xAxisSpawnLocations.append( xPosition )

}

#1


1  

You are trying to use self before Levelunit has be fully initialized. For example, this is one place where you use self implicitly:

在Levelunit完全初始化之前,您正试图使用​​self。例如,这是您隐式使用self的地方:

var xPosition = (frame.maxX) / CGFloat((numberOfNodes - 1)) * CGFloat(i)

it is the same as:

它是一样的:

var xPosition = (self.frame.maxX) / CGFloat((numberOfNodes - 1)) * CGFloat(i)

So, what you can do is to initialize this property at Levelunit initializer (which I would skip if you don't want to have a dance with automatic initializer inheritance rules), or to initialize it in didMoveToView, which will work fine. If I am not wrong, Levelunit is probably subclass of a SKScene, right? If so, just do this:

所以,你可以做的是在Levelunit初始化器初始化这个属性(如果你不想与自动初始化器继承规则跳舞,我会跳过它),或者在didMoveToView中初始化它,这将正常工作。如果我没错,Levelunit可能是SKScene的子类,对吧?如果是这样,请执行以下操作:

//Define a property of Levelunit like this

 var xAxisSpawnLocations:[CGFloat] = []

Then just initialize it inside of didMoveToView:

然后在didMoveToView中初始化它:

let numberOfNodes = 5

for i in 0...numberOfNodes - 1 {

    var xPosition = (self.frame.maxX) / CGFloat((numberOfNodes - 1)) * CGFloat(i)

    xPosition += thePlayer.size.width/2
    xPosition -= frame.maxX/1.6
    xAxisSpawnLocations.append( xPosition )

}