访问数组中对象的方法。(迅速)

时间:2022-09-25 09:19:36

This problem seems easy enough, but I can't find the answer and my search terms seem to generic to get an answer from the web - hoping SO can help:

这个问题似乎很简单,但我找不到答案,我的搜索词似乎是通用的,可以从网上得到答案——希望能帮上忙:

I've created an array of SKSpriteNode objects in swift:

我在swift中创建了一个SKSpriteNode对象数组:

var images:[SKSpriteNode] = [];

I add some nodes:

我添加一些节点:

images+=[SKSpriteNode(imageNamed: "StarrySky")];
images+=[SKSpriteNode(imageNamed: "StarrySky")];
images+=[SKSpriteNode(imageNamed: "StarrySky")];
images+=[SKSpriteNode(imageNamed: "StarrySky")];

Now I get inconsistent results when I'm access methods of the SKSpriteNode with:

现在当我访问SKSpriteNode的方法时,得到的结果不一致:

images[0].position

For example:

例如:

func moveBackground(direction: CGVector) {
    //works
    let origin = images[0].position;
    var newPoint = CGPoint(x: origin.x + direction.dx, y: origin.y + direction.dy);
    positionBackground(newPoint);

    //next line has a compilation error.
    positionBackground(images[0].position + direction);
}

Sometimes it it works, other times xCode is saying Could not find member position. I figure it's saying the array doesn't have a member position, instead of the SKSpriteNode.

有时它是有效的,其他时候xCode是说找不到成员位置。我想它是说数组没有成员位置,而不是SKSpriteNode。

Any idea how I specify it's a method of the object in the array itself and not the array?

知道我怎么指定它是数组中对象的方法而不是数组吗?

1 个解决方案

#1


1  

The error message "Could not find member position" is misleading, and the problem has nothing to do with the sprite nodes being stored in an array.

错误消息“找不到成员位置”具有误导性,问题与存储在数组中的精灵节点无关。

The problem is that you are trying to add a CGPoint and a CGVector:

问题是,您正在尝试添加一个CGPoint和一个CGVector:

images[0].position + direction
    CGPoint--^  CGVector--^

and Swift does not have a + operator which takes (CGPoint, CGVector) as arguments.

Swift没有以(CGPoint, CGVector)为参数的+运算符。

Your other code

你的其他代码

let origin = images[0].position
var newPoint = CGPoint(x: origin.x + direction.dx, y: origin.y + direction.dy)
positionBackground(newPoint)

works because you add the point and the vector component-wise, i.e. you call the + operator with CGFloat arguments.

之所以有效,是因为您添加了点和向量组件,即使用CGFloat参数调用+运算符。

If you want to shorten this to

如果你想把它缩短成

positionBackground(images[0].position + direction)

then you would have to define a custom + operator:

然后你需要定义一个自定义+运算符:

func +(lhs: CGPoint, rhs: CGVector) -> CGPoint {
    return CGPoint(x: lhs.x + rhs.dx, y: lhs.y + rhs.dy)
}

#1


1  

The error message "Could not find member position" is misleading, and the problem has nothing to do with the sprite nodes being stored in an array.

错误消息“找不到成员位置”具有误导性,问题与存储在数组中的精灵节点无关。

The problem is that you are trying to add a CGPoint and a CGVector:

问题是,您正在尝试添加一个CGPoint和一个CGVector:

images[0].position + direction
    CGPoint--^  CGVector--^

and Swift does not have a + operator which takes (CGPoint, CGVector) as arguments.

Swift没有以(CGPoint, CGVector)为参数的+运算符。

Your other code

你的其他代码

let origin = images[0].position
var newPoint = CGPoint(x: origin.x + direction.dx, y: origin.y + direction.dy)
positionBackground(newPoint)

works because you add the point and the vector component-wise, i.e. you call the + operator with CGFloat arguments.

之所以有效,是因为您添加了点和向量组件,即使用CGFloat参数调用+运算符。

If you want to shorten this to

如果你想把它缩短成

positionBackground(images[0].position + direction)

then you would have to define a custom + operator:

然后你需要定义一个自定义+运算符:

func +(lhs: CGPoint, rhs: CGVector) -> CGPoint {
    return CGPoint(x: lhs.x + rhs.dx, y: lhs.y + rhs.dy)
}