H:/0926/01-间隔动作_HelloWorldLayer.h
//
// HelloWorldLayer.h
// 01-间隔动作
//
// Created by apple on 13-9-26.
// Copyright itcast 2013年. All rights reserved.
//
#import <GameKit/GameKit.h>
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
// HelloWorldLayer
@interface HelloWorldLayer : CCLayer
// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
@end
H:/0926/01-间隔动作_HelloWorldLayer.m
// HelloWorldLayer.m
// 01-间隔动作
// Created by apple on 13-9-26.
// Copyright itcast 2013年. All rights reserved.
#import "HelloWorldLayer.h"
#import "AppDelegate.h"
@interface HelloWorldLayer()
{
CCSprite *_sprite;
}
@end
@implementation HelloWorldLayer
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
// 成员精灵
_sprite = [CCSprite spriteWithFile:@"grossini.png"];
_sprite.position = ccp(30, winSize.height * 0.5);
[self addChild:_sprite];
// 设置图层可以触摸
self.touchEnabled = YES;
}
return self;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// 延时执行动画
CCMoveBy *by1 = [CCMoveBy actionWithDuration:1 position:ccp(100, 0)];
CCDelayTime *delay = [CCDelayTime actionWithDuration:2];
[_sprite runAction:[CCSequence actions:by1, delay, by1, nil]];
}
#pragma mark 修改动画执行节奏
- (void)ease
{
CCScaleBy *move = [CCScaleBy actionWithDuration:2 scale:0.1];
// 由慢到快
// CCEaseIn *easeIn = [CCEaseIn actionWithAction:move rate:10];
// 由慢到快,再由快到慢
// CCEaseInOut *easeInOut = [CCEaseInOut actionWithAction:move rate:10];
// 开始的时候弹簧
CCEaseBounceIn *bounce = [CCEaseBounceIn actionWithAction:move];
//
//CCEaseBounceInOut *bounce = [CCEaseBounceInOut actionWithAction:move];
[_sprite runAction:bounce];
}
#pragma mark 淡入淡出、渐变
- (void)fadeAndTint
{
// fade tint 参数范围:0~255
CCFadeOut *fadeIn = [CCFadeOut actionWithDuration:2];
CCTintTo *red = [CCTintTo actionWithDuration:2 red:255 green:0 blue:0];
CCTintTo *blue = [CCTintTo actionWithDuration:2 red:0 green:0 blue:255];
CCTintTo *yellow = [CCTintTo actionWithDuration:2 red:255 green:255 blue:0];
[_sprite runAction:[CCSequence actions:red, blue, yellow, nil]];
// [_sprite runAction:[CCFadeTo actionWithDuration:2 opacity:125]];
}
#pragma mark 多个动画混合,CCSpawn,鱼产卵,大量产生,(同时)
- (void)mix
{
// 如果scale到0了,就无法再反转reverse回来,因为scaleBy里面是1/scale
CCScaleBy *scale = [CCScaleBy actionWithDuration:1 scale:0.01];
CCRotateBy *rotate = [CCRotateBy actionWithDuration:1 angle:360];
// 下面也是同时执行,但不专业
// [_sprite runAction:scale];
// [_sprite runAction:rotate];
// 通过两个动作,形成spawn动作
CCSpawn *spawn = [CCSpawn actions:scale, rotate, nil];
// 通过spawn 和spawn reverse反转,形成序列sequence动作
CCSequence *sequence = [CCSequence acti ons:spawn, [spawn reverse], nil];
// 通过序列动作,形成永远重复的动作
[_sprite runAction:[CCRepeatForever actionWithAction:sequence]];
}
#pragma mark 简单动画:扭曲、旋转、缩放
- (void)simple
{
// CGFloat x = 100;
// skew 歪斜,扭曲(倾斜)
// CCSkewTo *skew = [CCSkewTo actionWithDuration:1 skewX:x skewY:-x];
// angleX : 绕着x轴旋转
// angleY : 绕着y轴旋转
// CCRotateTo *rotate = [CCRotateTo actionWithDuration:1 angleX:180 angleY:0];
// 1 ~ -1
// CCScaleTo *scale = [CCScaleTo actionWithDuration:1 scaleX:1 scaleY:-1];
}
#pragma mark 跳跃
- (void)jump
{
CCJumpTo *jump = [CCJumpTo actionWithDuration:2 position:_sprite.position height:200 jumps:10];
// CCJumpBy *jump = [CCJumpBy actionWithDuration:2 position:ccp(100, 100) height:50 jumps:5];
// CCJumpBy *jump2 = [CCJumpBy actionWithDuration:2 position:ccp(-100, -100) height:50 jumps:5];
// By的动画,都可以reverse 动作翻转
[_sprite runAction:[CCSequence actionOne:jump two:[jump reverse]]];
}
@end
H:/0926/02-帧动画_HelloWorldLayer.h
//
// HelloWorldLayer.h
// 02-帧动画
//
// Created by apple on 13-9-26.
// Copyright itcast 2013年. All rights reserved.
//
#import <GameKit/GameKit.h>
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
// HelloWorldLayer
@interface HelloWorldLayer : CCLayer
// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
@end
H:/0926/02-帧动画_HelloWorldLayer.m
// HelloWorldLayer.m
// 02-帧动画
// Created by apple on 13-9-26.
// Copyright itcast 2013年. All rights reserved.
#import "HelloWorldLayer.h"
#import "AppDelegate.h"
@interface HelloWorldLayer()
{
CCSprite *_zy;
}
@end
@implementation HelloWorldLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
if( (self=[super init]) ) {
// 1.精灵帧缓存,加载纹理相册
//(这里传的TextPackage生成的plist文件名,里面是字典,包含所有帧图片信息)
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"zy.plist"];
CGSize winSize = [[CCDirector sharedDirector] winSize];
// 根据文件名取出对应的精灵帧CCSpriteFrame
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:@"9.png"];
// 通过精灵帧CCSpriteFrame,创建精灵
_zy = [CCSprite spriteWithSpriteFrame:frame];
_zy.position = ccp(winSize.width * 0.5, winSize.height * 0.5);
[self addChild:_zy];
// 图层可以被点击
self.touchEnabled = YES;
}
return self;
}
// 通过TexturePackage软件打包所有的帧图片,生成一个大图片,和plist字典
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// 数组中的元素是:CCSpriteFrame精灵帧
NSMutableArray *frames = [NSMutableArray array];
for (int i = 1; i<=10; i++) {
NSString *filename = [NSString stringWithFormat:@"%d.png", i];
// 根据图片文件名,创建精灵帧,CCSpriteFrame
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:filename];
// 添加帧
[frames addObject:frame];
}
// 1.用CCAnimation对象添加需要显示的图片(每隔0.1秒切换下一张图片)
// 通过精灵帧对象数组,创建CCAnimation
CCAnimation *animation = [CCAnimation animationWithSpriteFrames:frames
delay:0.1];
// 2.利用CCAnimate这个动作来执行CCAnimation中添加的图片
CCAnimate *anim = [CCAnimate actionWithAnimation:animation];
[_zy runAction:[CCRepeatForever actionWithAction:anim]];
}
- (void)animate
{
// 数组中的元素是:CCSpriteFrame精灵帧
NSMutableArray *frames = [NSMutableArray array];
for (int i = 1; i<=10; i++) {
NSString *filename = [NSString stringWithFormat:@"zy.bundle/%d.png", i];
// 加载纹理,通过图片的文件名
CCTexture2D *texure = [[CCTextureCache sharedTextureCache]
addImage:filename];
// 利用纹理(CCTexture2D)创建一个精灵帧,并指定rect
CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:texure
rect:(CGRect){CGPointZero, texure.contentSize}];
// 添加帧到精灵帧数组当中
[frames addObject:frame];
}
// 1.用CCAnimation对象添加需要显示的图片(每隔0.1秒切换下一张图片)
// 通过精灵帧对象数组,创建CCAnimation
CCAnimation *animation = [CCAnimation animationWithSpriteFrames:frames
delay:0.1];
// 2.利用CCAnimate这个动作来执行CCAnimation中添加的图片
CCAnimate *anim = [CCAnimate actionWithAnimation:animation];
[_zy runAction:[CCRepeatForever actionWithAction:anim]];
}
@end
H:/0926/03-纹理相册_HelloWorldLayer.h
//
// HelloWorldLayer.h
// 03-纹理相册
//
// Created by apple on 13-9-26.
// Copyright itcast 2013年. All rights reserved.
//
#import <GameKit/GameKit.h>
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
// HelloWorldLayer
@interface HelloWorldLayer : CCLayer <GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate>
{
}
// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
@end
H:/0926/03-纹理相册_HelloWorldLayer.m
// HelloWorldLayer.m
// 03-纹理相册
// Created by apple on 13-9-26.
// Copyright itcast 2013年. All rights reserved.
/*
1,
纹理 即图片
纹理相册,即使用软件texturePackage生成的大图片(pvr.ccz)+plist
其中,plist字典记录每张小图片在大图片的所有信息
2,
CCSpriteBatchNode 里面添加的是精灵,
而且是使用同一个纹理的精灵
因为,一个CCSpriteBatchNode只能管理一个纹理
3,
CCSpriteFrameCache 里面添加的是plist,关联的是一个大纹理
保存的是一个个精灵帧
精灵帧:就是一个大纹理+plist--->记录了的每个小图片
4,使用用BatchNode管理使用同一纹理的精灵的好处是:
一个BatchNode只会渲染一次,效率极高
*/
#import "HelloWorldLayer.h"
#import "AppDelegate.h"
@implementation HelloWorldLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// spriteWithFile: 到项目中加载文件名对应的图片(效率低,不是从帧缓存中取图片)
// spriteFrameByName: 去帧缓存中根据帧名取出对应的帧(CCSpriteFrame)
-(id) init
{
if( (self=[super init]) ) {
// 1.加载纹理相册,根据texturePackage软件生成的plist文件,
// 添加所有的精灵帧,到精灵帧缓存里面
[[CCSpriteFrameCache sharedSpriteFrameCache]
addSpriteFramesWithFile:@"my.plist"];
// 2.spriteWithFile,直接去工程中找该名称的图片,效率低
CCSprite *sprite = [CCSprite spriteWithFile:@"my.png"];
// 3.spriteFrameByName,从帧缓存中取出精灵,效率奇高!
CCSprite *sprite = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache
sharedSpriteFrameCache] spriteFrameByName:@"grossini.png"]];
// 4.spriteWithSpriteFrameName,从帧缓存中取出精灵,效率奇高!
CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"bullet.png"];
sprite.position = ccp(200, 200);
[self addChild:sprite];
}
return self;
}
- (void) dealloc
{
[super dealloc];
}
#pragma mark GameKit delegate
-(void) achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
{
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[app navController] dismissModalViewControllerAnimated:YES];
}
-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[app navController] dismissModalViewControllerAnimated:YES];
}
@end
H:/0926/04-CCProgressTo_HelloWorldLayer.h
//
// HelloWorldLayer.h
// 04-CCProgressTo
//
// Created by apple on 13-9-26.
// Copyright itcast 2013年. All rights reserved.
//
#import <GameKit/GameKit.h>
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
// HelloWorldLayer
@interface HelloWorldLayer : CCLayer <GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate>
{
}
// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
@end
H:/0926/04-CCProgressTo_HelloWorldLayer.m
// HelloWorldLayer.m
// 04-CCProgressTo
// Created by apple on 13-9-26.
// Copyright itcast 2013年. All rights reserved.
#import "HelloWorldLayer.h"
#import "AppDelegate.h"
@interface HelloWorldLayer()
{
CCProgressTimer *_timer;
}
@end
@implementation HelloWorldLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init
{
if( (self=[super init]) ) {
CGSize winSize = [CCDirector sharedDirector].winSize;
// 垫在进度条下面的文字精灵
CCLabelTTF *bgLabel = [CCLabelTTF labelWithString:@"Loading"
fontName:@"Marker Felt" fontSize:60];
bgLabel.position = ccp(winSize.width * 0.5, winSize.height * 0.5);
[self addChild:bgLabel];
// 用来显示进度条的精灵
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Loading"
fontName:@"Marker Felt" fontSize:60];
label.color = ccc3(255, 0, 0);
// 进度条
_timer = [CCProgressTimer progressWithSprite:label];
_timer.position = bgLabel.position;
_timer.type = kCCProgressTimerTypeBar;
// midpoint为进度条中心点的位置
_timer.midpoint = ccp(0, 0.5);
// 设置进度值改变的速率,ccp(1, 0)代表只在X方向增加
_timer.barChangeRate = ccp(1, 0);
// percentage百分比,进度值 0~100,放在最后面调整
_timer.percentage = 10;
[self addChild:_timer];
self.touchEnabled = YES;
}
return self;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CCProgressFromTo *fromTo = [CCProgressFromTo actionWithDuration:2
from:0 to:100];
// CCProgressTo *to = [CCProgressTo actionWithDuration:2 percent:80];
[_timer runAction:fromTo];
}
- (void)progress
{
CGSize winSize = [CCDirector sharedDirector].winSize;
CCSprite *sprite = [CCSprite spriteWithFile:@"progress.png"];
// 进度条
_timer = [CCProgressTimer progressWithSprite:sprite];
_timer.position = ccp(winSize.width * 0.5, winSize.height * 0.5);
// percentage百分比,进度值 0~100,放在最后面调整
_timer.percentage = 0;
// 雷达形状
// _timer.type = kCCProgressTimerTypeRadial;
// midpoint为进度条中心点的位置
// _timer.midpoint = ccp(0.75, 0.25);
_timer.type = kCCProgressTimerTypeBar;
// 设置进度值改变的速率,ccp(1, 0)代表只在X方向增加
_timer.barChangeRate = ccp(1, 0);
// 在条状的样式下,midpoint,设置进度开始的起始点坐标~
_timer.midpoint = ccp(0, 0);
[self addChild:_timer];
}
- (void)update:(ccTime)delta
{
// percentage百分比,进度值 0~100,放在最后面调整
_timer.percentage += 0.5;
}
#pragma mark GameKit delegate
-(void) achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
{
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[app navController] dismissModalViewControllerAnimated:YES];
}
-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[app navController] dismissModalViewControllerAnimated:YES];
}
@end
H:/0926/05-瞬时动作_HelloWorldLayer.h
//
// HelloWorldLayer.h
// 05-瞬时动作
//
// Created by apple on 13-9-26.
// Copyright itcast 2013年. All rights reserved.
//
#import <GameKit/GameKit.h>
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
// HelloWorldLayer
@interface HelloWorldLayer : CCLayer <GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate>
{
}
// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
@end
H:/0926/05-瞬时动作_HelloWorldLayer.m
// HelloWorldLayer.m
// 05-瞬时动作,联合使用时威力巨大
// Created by apple on 13-9-26.
// Copyright itcast 2013年. All rights reserved.
#import "HelloWorldLayer.h"
#import "AppDelegate.h"
@implementation HelloWorldLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init
{
if( (self=[super init]) ) {
// CCSprite *bg = [CCSprite spriteWithFile:@"Default.png"];
// bg.contentSize = [CCDirector sharedDirector].winSize;
// bg.position = ccp(bg.contentSize.width * 0.5, bg.contentSize.height * 0.5);
// [self addChild:bg];
CCSprite *s = [CCSprite spriteWithFile:@"Icon.png"];
s.position = ccp(100, 200);
s.tag = 10;
[self addChild:s];
CCSprite *s2 = [CCSprite spriteWithFile:@"Icon.png"];
s2.position = ccp(300, 200);
s2.tag = 20;
[self addChild:s2];
// 设置图层可点击
self.touchEnabled = YES;
}
return self;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CCSprite *s1 = (CCSprite *)[self getChildByTag:10];
CCSprite *s2 = (CCSprite *)[self getChildByTag:20];
CCJumpBy *jump = [CCJumpBy actionWithDuration:1 position:CGPointZero height:50 jumps:4];
CCRotateBy *rotate = [CCRotateBy actionWithDuration:1 angle:360];
// 可以指挥另一个节点执行一个动作
CCTargetedAction *target = [CCTargetedAction actionWithTarget:s1 action:jump];
CCTargetedAction *target2 = [CCTargetedAction actionWithTarget:s1 action:rotate];
CCTargetedAction *target3 = [CCTargetedAction actionWithTarget:s1 action:[rotate reverse]];
[s2 runAction:[CCRepeatForever actionWithAction:[CCSequence actions:jump, target, rotate, target2, [rotate reverse], target3,nil]]];
}
- (void)follow
{
CCSprite *s = (CCSprite *)[self getChildByTag:10];
CCMoveBy *move = [CCMoveBy actionWithDuration:1 position:ccp(600, 0)];
[s runAction:move];
// 在 700 x 320 范围内都能跟踪
CCFollow *follow = [CCFollow actionWithTarget:s worldBoundary:CGRectMake(0, 0, 700, 320)];
[self runAction:follow];
}
- (void)speed
{
CCSprite *s = (CCSprite *)[self getChildByTag:10];
CCMoveBy *move = [CCMoveBy actionWithDuration:1 position:ccp(300, 100)];
// 调整动作执行的速度
CCSpeed *speed = [CCSpeed actionWithAction:move speed:0.5];
[s runAction:speed];
}
- (void)callBlock
{
CCSprite *s = (CCSprite *)[self getChildByTag:10];
CCTintTo *red = [CCTintTo actionWithDuration:1 red:255 green:0 blue:0];
CCTintTo *green = [CCTintTo actionWithDuration:1 red:0 green:255 blue:0];
CCTintTo *blue = [CCTintTo actionWithDuration:1 red:0 green:0 blue:255];
// CCCallBlock没有参数
CCCallBlock *redBlock = [CCCallBlock actionWithBlock:^{
CCLOG(@"变为了红色");
}];
// CCCallBlockN有1个参数,是当前Node结点对象
CCCallBlockN *greenBlock = [CCCallBlockN actionWithBlock:^(CCNode *node) {
node.rotation = 45;
CCLOG(@"变为了绿色");
}];
// CCCallBlockO有1个参数,是方法的第2个参数传递的object
CCCallBlockO *blueBlock = [CCCallBlockO actionWithBlock:^(id object) {
CCLOG(@"变为了蓝色 -- %@", object);
} object:@"test"];
[s runAction:[CCSequence actions:red, redBlock, green, greenBlock, blue, blueBlock, nil]];
}
- (void)callFunc
{
CCSprite *s = (CCSprite *)[self getChildByTag:10];
CCTintTo *red = [CCTintTo actionWithDuration:1 red:255 green:0 blue:0];
CCTintTo *green = [CCTintTo actionWithDuration:1 red:0 green:255 blue:0];
CCTintTo *blue = [CCTintTo actionWithDuration:1 red:0 green:0 blue:255];
// CCCallFunc *func = [CCCallFunc actionWithTarget:self selector:@selector(tintFinish)];
// CCCallFuncN会传递一个节点(Node)参数(正在运行当前动作的节点)
CCCallFuncN *redFunc = [CCCallFuncN actionWithTarget:self
selector:@selector(turnRed:)];
// CCCallFuncO传递一个OC对象(Object),即方法中的第3个参数(object后面的值)
CCCallFuncO *greenFunc = [CCCallFuncO actionWithTarget:self
selector:@selector(turnGreen:) object:s];
// CCCallFuncND传递2个参数,当前结点Node,第2个参数是方法的第3个参数data
CCCallFuncND *blueFunc = [CCCallFuncND actionWithTarget:self
selector:@selector(turnBlue:data:) data:@"5267"];
[s runAction:[CCSequence actions:red, redFunc, green, greenFunc, blue, blueFunc, nil]];
}
- (void)turnBlue:(CCSprite *)sprite data:(void *)data
{
CCLOG(@"----改为了蓝色 -- %@", data);
}
- (void)turnGreen:(CCSprite *)sprite
{
sprite.rotation = 45;
CCLOG(@"----改为了绿色");
}
- (void)turnRed:(CCSprite *)sprite
{
sprite.scale = 1.5;
CCLOG(@"----改为了红色");
}
- (void)move
{
CCSprite *s = (CCSprite *)[self getChildByTag:10];
CCMoveBy *move = [CCMoveBy actionWithDuration:1 position:ccp(100, 100)];
// CCFlipX *flipX = [CCFlipX actionWithFlipX:YES];
// CCHide *hide = [CCHide action];
// CCCallFunc,运行该动画时,执行绑定的方法,但最大的缺点,不能传参数
CCCallFunc *func = [CCCallFunc actionWithTarget:self selector:@selector(moveFinish)];
[s runAction:[CCSequence actions:move, func, nil]];
}
- (void)moveFinish
{
CCLOG(@"----移动完毕");
}
- (void) dealloc
{
[super dealloc];
}
#pragma mark GameKit delegate
-(void) achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
{
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[app navController] dismissModalViewControllerAnimated:YES];
}
-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[app navController] dismissModalViewControllerAnimated:YES];
}
@end
H:/0926/06-CCSpriteBatchNode_HelloWorldLayer.h
//
// HelloWorldLayer.h
// 06-CCSpriteBatchNode
//
// Created by apple on 13-9-26.
// Copyright itcast 2013年. All rights reserved.
//
#import <GameKit/GameKit.h>
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
// HelloWorldLayer
@interface HelloWorldLayer : CCLayer
+(CCScene *) scene;
@end
H:/0926/06-CCSpriteBatchNode_HelloWorldLayer.m
// HelloWorldLayer.m
// 06-CCSpriteBatchNode
// Created by apple on 13-9-26.
/*
1,
纹理 即图片
纹理相册,使用软件texturePackage生成的大图片(pvr.ccz)+plist
其中,plist字典记录每张小图片在大图片的所有信息
2,
CCSpriteBatchNode 里面添加的是精灵,
而且是使用同一个纹理的精灵
因为,一个CCSpriteBatchNode只能管理一个纹理
3,
CCSpriteFrameCache 里面添加的是plist,关联的是一个大纹理
保存的是一个个精灵帧
精灵帧:就是一个大纹理+plist--->记录了的每个小图片
4,使用用BatchNode管理使用同一纹理的精灵的好处是:
一个BatchNode只会渲染一次,效率极高
*/
// Import the interfaces
#import "HelloWorldLayer.h"
// Needed to obtain the Navigation Controller
#import "AppDelegate.h"
@interface HelloWorldLayer()
{
CCSpriteBatchNode *_batch;
CCSpriteBatchNode *_batch2;
}
@end
#pragma mark - HelloWorldLayer
// HelloWorldLayer implementation
@implementation HelloWorldLayer
// Helper class method that creates a Scene with the HelloWorldLayer as the only child.
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
self.touchEnabled = YES;
// 加载纹理相册
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"my.plist"];
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"my.pvr.ccz"];
_batch = [CCSpriteBatchNode batchNodeWithTexture:texture];
// 创建一个精灵批处理,一个批处理,只能添加使用相同纹理图片(包括符合图片)的那些精灵
// _batch = [CCSpriteBatchNode batchNodeWithFile:@"my.pvr.ccz"];
[self addChild:_batch];
// 一个CCSpriteBatchNode对象只能处理一个纹理对象(添加到batch中的子节点必须和batch使用同一个纹理对象)
_batch2 = [CCSpriteBatchNode batchNodeWithFile:@"grossini.png"];
[self addChild:_batch2];
// 创建一个精灵批处理,一个批处理,只能添加使用相同纹理图片的那些精灵
// _batch = [CCSpriteBatchNode batchNodeWithFile:@"bullet.png"];
// CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"bullet.png"];
// _batch = [CCSpriteBatchNode batchNodeWithTexture:texture];
// [self addChild:_batch];
//
// CCSprite *s = [CCSprite spriteWithFile:@"Icon.png"];
// [self addChild:s];
}
return self;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[CCDirector sharedDirector] convertTouchToGL:[touches anyObject]];
CCSprite *s = [CCSprite spriteWithFile:@"grossini.png"];
s.position = p;
// 一个精灵批处理,只能添加使用相同纹理图片(包括CCZ大图)的那些精灵
[_batch2 addChild:s];
CCSprite *s3 = [CCSprite spriteWithSpriteFrameName:@"grossini.png"];
s3.position = ccpAdd(p, ccp(0, 50));
[_batch addChild:s3];
CCSprite *s2 = [CCSprite spriteWithSpriteFrameName:@"bullet.png"];
s2.position = ccpAdd(p, ccp(20, 0));
[_batch addChild:s2];
}
@end