Cocos2D (iv) ---- CCSprite, cocos2dccsprite

Source: Internet
Author: User

Cocos2D (iv) ---- CCSprite, cocos2dccsprite

Before introducing CCSprite, you must first understand a core concept in Game Development: genie. The genie is also called a game object. It can be used to represent any object in the game, such as an enemy, bullet, or even a background image or text. CCSprite is the most commonly used class in cocos2d. It can be called the "Genie class". It can display the genie on the screen as an image.


Create genie

Before talking about how to create an genie, let's take a look at another core concept in Game Development: texture. Texture is actually a graph used to describe the details of an object's surface. It is also called a texture map. It maps a texture to an object's surface in a specific way to make the object look more authentic. Map the texture to the screen to display the corresponding image.


1. the simplest way to create an genie is to specify an image file for the Genie. cocos2d uses this image to generate a texture object (CCTexture2D) and load it to the texture cache Library (CCTextureCache ), in the end, the genie controls the position of the texture displayed on the screen.

[Java]View plaincopy
  1. // Add the genie to the Layer
  2. -(Id) init {
  3. If (self = [super init]) {
  4. CCSprite * sprite = [CCSprite spriteWithFile: @ "lufy.png"];
  5. CGSize winSize = [CCDirector shareddire]. winSize;
  6. Sprite. position = ccp (winSize. width * 0.5f, winSize. height * 0.5f );
  7. [Self addChild: sprite];
  8. }
  9. Return self;
  10. }
To ensure normal reading of image resources, put your image files in the Resource folder.

Running effect:


Other common settings

1> set the opacity of the genie

[Java]View plaincopy
  1. // Approximately translucent
  2. Sprite. opacity = 125;

The opacity value ranges from 0 to 255, indicating that the opacity is completely transparent. The value indicates that the opacity is completely opaque.



2> color the genie

[Java]View plaincopy
  1. // Red
  2. Sprite. color = ccc3( 255, 0, 0 );



3> horizontal Image

[Java]View plaincopy
  1. Sprite. flipX = YES;


4> vertical image

[Java]View plaincopy
  1. Sprite. flipY = YES;


If flipX and flipY are used together

[Java]View plaincopy
  1. Sprite. flipX = YES;
  2. Sprite. flipY = YES;


2. You can specify a range to load only a rectangular area of the image.

The original size of this image is X.

[Java]View plaincopy
  1. CCSprite * sprite = [CCSprite spriteWithFile: @ "lufy.png" rect: CGRectMake (0, 0, 50, 50)];

Only the 50x50 area in the upper left corner of the image is loaded.


3. You can directly input a texture object (CCTexture2D)

[Java]View plaincopy
  1. // Create a texture object
  2. CCTexture2D * texture = [[CCTextureCache sharedTextureCache] addImage: @ "lufy.png"];
  3. // Input the texture object to generate the genie
  4. CCSprite * sprite = [CCSprite spriteWithTexture: texture];

CCTextureCache is used to cache CCTexture2D objects. It contains an NSMutableDictionary * textures _ dictionary, the key is the image name, And the vale is the CCTexture2D object. When you call its addImage: Method to add an image, the system first searches for the corresponding CCTexture2D object in the dictionary based on the image name. If yes, the system returns the result directly. If no, the CCTexture2D object will be loaded according to the image name. After loading, the CCTexture2D object will be placed into the dictionary.

Or you can specify a range to load only a rectangular area of the image.

[Java]View plaincopy
  1. CCSprite * sprite = [CCSprite spriteWithTexture: texture rect: CGRectMake (0, 0, 50, 50)];

Texture size

Currently, iOS devices only support "2 n power" textures, so the width and height of each texture can only be: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, and 2048 pixels. That is to say, the width and height of the texture can only be a combination of the preceding values, such as 2x4, 32x32,512 x64.

When we make a texture image, the best image size meets the above requirements. Otherwise, there are many disadvantages.

For example, if there is a 32-bit color map of 260x260 pixels, it should have approximately 260 KB in memory (260x32/8 x = 270400B), but it is limited by the texture size, the texture size must be 2 to n power, and the system will automatically generate a size closest to the source image (but not smaller than the original image) the width and height are all 2 n-power textures, so the system will generate a 512 x512 pixel texture, so it will eventually occupy 1 MB of memory (512x32/8 x = 1048576B ), the actual memory used is four times the required memory. The solution is to change the 260x260 pixel image to 256x256 pixel, and the system will also generate a texture of 256x256 pixel.


HD and SD Images

The pixel differentiation rate of iOS varies with the hardware of the device. For the first time, the iPhone introduced a high-resolution Retina screen (Retina Display Screen) with a pixel resolution of 960x640, this is exactly twice the pixel resolution of the previous iPod and iPhone (480x320. Generally, the image used by the Retina display screen is called a high-resolution (HD) image, and the image used by a non-Retina display screen is called a standard resolution (SD) image.

Next let's take a look at the specification parameters of each generation of iOS devices.

Device Maximum texture size Pixel resolution Coordinate System
IPhone 2G \ 3G \ 3GS, iPod Touch
1024x1024
480x320 480x320
IPhone4 and iPhone4s 2048x2048
960x640 480x320
IPad, iPad2 2048x2048 1024x768 1024x768
Although the pixel resolutions of these devices are different, cocos2d's coordinate system, like UIKit, is not related to pixels. It uses the coordinate system, not the pixel coordinate system, that is, the unit is vertex, not pixel. On the device that displays the Retina screen, the value 1 is 2 pixels. On a device that does not display the screen, the value 1 is 1 pixel. The coordinates are the same on the two devices.

If the game is running on a device with a Retina display screen, cocos2d first tries to load an image with a-hd suffix. . Therefore, in order to better support the Retina display, we usually use HD resolution to create all the images, then reduce the width and height by 50%, and save them as SD-resolution images.

However, to enable cocos2d to automatically load HD images, you also need to enable support for the Retina display screen.

[Java]View plaincopy
  1. [[CCDirector shareddire] enableRetinaDisplay: YES];

CCLabelTTF

CCLabelTTF inherits from CCSprite and can be used to display text

[Java]View plaincopy
  1. CCLabelTTF * label = [CCLabelTTF labelWithString: @ "Hello World" fontName: @ "Courier New" fontSize: 20];
  2. Label. position = ccp (winSize. width * 0.5f, winSize. height * 0.5f );
  3. [Self addChild: label];

You can use the color attribute to set the text color.

[Java]View plaincopy
  1. // Red
  2. Label. color = ccc3( 255, 0, 0 );



Original article address: http://blog.csdn.net/q199?#q/article/details/860%3thank you ~!
What is the role of the opacity attribute of CCSprite in cocos2d?

The greater the value, the more colors sprite.
 
Cocos2d create CCSprite

It can be written in this way.

-(Id) init
{
If (self = [super init])

{
CCSprite * sprite = [CCSprite spriteWithFile: @ "Icon.png"];
[Self addChild: sprite];
}
Return self;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.