您的位置:首页 > 移动开发 > Cocos引擎

IOS_Cocos2D_label_菜单_粒子系统_图层_飞机

2014-06-03 22:36 330 查看
H:/0928/01-Label_HelloWorldLayer.h
//
//  HelloWorldLayer.h
//  01-Label
//
//  Created by apple on 13-9-28.
//  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:/0928/01-Label_HelloWorldLayer.m
//  HelloWorldLayer.m
//  01-Label
//  Created by apple on 13-9-28.
//  Copyright itcast 2013年. All rights reserved.
#import "HelloWorldLayer.h"
#import "AppDelegate.h"
@interface HelloWorldLayer()
{
/* 1,	CCLabelTTF
可以随时重新设置显示的内容,但是非常消耗性能
因为每次更改内容时,重新生成纹理,进行渲染
2,	CCLabelBMFont
当频繁更新文本内容的时候,建议使用CCLabelBMFont
因为它是通过占用内容来提升CUP性能
每个字符都是一个精灵,有对应的纹理缓存
从而,更新文本的时候,不会重新创建纹理
使用工具:Glyth designer可以创建fnt字体
优点2:可以对每个字符进行动画
3,	CCLabelAtlas 大图里面的字符:按顺序,尺寸相同
atlas意思是地图集
每个字符也是图片,但不是精灵.
而是整个png图片才是一个纹理
参数1:charMapFile为大的png图片(里面整齐排列着每一个连续的ASCII值)
参数2:itemWidth为里面每个小字符的宽
参数3:itemHeight为里面每个小字符的高
参数4:startCharMap为里面开始字符的ascii值

*/
CCLabelTTF *_label1;
CCLabelBMFont *_bm;
CCLabelAtlas *_atlas;
}
@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]) ) {
[self bmFont];
// [self scheduleUpdate];
}
return self;
}
- (void)atlas
{
_atlas = [CCLabelAtlas labelWithString:@"100.89"
charMapFile:@"fps_images.png" itemWidth:16
itemHeight:32 startCharMap:'.'];
_atlas = [CCLabelAtlas labelWithString:@"100.89"
fntFile:@"fps_images.plist"];
_atlas.position = ccp(240, 160);
// 只有精灵锚点才是默认0.5 0.5,其他是0 0
_atlas.anchorPoint = ccp(0.5, 0.5);
[self addChild:_atlas];
}
- (void)bmFont
{
_bm = [CCLabelBMFont labelWithString:@"Game Over"
fntFile:@"testfont.fnt" width:250 alignment:kCCTextAlignmentCenter];
_bm.position = ccp(240, 160);
_bm.scale = 0.5;
[self addChild:_bm];
self.touchEnabled = YES;
// [self scheduleUpdate];
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// CCLabelBMFont里面的每个字符都是一个精灵
// CCLabelBMFont的children是CCArray不能使用[]快速访问
CCSprite *first = [_bm.children objectAtIndex:0];
CCJumpBy *jump = [CCJumpBy actionWithDuration:0.5 position:CGPointZero height:40 jumps:2];
CCDelayTime *delay = [CCDelayTime actionWithDuration:1];
CCSequence *sequence = [CCSequence actionOne:jump two:delay];
[first runAction:[CCRepeatForever actionWithAction:sequence]];
CCSprite *five = [_bm.children objectAtIndex:5];
[five runAction:[CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration:1 angle:360]]];
}
- (void)ttf
{
//        _label1 = [CCLabelTTF labelWithString:@"I am a hello hello hello hello hello hello am is test" fontName:@"Marker Felt" fontSize:50 dimensions:[CCDirector sharedDirector].winSize hAlignment:kCCTextAlignmentLeft vAlignment:kCCVerticalTextAlignmentTop lineBreakMode:kCCLineBreakModeWordWrap];
_label1 = [CCLabelTTF labelWithString:@"100000" fontName:@"Marker Felt" fontSize:50];
_label1.position = ccp(240, 160);
[self addChild:_label1];
[self scheduleUpdate];
}
- (void)update:(ccTime)delta
{
_atlas.string = [NSString stringWithFormat:@"%.1f", CCRANDOM_0_1() * 60];
// _bm.string = [NSString stringWithFormat:@"%f %c", CCRANDOM_MINUS1_1() * 1000, 'a' + (int)(CCRANDOM_0_1() * 26)];
}
@end

H:/0928/02-菜单_HelloWorldLayer.h
//
//  HelloWorldLayer.h
//  02-菜单
//
//  Created by apple on 13-9-28.
//  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:/0928/02-菜单_HelloWorldLayer.m
//  HelloWorldLayer.m
//  02-菜单
//  Created by apple on 13-9-28.
//  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]) ) {
// 1.菜单项
// 文本菜单项
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Setting" fontName:@"Marker Felt" fontSize:30];
label.color = ccRED;
CCMenuItemLabel *item1 = [CCMenuItemLabel itemWithLabel:label block:^(id sender) {
CCLOG(@"点击了Setting");
}];

