views:

957

answers:

4

Hello! I'm trying to change the image that a sprite from an array is displaying. Here is the code I'm using:

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

I get the error message:

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

What am I doing wrong?

Thanks for reading.

A: 

I don't think that this is the way to do this. Rather than that you should call one of the init methods:

- (id) initWithCGImage:(CGImageRef)image;

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

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

In my game i do this differently, i have a class for my object called 'Zed' which i also store in an array. It has a sprite as a field and if i want to change the image i swap the whole sprite and make sure i hide the old and show the new one.

Christian
A: 

Hello, I m trying to do the same, but it doesn t seem to work. I use the spriteWithTexture fonction. my code :

Sprite *img = (Sprite *) [self getChildByTag:1];

img = [Sprite spriteWithTexture:(Texture2D *)[baseImg objectAtIndex:i] ];

  • baseImg is an NSArray with some Texture2D.

But the image does not change =(

Thank you in advance for your help =)

Axolotl
A: 

Try using

[((Sprite *)[enemiesArray objectAtIndex:index]) setImage:baseImage];

Biranchi
A: 

I do it like this:

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