Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

I do it like this:

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

You'd better do it this way:

Somewhere in the init:

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

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

Somewhere in action:

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

*.plist and *.png could be done with Zwoptex.

Other solutions might break you animation when you are trying to change the texture while doing it.

Try using

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top