2014-04-01 37 views
0

好的,所以我在直接修改NSMutableArray的內容時遇到問題。基本上,我有一個在初始化函數中創建的SKSpriteNodes的數組。我希望能夠在指定索引處修改SKSpriteNode,具體取決於用戶觸摸屏幕的位置。我可以得到正確的索引,並且我可以獲取節點,但我似乎無法獲得節點的屬性更改。無法修改SKSpriteNodes的objectAtIndex NSMutableArray

爲了清晰起見,我在這裏包含了完整的代碼,但問題出在最後一個函數(touchesBegan)中。

我想要做的是能夠在指定的索引處設置SKSpriteNode的屬性。調試消息說* leftTile正在正確創建幷包含正確的數據,但當我調用setHidden函數時,更改不會在屏幕上生效。

#import "FLGameScene.h" 

@implementation FLGameScene 

/** 
* Convert Multidimensional Coordinates to Single Index 
* 
* @param int row     Row index. 
* @param int col     Column index. 
* 
* @return int 
*/ 
- (int)multiToSingle:(int)row :(int)col 
{ 
    return ((row * numTiles) + col); 
} 

/** 
* Determine the Iso Coordinates closest to the Real Position. 
* 
* @param CGPoint coords   Real coordinates. 
* 
* @return CGPoint 
*/ 
- (CGPoint)xyToIso:(CGPoint)coords 
{ 
    // Determine the Row and Column 
    int x = (coords.x - offsetX)/tileSize; 
    int y = (coords.y - offsetY)/tileSize; 

    // Create the CGPoint 
    return CGPointMake(x, y); 
} 

/** 
* Determine the Real Coordinates for a specified index. 
* 
* @param CGPoint coords   Iso coordinates. 
* 
* @return CGPoint 
*/ 
- (CGPoint)isoToXY:(CGPoint)coords 
{ 
    // Determine the X and Y Values 
    float x = (coords.x * tileSize) + offsetX; 
    float y = (coords.y * tileSize) + offsetY; 

    // Create the CG Point 
    return CGPointMake(x, y); 
} 

/** 
* Initialisation Function. 
* 
* @param CGSize size    Size of the Scene Area. 
* 
* @return *SKScene 
*/ 
- (id)initWithSize:(CGSize)size 
{ 
    // Call the Parent Constructor 
    if (self = [super initWithSize:size]) 
    { 
     // Log a Message 
     if (debugMode) NSLog(@"Game Scene Created. Size: %f x %f", size.width, size.height); 

     // Set the Background Colour 
     [self setBackgroundColor:[UIColor colorWithRed:0.15f green:0.25f blue:1.0f alpha:1.0f]]; 

     // Set the Tile Size 
     tileSize = 50.0f; 

     // Set the Number of Tiles 
     numTiles = 14; 

     // Determine the Offset 
     offsetX = (size.width/2.0f) - ((numTiles/2) * tileSize); 
     offsetY = (size.height/2.0f) - ((numTiles/2) * tileSize); 

     // Create the Lilypad Texture 
     SKTexture *lilypadTex = [SKTexture textureWithImageNamed:@"lilypad"]; 

     // Create the Frog Texture 
     SKTexture *frogTex = [SKTexture textureWithImageNamed:@"frog"]; 

     // Create the Tile Array 
     _tileArray = [[NSMutableArray alloc] initWithCapacity:(numTiles * numTiles)]; 

     // Loop through and Create the Grid 
     for (int col = 0; col < numTiles; col++) 
     { 
      for (int row = 0; row < numTiles; row++) 
      { 
       // Create a Random BOOL Value for the Frogs 
       int containsFrog = (rand() % 2); 

       // Log a Message 
       if (debugMode) NSLog(@"Square %i, %i :: %i", row, col, containsFrog); 

       // Calculate the X Position 
       float posX = (col * tileSize) + offsetX; 

       // Calculate the Y Position 
       float posY = (row * tileSize) + offsetY; 

       // Create the Sprite Node 
       SKSpriteNode *tile = [[SKSpriteNode alloc] initWithTexture:lilypadTex]; 

       // Set the Tile Origin 
       [tile setAnchorPoint:CGPointZero]; 

       // Add the Sprite User Data Dictionary 
       [tile setUserData:[NSMutableDictionary dictionary]]; 

       // Set the Flag for Containing a Frog 
       [tile.userData setValue:[NSNumber numberWithInt:containsFrog] forKey:@"containsFrog"]; 

       // Set the Tile Position 
       [tile setPosition:CGPointMake(posX, posY)]; 

       // Set the Tile Size 
       [tile setSize:CGSizeMake(50.0f, 50.0f)]; 

       // Add the Tile to the Board 
       [self addChild:tile]; 

       // Add the Tile to the Array 
       [_tileArray addObject:[tile copy]]; 
      } 
     } 

     for (SKSpriteNode *tile in _tileArray) 
     { 
      if ([[tile.userData valueForKey:@"containsFrog"] boolValue] == YES) 
      { 
       // Create a Frog Node 
       SKSpriteNode *frog = [[SKSpriteNode alloc] initWithTexture:frogTex]; 
       [frog setAnchorPoint:CGPointZero]; 
       [frog setSize:CGSizeMake(tileSize, tileSize)]; 
       [frog setPosition:tile.position]; 

       // Add the Frog Node 
       [self addChild:frog]; 
      } 
     } 
    } 

    // Return the Instance 
    return self; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // Get the Touches 
    UITouch *touch = [touches anyObject]; 

    // Get the Touch Location 
    CGPoint touchLocation = [touch locationInNode:self]; 

    // Determine the Tile X and Y 
    CGPoint tileIndex = [self xyToIso:touchLocation]; 

    // Log a Debug Message 
    if (debugMode) NSLog(@"Tile at %f, %f Tapped", tileIndex.x, tileIndex.y); 

    // Search Available Tiles 

    // Tile to the Left 
    int leftTileIndex = [self multiToSingle:(int)tileIndex.x - 2 :(int)tileIndex.y]; 

    // Get the Tile Object 
    SKSpriteNode *leftTile = [_tileArray objectAtIndex:leftTileIndex]; 

    if (debugMode) NSLog(@"Description: %@", [leftTile.userData description]); 

    // Check for Frogs 
    if ([[leftTile.userData objectForKey:@"containsFrog"] boolValue] == NO) 
    { 
     NSLog(@"Tile empty"); 
     [leftTile setHidden: YES]; 
    } 
} 

@end 
+1

有一個你可能感興趣的預處理器常量'DEBUG'。另見['DLog()'](http://stackoverflow.com/questions/9659763/difference-between-nslog-and-dlog)宏。 –

回答

2
  // Add the Tile to the Board 
      [self addChild:tile]; 

      // Add the Tile to the Array 
      [_tileArray addObject:[tile copy]]; 

的精靈添加到場景圖是不一樣的你,因爲你創建它的copy添加到陣列中的一個。不要複製,它會起作用。

更好:把所有的瓷磚都放在一個SKNode中,這樣你就不需要額外的數組了,相反,你可以通過children數組來引用瓷磚。