问题
This is my code
- (void) update:(NSTimeInterval)currentTime
{
[self.children enumerateObjectsUsingBlock:^(SKSpriteNode * child, NSUInteger idx, BOOL *stop) {
child.position = CGPointMake(child.position.x-self.scrollingSpeed, child.position.y);
if (child.position.x <= -child.size.width){
float delta = child.position.x+child.size.width;
child.position = CGPointMake(child.size.width*(self.children.count-1)+delta, child.position.y);
}
}];
}
And i am getting an an error
Incompatible block pointer types sending 'void (^)(SKSpriteNode *__strong, NSUInteger, BOOL *)' to parameter of type 'void (^ _Nonnull)(SKNode * _Nonnull __strong, NSUInteger, BOOL * _Nonnull)'
and I don't know why. I tried changing it to SKNode
but it says size does not exist.
Any ideas would be greatly appreciated.
回答1:
The enumeration block syntax is not correct, you need to change:
[self.children enumerateObjectsUsingBlock:^(SKSpriteNode * child, NSUInteger idx, BOOL *stop)
{
child.position = CGPointMake(child.position.x-self.scrollingSpeed, child.position.y);
if (child.position.x <= -child.size.width)
{
float delta = child.position.x+child.size.width;
child.position = CGPointMake(child.size.width*(self.children.count-1)+delta, child.position.y);
}
}];
to
[self.children enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop)
{
SKSpriteNode *child = (SKSpriteNode *)object;
child.position = CGPointMake(child.position.x-self.scrollingSpeed, child.position.y);
if (child.position.x <= -child.size.width)
{
float delta = child.position.x+child.size.width;
child.position = CGPointMake(child.size.width*(self.children.count-1)+delta, child.position.y);
}
}];
The enumeration block's first parameter is of type id
not SKSpriteNode
. There is no such implementation for NSArray.
来源:https://stackoverflow.com/questions/33584180/incompatible-block-pointer-skspritenode