// 文本菜单项2
[CCMenuItemFont setFontName:@"Courier New"];
[CCMenuItemFont setFontSize:40];
CCMenuItemFont *item2 = [CCMenuItemFont itemWithString:@"Config" block:^(id sender) {
CCLOG(@"点击了Config");
}];

// 文本菜单项3
//        CCMenuItemAtlasFont *item3 = [CCMenuItemAtlasFont itemWithString:<#(NSString *)#> charMapFile:<#(NSString *)#> itemWidth:<#(int)#> itemHeight:<#(int)#> startCharMap:<#(char)#>];

// 精灵菜单
CCSprite *normal = [CCSprite spriteWithFile:@"btn-about-normal.png"];
CCSprite *selected = [CCSprite spriteWithFile:@"btn-about-selected.png"];
CCMenuItemSprite *item3 = [CCMenuItemSprite itemWithNormalSprite:normal selectedSprite:selected block:^(id sender) {
CCLOG(@"点击了About");
}];
CCScaleBy *scale = [CCScaleBy actionWithDuration:1 scale:1.5];
[item3 runAction:[CCRepeatForever actionWithAction:[CCSequence actions:scale, [scale reverse], nil]]];

// 图片菜单
CCMenuItemImage *item4 = [CCMenuItemImage itemWithNormalImage:@"btn-play-normal.png" selectedImage:@"btn-play-selected.png" block:^(id sender) {
CCLOG(@"点击了Play");
}];

// 开关菜单
[CCMenuItemFont setFontName:@"Marker Felt"];
[CCMenuItemFont setFontSize:30];
CCMenuItemFont *high = [CCMenuItemFont itemWithString:@"High"];
CCMenuItemFont *middle = [CCMenuItemFont itemWithString:@"Middle"];
CCMenuItemFont *low = [CCMenuItemFont itemWithString:@"Low"];
CCMenuItemFont *slient = [CCMenuItemFont itemWithString:@"Slient"];
CCMenuItemToggle *item5 = [CCMenuItemToggle itemWithItems:@[slient, low, middle, high] block:^(id sender) {
CCLOG(@"点击了开关");
}];
item5.selectedIndex = 2;

// 2.整个菜单
CCMenu *menu = [CCMenu menuWithItems:item1,item2,item3,item4,item5, nil];
// 菜单项垂直排布
// [menu alignItemsVerticallyWithPadding:20];
// [menu alignItemsHorizontally];
// 第1行1个,第2行也是一个,第3行2个,第4行1个菜单项
[menu alignItemsInColumnsWithArray:@[@1, @1, @2, @1]];
// 第1列1个,第2列2个,第3列也是2个菜单项
[menu alignItemsInRowsWithArray:@[@1, @2, @2]];
[self addChild:menu];
}
return self;
}
@end

H:/0928/03-粒子系统_HelloWorldLayer.h
//
//  HelloWorldLayer.h
//  03-粒子系统
//
//  Created by apple on 13-9-28.
//  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:/0928/03-粒子系统_HelloWorldLayer.m
//
//  HelloWorldLayer.m
//  03-粒子系统
//
//  Created by apple on 13-9-28.
//  Copyright itcast 2013年. All rights reserved.
//

// Import the interfaces
#import "HelloWorldLayer.h"

// Needed to obtain the Navigation Controller
#import "AppDelegate.h"

#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;
}
return self;
}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//    CCParticleSystem *p = [CCParticleSun node];
//    p.texture = [[CCTextureCache sharedTextureCache] addImage:@"smoke.png"];
//    p.position = ccp(100, 100);
//
//    CCParticleSystem *p2 = [CCParticleSun node];
//    p2.texture = [[CCTextureCache sharedTextureCache] addImage:@"smoke.png"];
//    p2.position = ccp(300, 100);
//
//    CCParticleSystem *p3 = [CCParticleSun node];
//    p3.texture = [[CCTextureCache sharedTextureCache] addImage:@"smoke.png"];
//    p.texture = [[CCTextureCache sharedTextureCache] addImage:@"smoke.png"];
//
//    CCParticleSystemQuad *p = [CCParticleSystemQuad particleWithFile:@"my.plist"];
// 使用了同一张纹理的多个粒子,可以统一加到一个BatchNode里面
//     CCParticleBatchNode *batch = [CCParticleBatchNode batchNodeWithFile:@"smoke.png"];
//    CCParticleBatchNode *batch = [CCParticleBatchNode batchNodeWithTexture:p3.texture];
//    [batch addChild:p];
//    [batch addChild:p2];
//    [batch addChild:p3];
// 所有的粒子效果都直接继承自CCParticleSystemQuad
// plist是使用particleDesigner软件生成的
CCParticleSystemQuad *p = [CCParticleSystemQuad particleWithFile:@"LavaFlow.plist"];
[self addChild:p];
}

