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 ...
随机推荐
- extjs_11_mvc模式
1.非mvc模式 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWRhbV93enM=/font/5a6L5L2T/fontsize/400/fill/I ...
- Learning Cocos2d-x for WP8(3)——文字篇
原文:Learning Cocos2d-x for WP8(3)--文字篇 C#兄弟篇Learning Cocos2d-x for XNA(3)——文字篇 文字,是人类文明的象征. 文字显示,可用字符 ...
- 利用h5标签在网页上播放音乐
方案1: <embed src="等一分钟.mp3" id="aa"> <input type=button value=暂停 onclick ...
- 【Android Training - UserInfo】记住登入用户的信息[Lesson 1 - 使用AccountManager来记住用户]
Remembering Your User[记住你的用户] 每一个人都非常喜欢自己的名字能被人记住.当中最简单,最有效的使得你的app让人喜欢的方法是记住你的用户是谁,特别是当用户升级到一台新的设备或 ...
- wordpress博客近期变慢之解决(fonts.google.com)
近期发现站点訪问速度变慢.博客文章打开速度特慢,也没改动过东西. 并且近期发现google的服务非常多訪问都打不开或是变慢. 于是知道可能是那"伟大东西"在作坏事了. 症状: 网页 ...
- Codeforces Round#310 div2
C题:这题说的是套娃,如果做题的时候知道是套娃,那就好理解多了 规则1:套娃A可以放到套娃B里面,当且仅当套娃B没有放在其他套娃里面 规则2:套娃A放在套娃B里面,且套娃B没有放在其他套娃里面,那么可 ...
- Android PackageInstaller 安装和卸载
应用的安装方式:adb install或者下载安装 过程分析请參考老罗的blog,这里记录一下第三方应用程序安装apk的过程. 安装的过程主要是调用PackageInstaller这个App,源码的位 ...
- Android kxml解析WBXML
WAP Binary XML定义好XML片断表述出同步server地址.远程数据库名称.登录账号等等内容一.两种訪问方法: 眼下的kxml支持两种wap格式:WBXML/WML. 而有两种方法将解析 ...
- Codeforces Round#308
A题,看样例就知道要求什么, 水过去 #include <stdio.h> #include <string.h> #include <stdlib.h> #i ...
- slider使用TickPlacement获得游标效果
<Slider Name="slider游标效果" Maximum="3" SmallChange="0.25" TickPlacem ...