2013-05-11 51 views
-3

我需要一种将图像纹理从nsmutable数组中的一个索引切换到另一个索引的方法。如何将nsarray对象从一个索引切换到另一个索引

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

要切换的指数,

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

    } 
+0

如何将添加图像到这个方法。 – 2013-05-11 12:22:13

+0

我在答案中添加了更多信息.. – 2013-05-11 12:28:25

相关问题