@end

H:/0928/04-图层_HelloWorldLayer.h
//
//  HelloWorldLayer.h
//  04-图层
//
//  Created by apple on 13-9-28.
//  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:/0928/04-图层_HelloWorldLayer.m
//
//  HelloWorldLayer.m
//  04-图层
//
//  Created by apple on 13-9-28.
//  Copyright itcast 2013年. All rights reserved.
//

// Import the interfaces
#import "HelloWorldLayer.h"

// Needed to obtain the Navigation Controller
#import "AppDelegate.h"

#pragma mark - HelloWorldLayer

@interface HelloWorldLayer()
{
CCSprite *_sprite;

CGFloat _accelerationX;
}
@end

// 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.accelerometerEnabled = YES;

_sprite = [CCSprite spriteWithFile:@"Icon.png"];
[self addChild:_sprite];

[self scheduleUpdate];
}
return self;
}

#pragma mark 监听加速计
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
// -1 ~ 1
CCLOG(@"x=%f, y=%f, z=%f", acceleration.x, acceleration.y, acceleration.z);

// 灵敏度
int lingmindu = 6;
// 加速计关键--1--在这儿只记录值,不要更新位置,因为调用频率低
_accelerationX = acceleration.x * lingmindu;
}

// 加速计关键--2--在这儿才更改位置值,好处是:调用频率高
- (void)update:(ccTime)delta
{
_sprite.position = ccpAdd(_sprite.position, ccp(6, 0));
}

@end

H:/0928/05-CCLayerColor_HelloWorldLayer.h
//
//  HelloWorldLayer.h
//  05-CCLayerColor
//
//  Created by apple on 13-9-28.
//  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 : CCLayerGradient <GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate>
{
}

// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;

@end

H:/0928/05-CCLayerColor_HelloWorldLayer.m
//  HelloWorldLayer.m
//  05-CCLayerColor
//  Created by apple on 13-9-28.
//  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];

// 创建有颜色的图层
// HelloWorldLayer *layer = [HelloWorldLayer layerWithColor:ccc4(255, 0, 0, 255)];

HelloWorldLayer *layer = [HelloWorldLayer layerWithColor:ccc4(255, 0, 0, 255) fadingTo:ccc4(0, 0, 255, 255)];

// add layer as a child to scene
[scene addChild: layer];

// return the scene
return scene;
}

// 当使用CCLayerColor\CCLayerGradient时,重写init方法是无效的
// -(id) initWithColor:(ccColor4B)color
-(id) initWithColor:(ccColor4B)color fadingTo:(ccColor4B)end
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self = [super initWithColor:color fadingTo:end]) ) {

// create and initialize a Label
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];

// ask director for the window size
CGSize size = [[CCDirector sharedDirector] winSize];

// position the label on the center of the screen
label.position =  ccp( size.width /2 , size.height/2 );

// add the label as a child to this Layer
[self addChild: label];

//
// Leaderboards and Achievements
//

// Default font size will be 28 points.
[CCMenuItemFont setFontSize:28];

// to avoid a retain-cycle with the menuitem and blocks
__block id copy_self = self;

// Achievement Menu Item using blocks
CCMenuItem *itemAchievement = [CCMenuItemFont itemWithString:@"Achievements" block:^(id sender) {

GKAchievementViewController *achivementViewController = [[GKAchievementViewController alloc] init];
achivementViewController.achievementDelegate = copy_self;

AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];

[[app navController] presentModalViewController:achivementViewController animated:YES];

[achivementViewController release];
}];

// Leaderboard Menu Item using blocks
CCMenuItem *itemLeaderboard = [CCMenuItemFont itemWithString:@"Leaderboard" block:^(id sender) {

GKLeaderboardViewController *leaderboardViewController = [[GKLeaderboardViewController alloc] init];
leaderboardViewController.leaderboardDelegate = copy_self;

AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];

[[app navController] presentModalViewController:leaderboardViewController animated:YES];

[leaderboardViewController release];
}];

CCMenu *menu = [CCMenu menuWithItems:itemAchievement, itemLeaderboard, nil];

[menu alignItemsHorizontallyWithPadding:20];
[menu setPosition:ccp( size.width/2, size.height/2 - 50)];

// Add the menu to the layer
[self addChild:menu];

}
return self;
}

@end

H:/0928/06-飞机_GameLayer.h
//
//  GameLayer.h
//  06-飞机
//
//  Created by apple on 13-9-28.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "CCLayer.h"
#import "cocos2d.h"

