I'm using Sprite Kit and trying to write an animation that will send a message to an object. What I'm trying to accomplish is calling this code:

[self.tileSelectionManager swapSingleTileBackToNotSelected:_letterTile];

but by using SKActions. I first tried using a runBlock like so:

for (_letterTile in letterTileArray) {
    SKAction *swapToNonSelected = [SKAction runBlock:^ {
            [self.tileSelectionManager swapSingleTileBackToNotSelected:_letterTile];
        }];

    [_letterTiles runAction:swapToNonSelected];
}

but found when swapSingleTileBackToNotSelected fired, the _letterTile was nil. I looked into passing info into blocks, and tried writing (Lettertile *letterTile) after the ^, but I couldn't get this to work.

Then I looked at simply calling SKAction's performSelector onTarget, but I couldn't find anywhere how to call a method that wasn't in "self", much less how to account for the missing "withObject" that normal Objective-C incorporates but seems to be missing from Sprite Kit.

Can someone please tell me how I'm supposed to use an SKAction to call this method I have in my TileSelectionManager object and pass in my _letterTile?

Thanks!!

edit**

I'm accessing the _letterTile in a for loop

有帮助吗?

解决方案

The block will work. The problem is _letterTile was never assigned to a non-nil value to begin with. If it were, the block would gave retained the object and would send the original object even if you had set the variable to nil in the meantime.

Tip: set a breakpoint and look at the debugger's variables list.

其他提示

It had to do with how I was accessing my object. Since the line

[self.tileSelectionManager swapSingleTileBackToNotSelected:_letterTile];

outside of a block fires immediately, it functioned correctly. However, since I was iterating through a for loop which carried my _letterTile, by the time the block fired it had lost track of it.

Changing the for loop to (LetterTile *tile in letterTileArray) solved my problem.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top