2

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:

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:

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.

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

2
  • Remark: The Swift language does not require a semi-colon at the end of a statement. Commented Dec 22, 2014 at 10:39
  • Thanks - The doco actually says semi-colons are optional and I chose to keep using them. With xcode automatically splitting lines, it lets me easily see if it's the end of a command. Commented Dec 23, 2014 at 21:42

1 Answer 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:

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

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

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.

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)
}
Sign up to request clarification or add additional context in comments.

1 Comment

You may be right. I'm sure I had other examples that were valid syntax but I can't replicate it again now (after restarting xcode). I'll leave this open for a little and see if I can trigger another failure. In regards to adding vector's I'd got confused late at night and thought a swift library for vector arithmetic (github.com/seivan/VectorArithmetic) was part of the standard SDK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.