2009-09-15 29 views
0

我正在嘗試更改數組中的精靈正在顯示的圖像。這裏是代碼我使用:如何更改數組中的cocos2d精靈圖像?

((Sprite *)[enemiesArray objectAtIndex:index]).image = baseImage; 

我得到的錯誤信息:

error: request for member 'image' in something not a structure or union 

我在做什麼錯?

感謝您的閱讀。

回答

0

我不認爲這是這樣做的方式。 而不是你應該調用其中一個init方法:

- (id)initWithCGImage:(CGImageRef)image;

- (id)initWithTexture:(Texture2D *)tex;

- (id)initWithFile:(NSString *)imageFile;

在我的遊戲中,我做了不同的處理,我有一個名爲'Zed'的對象的類,我也將它存儲在一個數組中。它有一個精靈作爲一個領域,如果我想改變圖像我交換整個精靈,並確保我隱藏舊的並顯示新的。

0

使用try

[((精靈*)[enemiesArray objectAtIndex:指數])setImage:baseImage];

1

我不喜歡這樣寫道:

Texture2D *texture = [[Texture2D alloc] initWithImage:myUIImage] 
[sprite setTexture: texture]; 
1

你最好做這種方式:

某處在init:

CCSpriteBatchNode *spritesheet = [CCSpriteBatchNode batchNodeWithFile:@"car_anim.png"]; 
[self addChild:spritesheet]; 

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"car_anim.plist"]; 

某處在行動:

[yourSprite setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"car_left.png"]]; 

* .plist和* .png可以用Zwoptex來完成。

其他解決方案可能會在您嘗試更改紋理時破壞動畫。