1. The UIKit coordinate system has the origin (0,0) at the upper left of the screen. How- ever, Cocos2D uses the OpenGL ES coordinate system, which places the origin (0,0) at the lower left of the screen.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
vikingSprite.scaleX = screenSize.width/1024.0f;
vikingSprite.scaleY = screenSize.height/768.0f;
}
2. 根据屏幕的大小,对vikingSprite进行缩放
3. In Cocos2D the z value determines the order in which your CCNodes (sprites, layers, etc.) will be drawn.
[self scheduleUpdate];
5. call to the Cocos2D scheduler to call the update method every time this layer is set to be rendered on the screen. That means the update method was called before every frame of Space Viking is to be rendered.
-(void) update:(ccTime)deltaTime
{
[self applyJoystick:leftJoystick toNode:vikingSprite forTimeDelta:deltaTime];
}
7. tag
#define kVikingSprite 10
[self addChild:tempSprite z:0 tag:kVikingSprite];
CCSprite *tempSprite = [self getChildByTag:kVikingSprite];
Terminology Review
2. Texture
The image file of your character has to be decompressed and possibly converted into a format that the iPhone and iPad GPU can understand and loaded into RAM before it can be used. The loaded image in RAM is referred to as a texture.This is what OpenGL ES draws on the screen.
3. Texture Atlas or Sprite Sheet
In order to save on memory and reduce the amount of wasted empty space in your textures, you want to combine them into one larger texture, called a texture atlas. The texture atlas is simply a large texture, containing your images, from which smaller textures for each of your images can be cut or extracted from. One of the keys to get- ting more graphical performance is handing OpenGL ES as few textures as you can in one pass by having a lot of textures combined into the texture atlas. The key is in batching the draw calls and reducing the number of textures OpenGL ES binds to, both accomplished by using a texture atlas in Cocos2D via theCCSpriteBatchNode.
Texture Atlas
1. For each sprite, OpenGL ES has to bind to the texture for that CCSprite and then draw (also called rendering) the sprite onscreen.
2. Your game will run faster if you do all of your rendering work with the least number of OpenGL ES calls.
3. The CCSpriteBatchNode is a special class in Cocos2D that can act as the parent to a host of CCSprites. It sits between the CCLayer and the CCSprites. All of the draw calls for the CCSprites are batched and done byCCSpriteBatchNode at once.
4. The large performance savings is not from just batching the calls together but from combining all of the little textures into one large texture that OpenGL ES has to bind just once.
5. Top Reasons to Use a Texture Atlas/Sprite Sheet
(1)Reduced OpenGL ES bind calls—the more images contained in the texture atlas, the greater the reduction.
(2)Reduced memory footprint for the images stored as textures in memory.
(3)Easy method to trim and save on transparent space in your images, allowing for more images/texture in the same space.
(4) Zwoptex and TexturePacker are fully supported by Cocos2D, so creating and using texture atlases is painless.
6. CCSpriteBatchNode
CCSpriteBatchNode *chapter2SpriteBatchNode;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"scene1atlas.plist"];
chapter2SpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"scene1atlas.png"];
} else {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"scene1atlasiPhone.plist"];
chapter2SpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"scene1atlasiPhone.png"];
}
vikingSprite = [CCSprite spriteWithSpriteFrameName:@"sv_anim_1.png"];
vikingSprite.position = ccp(screenSize.width/2, screenSize.height*0.17f);
[chapter2SpriteBatchNode addChild:vikingSprite];
[self addChild:chapter2SpriteBatchNode];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
vikingSprite.scaleX = screenSize.width/1024.0f;
vikingSprite.scaleY = screenSize.height/768.0f;
}
8. The steps required to use a texture atlas in Cocos2D are loading the sprite frames into the cache and creating a CCSpriteBatchNode with the texture atlas image.
9. CCSprite and CCSpriteFrame 的区别
CCSprite 是在界面上可以移动的图片,可以用spriteWithFile:创建,或者spriteWithSpriteFrameName:通过指定Frame的名称,从texture atlas中提取。