I recently learned how to develop games on the iphone. I think it is not that difficult to learn a tool. I would like to share some experience in image loading with beginners. I hope everyone can quickly become a master.
Currently, there are many formats for images in cocos2d development for iphone, but I only use two of the most commonly used formats here, one of which is "*. png "format, the other is ". pvr format, and compressed pvr format. pvr. ccz. Pvr is the image format used by the graphics processor in the iphone, so the advantage of using this format is that the reading and display speed is very fast (as you may say on the Internet ).
The question below is how to load images in cocos2d:
First, let's talk about how to create a single image. For example, if you have an image of background.png in your hand, the image should not be too big. If you are an iphone, you will not need it.
We can use the following methods to load normally:
Add this image to your project file before export background.png. Then use the following code:
CCSprite * bg = [CCSprite spriteWithFile: @ "background.png"];
[Bg setPosition: ccp (160,240)];
[Self addChild: bg z: 0];
For loading. for an image in pvr format, you only need to replace the name in the above Code. you just need to add the pvr image to the project file.
The second case is that we often see what someone said, using tools to load all the sprite images used in the game into the memory at a time, and adding the sprite table file. plist method. In this case, how can we easily use the various genie in this big picture in all classes after adding the genie table.
The two codes can be put there, and I put them in the proxy object loaded by the program. Then, we will load the big image into the cache in the initialization of the first CCScene class and add it as a child of the scenario. The Code is as follows:
CCSpriteBatchNode * batch _ = [CCSpriteBatchNode batchNodeWithFile: @ "boom.png"];
// Add _ pvr after the name of the pvr image. Otherwise, an error occurs. I don't know why.
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: @ "boom. plist"];
[Self addChild: batch _];
The second method in the above Code is to load the big picture elf table. In this table, there are various genie positions and sizes in the big picture.
After loading a large image, you only need to use the following method to use our image. If you want to load the genie in another class, you only need to use the image name in the genie table, if the loaded image is not in the larger image, an error is reported.
CCSprite * bg = [CCSprite spriteWithSpriteFrameName: @ "sprite_1.png"];
[Bg setPosition: ccp (160,240)]; // sets the coordinates after loading by the genie.
[Self addChild: bg z: 0]; // Add the bg image to the scenario
Another problem during image loading is how cocos2d processes images of different resolutions. In xcode, if you want to use HD images and plain images