iOS和Cocos2d - 改变CCSprite的形象和新维度= FAIL [英] iOS and Cocos2d - Changing CCSprite's image AND new dimensions = FAIL

查看:95
本文介绍了iOS和Cocos2d - 改变CCSprite的形象和新维度= FAIL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的游戏中的一个强大的主要精灵的大小减少。
所以为了记忆,而不是通过每个框架循环sprite.scale,我重新保存精灵的百分比大小低于原来的精灵,只是希望更换它。



因此,我使用这个代码使它变小:



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



并且此代码重置为正常:



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



然而,orignal镜像但是当我把它改变为新的精灵(它的尺寸正好是原来的75%),它改变它,但只显示新的精灵的象限,但与原始的尺寸。



我试图修改sprite.contentsize,然后再修饰sprite,但是所做的一切都是改变大小,但不影响图像的问题。



以下是一些视觉示例:



原创:








/ p>






将纹理图像复制到原始图像,并且contentSize未正确重置(oops,但这不是一个问题 - 我只是忘记了readd大小代码):





PS - 我有所有的-hd.png版本的我的精灵,所以我只是想添加,任何人想知道(图像和测试到目前为止只有在nonretina模拟器)。



谢谢!






编辑:在视网膜模拟器测试期间发生问题。

解决方案

由于您的主要关注点是内存,我建议只使用纹理图集。



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



缓存:

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

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



如果有超过一个sprite使用任何纹理,尝试和卸载/重新加载纹理将没有任何意义,因为会出现两个或多个精灵将使用任一纹理。



最后,我们最多谈论64 KB的内存。如果你关心内存使用情况和性能,你应该做的是加载PVR纹理,这是内存和磁盘上PNG文件大小的一小部分(但是失去一些颜色的活力)。

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


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:


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):

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.

解决方案

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.

这篇关于iOS和Cocos2d - 改变CCSprite的形象和新维度= FAIL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