Cocos2d-iPhone V3 (1) 基本程序框架以及常用动作介绍
来源:互联网 发布:沉默的螺旋理论 知乎 编辑:程序博客网 时间:2024/06/07 07:03
Cocos2d-iPhone V3 (1) 基本程序框架以及常用动作介绍
- 博客:http://blog.csdn.net/prevention
- 作者:大锐哥
-
第一部分:一个 Cocos2d-iPhone V3 的基本框架
1. AppDelegate
首先看AppDelegate.h
,类是继承自CCAppDeleagate
,其他没什么特别的:
#import "cocos2d.h"@interface AppDelegate : CCAppDelegate@end
再看AppDelegate.m
,只要实现两个函数即可,注意其中实现startScene
就可以加载你自定义的场景啦,简单吧:
#import "AppDelegate.h"#import "MainScene.h"@implementation AppDelegate-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ [self setupCocos2dWithOptions:@{ CCSetupShowDebugStats: @(YES), }]; return YES;}- (CCScene *)startScene{ return [HomeScene scene];}
2. 你自己的场景类 MainScene
先看看MainScene.h
,我们看有两个方法,一个是静态方法scene
,一个是类方法init
:
#import "cocos2d.h"#import "cocos2d-ui.h"@interface MainScene : CCScene+ (MainScene *)scene;- (id)init;@end
再看看MainScene.m
,这里头东西就多了。首先看整体结构:
#import "MainScene.h"@implementation MainScene{ CCSprite *_sprite;}+ (MainScene *)scene { /* ... */ }- (id)init { /* ... */ }- (void)dealloc { /* ... */ }- (void)onEnter { /* ... */ }- (void)onExit { /* ... */ }- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event { /* ... */ }@end
必须要有一个静态方法返回一个自定义的场景实例scene
方法。还要有onEnter
和onExit
表示进入/离开该场景就会调用。touchBegan
是一个 Touch Handler。
2.1. 静态方法 scene
没什么好说的:
+ (MainScene *)scene{ return [[self alloc] init];}
2.2. init
- (id)init{ // Apple recommend assigning self with supers return value self = [super init]; if (!self) return(nil); // Enable touch handling on scene node self.userInteractionEnabled = YES; // Create a colored background (Dark Grey) CCNodeColor *background = [CCNodeColor nodeWithColor:[CCColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:1.0f]]; [self addChild:background]; // Add a sprite _sprite = [CCSprite spriteWithImageNamed:@"Icon-72.png"]; _sprite.position = ccp(self.contentSize.width/2,self.contentSize.height/2); [self addChild:_sprite]; // Animate sprite with action CCActionRotateBy* actionSpin = [CCActionRotateBy actionWithDuration:1.5f angle:360]; [_sprite runAction:[CCActionRepeatForever actionWithAction:actionSpin]]; // done return self;}
- 调用
super
的init
- 设置
userInteractionEnabled
为YES
来接收触摸事件 - 添加背景节点,这里用的是
CCNodeColor
- 添加精灵节点
CCSprite
- 给精灵节点添加动作
CCActionRotateBy
- 返回
self
2.3. 进入场景 Handler:onEnter
一定要记得调用super
的onEnter
:
- (void)onEnter{ [super onEnter];}
2.4. 离开场景 Handler:onExit
一定要记得调用super
的onExit
:
- (void)onExit{ [super onExit];}
2.5. Touch Handler
- (void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event{ CGPoint touchLoc = [touch locationInNode:self]; CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:1.0f position:touchLoc]; [_sprite runAction:actionMove];}
- 首先根据传入的
UITouch
参数来获取被触摸位置CGPoint
- 根据获取到的位置设定
CCAction
,最后运行这个CCAction
第二部分:动作
1. 位移一段距离CCActionMoveBy
+ (id)actionWithDuration:(CCTime)duration position:(CGPoint)deltaPosition;
2. 位移到CCActionMoveTo
+ (id)actionWithDuration:(CCTime)duration position:(CGPoint)position;
3. 旋转一个角度CCActionRotateBy
注意其中的 angle 是角度(一周 360 度),不是弧度(一周 2π):
+ (id)actionWithDuration:(CCTime)duration angle:(float)deltaAngle;
4. 旋转到CCActionRotateTo
注意其中的 angle 是角度(一周 360 度),不是弧度(一周 2π):
+ (id)actionWithDuration:(CCTime)duration angle:(float)angle;
5. 渐变出现CCActionFadeIn
This action fades in the target, it modifies the opacity from 0 to 1.
+ (id)actionWithDuration:(CCTime)d;
6. 渐变消失CCActionFadeOut
This action fades out the target, it modifies the opacity from 1 to 0.
+ (id)actionWithDuration:(CCTime)d;
7. 渐变到CCActionFadeTo
你可能会注意到 Cocos2d 的源码里有拼写错误,opacity
写成了opactiy
(CCActionInterval.h
中):
/** * Creates a fade action. * * @param duration Action duration. * @param opactiy Opacity to fade to. * * @return New fade action. */+ (id)actionWithDuration:(CCTime)duration opacity:(CGFloat)opactiy;
-
转载请注明来自:http://blog.csdn.net/prevention
0 0
- Cocos2d-iPhone V3 (1) 基本程序框架以及常用动作介绍
- 【iOS-Cocos2d游戏开发】Cocos2d-iPhone动作Action-基本动作介绍
- 【iOS-Cocos2d游戏开发】Cocos2d-iPhone动作Action-基本动作介绍
- cocos2d-iphone v3官方介绍
- Cocos2d-x_CCAction(常用动作类)介绍
- cocos2d-iphone 动作
- cocos2d的基本动作
- Cocos2d-x基本动作
- cocos2d基本动作函数
- Cocos2d 各种动作介绍
- Cocos2d 各种动作介绍
- Cocos2D常用动作(Action)
- cocos2d-x常用动作
- 【iOS-Cocos2d游戏开发】Cocos2d-iPhone动作Action-瞬时动作
- 【iOS-Cocos2d游戏开发】Cocos2d-iPhone动作Action-延时动作
- 【iOS-Cocos2d游戏开发】Cocos2d-iPhone动作Action-组合动作
- 【iOS-Cocos2d游戏开发】Cocos2d-iPhone动作Action-扩展动作
- 【iOS-Cocos2d游戏开发】Cocos2d-iPhone动作Action-扩展动作
- HDU--Max sum(DP)
- Single Number II
- 【HTML、JAVASCRIPT、CSS】3、Javascript基本概念
- 求一个字符串中大小写个数
- 打印一个目录结构
- Cocos2d-iPhone V3 (1) 基本程序框架以及常用动作介绍
- 直接插入排序、二分插入排序、希尔排序、冒泡排序与简单选择排序
- HDU 3074 Multiply game
- php_header
- IplImage, CvMat, Mat 的关系和相互转换(转)
- Sicily.1342.开心的金明&&Sicily.1146.采药
- 酒桌饭局上的那些事,男生必学,女生必看,非常有用
- 输入一个字符串,打印出其中所有的数字
- ubuntu 和win7双系统安装后win7无法启动解决办法