-3

我需要一种在 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;
4

1 回答 1

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]];

  }
于 2013-05-11T11:52:16.203 回答