cocos2d从sprite sheet加载随机图像 [英] cocos2d load random image from sprite sheet

查看:204
本文介绍了cocos2d从sprite sheet加载随机图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我的概念化的游戏,我想做,并玩了一点,但遇到一个问题。我正在学习iOS编程和学习cocos2d。



我有大约20张图片。他们都是相同的大小64x64。基本上,它们被分组为5种不同的形状(正方形,圆形等),每种形状有5种不同的颜色。我想要能够做的,是随机放在屏幕上的肖像。每列只有一个下降。在屏幕上可以有重复,我真的想要的。虽然它是随机的,我想控制一种新的颜色的介绍。



我一直在努力解决我的大脑,找出如何做到这一点。正如我所说,我是新的,所以任何帮助将是真正伟大的。



如果我可以这么大胆,可以任何答案的格式解释

解决方案

当使用Cocos2D的最好的方法使用sprite是与Sprite Sheets。也就是说,两种类型的文件的组合:


  1. 一个plist文件,其中包含N个sprites的所有坐标, ,大小,名称等。

  2. 一个包含所有精灵的png文件

make a sprite sheet with different types of software,like:




  • Zwoptex

  • Sprite Helper

  • Sprite Packer

  • Sprite Master

  • Texture Packer

$关于性能,当使用精灵表,你将需要使用CCSpriteBatchNode,如API文档所述(阅读这里):


CCSpriteBatchNode 可以引用一个且仅一个纹理(一个图像文件,一个纹理地图集)。只有包含在该纹理中的CCSprites可以被添加到CCSpriteBatchNode。添加到CCSpriteBatchNode的所有CCSprites都绘制在一个OpenGL ES绘图调用中。 如果CCSprites没有添加到CCSpriteBatchNode,那么每个都需要一个OpenGL ES绘图调用,效率较低。


使用Sprite Sheet的代码:

  [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@sprite.plist ]; 

CCSpriteBatchNode * spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@sprite.png];
[self addChild:spriteSheet];

希望这有帮助。


Ok so I am conceptualizing for a game I want to make and have played a little bit but ran in to a problem. I am learning iOS programming and learning cocos2d also.

I have about 20 images. They are all the same size 64x64. Basically they are grouped as 5 different shapes (square, circle, etc.) each in 5 different colors. What I want to be able to do, is randomly drop them down the screen in portrait. There will only ever be one per column falling. There can be duplicates across the screen, I actually want that. While it is random, I want to control the introduction of a new color.

I have been really racking my brain to figure out how to do this. As I said, I am pretty new to this so any help would be really great.

If I could be so bold, can any answers be in a format to "Explain it like I'm 5 I have only been doing this for about 1 year"

解决方案

When using Cocos2D the best way to use sprites is with Sprite Sheets. That is, a combination of two type of files:

  1. A plist file with all the coordinates of N sprites and all different data about the sprites, size, name, etc.
  2. A png file with all the sprites

You can make a sprite sheet with different types of software, like:

  • Zwoptex
  • Sprite Helper
  • Sprite Packer
  • Sprite Master
  • Texture Packer

About performance, when using a sprite sheet you will need to use CCSpriteBatchNode, as the API documentation states (read here):

A CCSpriteBatchNode can reference one and only one texture (one image file, one texture atlas). Only the CCSprites that are contained in that texture can be added to the CCSpriteBatchNode. All CCSprites added to a CCSpriteBatchNode are drawn in one OpenGL ES draw call. If the CCSprites are not added to a CCSpriteBatchNode then an OpenGL ES draw call will be needed for each one, which is less efficient.

THe code to use Sprite Sheet:

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

CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"sprite.png"];
[self addChild:spriteSheet];

Hope this helps.

这篇关于cocos2d从sprite sheet加载随机图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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