xcode 替换精灵中的图片-iPhone的cocos2d游戏开发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1563109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:53:55  来源:igfitidea点击:

Replacing image in sprite - cocos2d game development of iPhone

xcodecocos2d-iphonesprite

提问by Sagar R. Kothari

I want to change the sprite image.

我想更改精灵图像。

Say for example:

比如说:

mainSprite=[Sprite spriteWithFile:@"redFile.png"];
[self addChild:mainSprite];

Here, Sprite is already added to a layer. I have mainSprite (pointer) which can access it.

在这里,Sprite 已经添加到图层中。我有可以访问它的 mainSprite(指针)。

If I change

如果我改变

[mainSprite setOpacity:150];

it works perfectly. But here I want to change the sprite image instead of opacity.

它完美地工作。但在这里我想改变精灵图像而不是不透明度。

But I don't know how.

但我不知道怎么做。

回答by 5upr1

@sagar: In cocos2d 0.99.x I use

@sagar:在 cocos2d 0.99.x 中我使用

[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"sprite.png"]];

It works. The next question is, how can I get back to my previous sprite? Thanks

有用。下一个问题是,我怎样才能回到我以前的精灵?谢谢

回答by Sagar R. Kothari

Ok. Damn Simple.

好的。该死的简单。

I find it by R & D.

我是通过研发找到的。

Texture2D *x=[[Texture2D alloc]initWithImage:[UIImage imageNamed:@"box-purple-dark.png"]];
[mainSprite setTexture:x];

回答by Ali Raza

Replace image in sprite :

替换精灵中的图像:

CCTexture2D *tex1 = [[CCTextureCache sharedTextureCache] addImage:@"new-image.png"];
[mainSprite setTexture:tex1]; 

回答by P.J.Radadiya

Replace image in sprite :

替换精灵中的图像:

[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"image.png"]];