Cocos2d-iPhone V3 (1) 其基本程序常用的行动框架和介绍
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
版权声明:本文博主原创文章。博客,未经同意不得转载。
Cocos2d-iPhone V3 (1) 其基本程序常用的行动框架和介绍的更多相关文章
- 移动端、PC端(前后台)、小程序常用的UI框架
1.移动端UI库 ①.Vant UI 官方地址:https://youzan.github.io/vant/#/zh-CN/intro github地址:https://github.com/youz ...
- Cocos2d-x v3.1 Hello world程序(四)
Cocos2d-x v3.1 Hello world程序(四) 在上一篇文章中我们我们已经使用Cocos-Console工具生成了工程,本机生成的目录为:"D:\CocosProject\T ...
- iPhone 真机调试应用程序
原文:http://blog.sina.com.cn/s/blog_68e753f70100r3w5.html 真机调试iphone应用程序 1.真机调试流程概述 1) 真机调试应用程序, ...
- 小程序常用API介绍
小程序常用API接口 wx.request https网络请求 wx.request({ url: 'test.php', //仅为示例,并非真实的接口地址 method:"GET&qu ...
- 微信小程序常用样式汇总
本文系转载: 原文作者:chenzheng8975 原文地址:https://www.cnblogs.com/chenzheng8975/p/9605186.html 微信小程序常用样式汇总 小程序特 ...
- iOS开发-常用第三方开源框架介绍
iOS开发-常用第三方开源框架介绍 图像: 1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网 ...
- 常用的CSS框架
常用的CSS框架 之前在写自己的个人网站的时候,由于自己Web前端不是特别好,于是就去找相关的CSS框架来搭建页面了. 找到以下这么一篇文章(列出了很多常用的CSS框架): http://w3scho ...
- 像VUE一样写微信小程序-深入研究wepy框架
像VUE一样写微信小程序-深入研究wepy框架 微信小程序自发布到如今已经有半年多的时间了,凭借微信平台的强大影响力,越来越多企业加入小程序开发. 小程序于M页比相比,有以下优势: 1.小程序拥有更多 ...
- Linux运维常用的几个命令介绍【转】
Linux运维常用的几个命令介绍 1. 查看系统内核版本 [root@funsion geekxa]# cat /etc/issue CentOS release 6.5 (Final) Kerne ...
随机推荐
- GString及IntelliJIdea中调试Groovy的操作步骤
今天是学习Groovy的第一天,首先我觉得学习任何一种语言都要先弄清楚这种语言的特性,因为只有了解了特性之后学习才能达到好的效果,那么groovy的特点是什么的.我觉得groovy是一种动态语言,动态 ...
- 【SSH 基础】浅谈Hibernate关系映射(4)
继上篇博客 多对多关联映射(单向) 多对多对象关系映射,须要增加一张新表完毕基本映射. Hibernate会自己主动生成中间表 Hibernate使用many-to-many标签来表示多对多的关联,多 ...
- 程序猿进化 - 在拉钩子1024对APE节讲座计划
注意:下面这篇文章来自于我在网上拉勾1024对APE节现场演示程序. 我是蒋宇捷,信天创投的合伙人.之前是百度魔图的联合创始人. 我先做个自我介绍,事实上每次介绍自己事实上是非常痛苦的事情,由于我前不 ...
- Android编程心得-Service数据绑定初步
在Android里,Service的数据绑定是一种重要的用法,我们知道Service与Activity一样是运行在当前应用进程的主线程里面的,他们之间交互的方式有多种,下面我来介绍一下如何使用数据绑定 ...
- memcached 分布式聚类算法
memcached 分布式集群,该决定必须书面开发商自己.和redis 由分布式server决定.上 memcached 有两个选项用于分布式.第一个是:模运算 另一种是:一致性hash 分布式算法. ...
- Codeforces Round#309 C Kyoya and Colored Balls
给定一个k表示颜色的种类从1到k 然后接下来k行, 每行一个数字, 代表该颜色的球有多少个 这些球都放在一个包中,然后依次拿出. 要求颜色i的最后一个球, 必须要排在颜色i+1的最后一个球前面, ...
- Oracle SQL Lesson (5) - 使用组函数输出聚合数据
组函数AVGCOUNTMAXMINSUMVARIANCE:方差STDDEV:标准差 SELECT AVG(salary), MAX(salary), MIN(salary), SUM(salary)F ...
- UVA - 12232 Exclusive-OR (并查集扩展偏离向量)
Description You are not given n non-negative integersX0,X1,..., Xn-1 less than220, but they do exist ...
- 开源Math.NET基础数学类库使用(15)C#计算矩阵行列式
原文:[原创]开源Math.NET基础数学类库使用(15)C#计算矩阵行列式 本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p ...
- Json的反序列化 .net Newtonsoft.Json
项目中有个.json文件. { "instances": [ { "name": "baidu", "url": &qu ...