如何将nsarray对象从一个索引切换到另一个索引

时间:2013-05-11 11:44:47

标签: iphone ios objective-c

我需要一种方法在nsmutable数组中将图像纹理从一个索引切换到另一个索引。

balloonTextures = [NSMutableArray array];

    // Add each of the balloon textures into the balloon textures array
    [balloonTextures addObject:[SPTexture textureWithContentsOfFile:@"greentutorial.png"]];
    [balloonTextures addObject:[SPTexture textureWithContentsOfFile:@"redtutorial.png"]];

    SPImage *image = [SPImage imageWithTexture:[balloonTextures objectAtIndex:0]];
    image.x = 0;
    image.y = 10 ;
    [playFieldSprite addChild:image];
    [image addEventListener:@selector(onTouchBalloon:) atObject:self forType:SP_EVENT_TYPE_TOUCH];

正如你所看到的,我有一个NSMutableArray,其图像包含图像纹理greentutorial。现在我想用红色纹理(红色教程)用20秒内调用的方法切换它。

 -(void)changeColor{

 image = [SPImage imageWithTexture:[balloonTextures objectAtIndex:1]];

  }

然而,它根本不起作用。我知道图像正在被调用,因为如果我在方法中添加下面的代码并且它可以工作。

  image.x = 300;

1 个答案:

答案 0 :(得分:1)

要切换索引,

-(void) swapTexturesAtIndex:(int) index1 and:(int) index2{
    id temp1 = [balloonTextures objectAtIndex:index1];
    id temp2 = [balloonTextures objectAtIndex:index2];
    [balloonTextures replaceObjectAtIndex:index1 withObject:temp2];
    [balloonTextures replaceObjectAtIndex:index2 withObject:temp1];
}

编辑1:

为什么不用这样的东西,

-(void)changeImageTextureFromIndex:(int) index{

 image = [SPImage imageWithTexture:[balloonTextures objectAtIndex:index]];

  }
相关问题