@interface GameLayer : CCLayer
+ (CCScene *)scene;
@end

H:/0928/06-飞机_GameLayer.m
//
//  GameLayer.m
//  06-飞机
//
//  Created by apple on 13-9-28.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "GameLayer.h"
#import "Player.h"
#import "SimpleAudioEngine.h"

@interface GameLayer()
{
CCSpriteBatchNode *_batch;

CCSprite *_bg1, *_bg2;

Player *_player;

BOOL _isGameRuning;
}
@end

@implementation GameLayer
+ (CCScene *)scene
{
CCScene *scene = [CCScene node];

GameLayer *layer = [GameLayer node];
[scene addChild:layer];

return scene;
}

- (id)init
{
if (self = [super init]) {
// 1.基本初始化
[self setupBasic];

// 2.初始化背景
[self setupBg];

// 3.初始化玩家
[self setupPlayer];

// 4.开始游戏
[self beginGame];
}
return self;
}

#pragma mark - 游戏控制
#pragma mark 暂停游戏
- (void)pauseGame
{
_isGameRuning = NO;

// 1.停止消息调度
[self unscheduleAllSelectors];

// 2.暂停背景音乐
[[SimpleAudioEngine sharedEngine] pauseBackgroundMusic];
}

#pragma mark 开始游戏
- (void)beginGame
{
_isGameRuning = YES;

// 1.开始消息调度
[self scheduleUpdate];

// 2.播放背景音乐
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"game_music.mp3"];
}

#pragma mark - 消息调度
- (void)update:(ccTime)delta
{
// 1.滚动背景
[self scrollBg];

// 2.碰撞检测
}

#pragma mark 滚动背景
- (void)scrollBg
{
CGFloat height = _bg1.contentSize.height;

CGFloat bg1Y = _bg1.position.y;
bg1Y --;

if (bg1Y <= -height) {
bg1Y = 0;
}

_bg1.position = ccp(0, bg1Y);
_bg2.position = ccp(0, bg1Y + height);
}

#pragma mark - 初始化
#pragma mark 基本初始化
- (void)setupBasic
{
// 1.加载纹理相册
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"gameArts.plist"];

// 2.创建批处理
_batch = [CCSpriteBatchNode batchNodeWithFile:@"gameArts.png"];
[self addChild:_batch];

// 3.设置可触摸
self.touchEnabled = YES;

// 4.菜单
CCMenuItem *item = [CCMenuItemFont itemWithString:@"暂停/开始" block:^(id sender) {
if (_isGameRuning) {
[self pauseGame];
} else {
[self beginGame];
}
}];
[self addChild:[CCMenu menuWithItems:item, nil]];
}

#pragma mark 初始化背景
- (void)setupBg
{
_bg1 = [CCSprite spriteWithSpriteFrameName:@"background_2.png"];
_bg1.anchorPoint = CGPointZero;

_bg2 = [CCSprite spriteWithSpriteFrameName:@"background_2.png"];
_bg2.anchorPoint = CGPointZero;

[_batch addChild:_bg1];
[_batch addChild:_bg2];
}

#pragma mark 初始化玩家
- (void)setupPlayer
{
_player = [Player node];
[_batch addChild:_player];
}

#pragma mark - 触摸监听
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];

// 1.当前点
CGPoint curP = [[CCDirector sharedDirector] convertToGL:[touch locationInView:touch.view]];

// 2.上一个点
CGPoint lastP = [[CCDirector sharedDirector] convertToGL:[touch previousLocationInView:touch.view]];

// 3.设置飞机的位置
_player.position = ccpAdd(_player.position, ccpSub(curP, lastP));
}
@end

H:/0928/06-飞机_Player.h
//
//  Player.h
//  06-飞机
//
//  Created by apple on 13-9-28.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "CCSprite.h"

@interface Player : CCSprite

@end

H:/0928/06-飞机_Player.m
//
//  Player.m
//  06-飞机
//
//  Created by apple on 13-9-28.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Player.h"
#import "cocos2d.h"

@implementation Player

- (id)init
{
if (self = [super initWithSpriteFrameName:@"hero_fly_1.png"]) {
// 1.初始化位置
self.anchorPoint = ccp(0.5, 0);
self.position = ccp([CCDirector sharedDirector].winSize.width * 0.5, 0);

// 2.播放帧动画
CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCSpriteFrame *frame1 = [cache spriteFrameByName:@"hero_fly_1.png"];
CCSpriteFrame *frame2 = [cache spriteFrameByName:@"hero_fly_2.png"];
CCAnimation *animation = [CCAnimation animationWithSpriteFrames:@[frame1, frame2] delay:0.1];
[self runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]]];
}
return self;
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