iOS 和 Cocos2d - 更改 CCSprite 图像和新尺寸 = 失败

发布于 2024-12-18 06:54:28 字数 1098 浏览 3 评论 0原文

我的游戏中的增强功能之一是主精灵尺寸的减小。 因此,为了记忆起见,我没有在每个帧中循环 sprite.scale,而是以低于原始精灵的百分比大小重新保存精灵,并且只想替换它。然后,一旦“死亡”发生或计时器耗尽,原始精灵就会返回。

因此,我使用此代码将其变小:

[player setTexture:[[CCTextureCache sharedTextureCache] addImage:@"player-small.png"]];

以及此代码用于重置回正常状态:

[player setTexture:[[CCTextureCache sharedTextureCache] addImage:@"player-orig.png"]];

但是,原始图像(在初始化中和重置时)看起来 普通的。但是,当我将其更改为新精灵(其尺寸恰好是原始精灵的 75%)时,它会更改它,但仅显示新精灵的象限,但具有原始尺寸。

我尝试在重新纹理化精灵之前修改 sprite.contentsize,但所做的只是更改大小,但不会影响图像混乱的问题。

以下是一些视觉示例:

原始:

Original


修改 contentSize 的重新纹理图像:

带 contentSize 修改的重新纹理化图像


将图像重新纹理化为原始图像,但 contentSize 未正确重置(哎呀,但这根本不是问题 - 我只是忘记读取尺寸代码):

将图像重新文本化为原始图像,但 contentSize 未正确重置(哎呀,但这不是一个问题 - 我只是忘记读取尺寸代码):

PS - 我的所有精灵都有“-hd.png”版本,所以我只是想添加这一点,以防有人想知道(图像到目前为止的测试已经仅使用过“非视网膜”模拟器)。

谢谢你!


编辑:在视网膜模拟器上测试期间也会出现问题。

One of the powerups in my game is a decrease in size of the main sprite.
So for memory sake, instead of looping a sprite.scale through each frame, I resaved the sprite at a percentage size lower than the original sprite, and just wish to replace it. Then once "death" occurs or timer runs out, the original sprite returns.

So, I am using this code for making it small:

[player setTexture:[[CCTextureCache sharedTextureCache] addImage:@"player-small.png"]];

and this code for resetting back to normal:

[player setTexture:[[CCTextureCache sharedTextureCache] addImage:@"player-orig.png"]];

however, the orignal image (both in the init and when reset) looks normal. But when I change it to the new sprite (which has dimensions exactly 75% of the original), it changes it, but shows only a quadrant of the new sprite, but with the original dimensions.

I tried modifying the sprite.contentsize before retexturing the sprite, but all that did was change the size, but not affect the issue with the image messing up.

Here's some visual examples:

Original:

Original


Retextured Image with contentSize modification:

Retextured Image with contentSize modification


Retextured image to the Original image with the contentSize not reset properly (oops, but this isn't a problem at all - i just forgot to readd the size code):

Retexted image to the Original image with the contentSize not reset properly (oops, but this isn't a problem at all - i just forgot to readd the size code):

PS -- I do have "-hd.png" versions of all of my sprites, so i just wanted add that incase anyone wanted to know (the images and testing so far has only been on "nonretina" simulator).

THANK YOU!


EDIT: Issue occurs during testing on retina simulator as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把时间冻结 2024-12-25 06:54:28

由于您主要关心的是内存,因此我建议仅使用纹理图集。

我将您的原始图像复制到图像编辑程序中以查看其尺寸。该图像的大小为 73x71 像素,这意味着它作为尺寸为 128x128 的纹理存储在内存中,因为纹理具有二维的能力。这将使纹理使用 64 KB 内存(假设 32 位颜色深度)。使用纹理图集,您将能够更紧密地打包纹理,并且可以使用 CCSprite setDisplayFrame 方法。

此外,您正在使用纹理缓存:

[CCTextureCache sharedTextureCache] addImage:@"player-small.png"]
[CCTextureCache sharedTextureCache] addImage:@"player-orig.png"]

除非您显式释放该纹理的内存,否则两个纹理都将保留在内存中。如果您释放当前未使用的纹理的内存,并经常交换这些纹理,您的游戏将花费大量时间释放内存并从设备的闪存重新加载图像 - 即使图像相对较小。

如果有多个精灵使用任一纹理,则尝试卸载/重新加载纹理没有任何意义,因为会出现两个或多个精灵使用任一纹理的情况。

最后,我们讨论最多 64 KB 的内存。如果您担心内存使用和性能,您应该做的就是加载 PVR 纹理,这些纹理在内存和磁盘上都是 PNG 文件大小的一小部分(但会失去一些颜色鲜艳度)。

查看 TexturePacker 用于创建纹理图集和 PVR 图像。

Since your main concern is about memory, I recommend to simply use a texture atlas.

I copied your original image into an image editing program to see what its dimensions are. The image is 73x71 pixels in size, that means it's stored in memory as a texture with dimensions 128x128 since textures will have power of two dimensions. This will have the texture use up 64 KB of memory (assuming 32-bit color depth). With a texture atlas you'll be able to pack your textures more tightly, and you can change the frame that is displayed more easily with the CCSprite setDisplayFrame method.

Furthermore, you are using the texture cache:

[CCTextureCache sharedTextureCache] addImage:@"player-small.png"]
[CCTextureCache sharedTextureCache] addImage:@"player-orig.png"]

Unless you explicitly free the memory of that texture, both textures will remain in memory anyway. If you do free the memory of the texture you're not currently using, and exchange these texture often, your game will spend a great deal of time releasing memory and reloading the image from the device's flash memory - even if the image is relatively small.

And if there's more than one sprite using either texture it wouldn't make any sense to try and unload/reload the textures because there will be occurrences where two or more sprites will use either texture.

Finally, we're talking about 64 KB of memory at most. If you're concerned about memory usage AND performance, the thing you should do is to load PVR textures instead, which are a fraction of the size of a PNG file both in memory and on disk (but losing some color vibrancy).

Have a look at TexturePacker for creating texture atlases and PVR images.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文