iOS_31_cocos2d环境搭建

最终效果图如:

1.从Git上下载 cocos2d的压缩包,大概100M

2.解压后,进入cocos2d主目录,复制路径到终端

3.执行./install.sh开始安装(实质是拷贝至XCode目录)

安装前XCode,新建项目时界面如下

安装后XCode,新建项目时界面如下

建好的工程目录结构如下

直接运行程序,效果如下:

应用代理

//
//  AppDelegate.h
//  31_cocos2D入门
//
//  Created by beyond on 14-9-5.
//  Copyright com.beyond 2014年. All rights reserved.

//  AppDelegate 继承自 CCAppDelegate
/*
    CCAppDelegate 继承自NSObect,遵守协议:<UIApplicationDelegate, CCDirectorDelegate>
    CCAppDelegate 拥有成员:UIWindow *window_

 *  大多 Cocos2d 应用 应该重写 CCAppDelegate, CCAppDelegate作为应用的程序入口.
    至少应该复写 startScene 方法,以便 返回应用要展示的首个场景
    如果想更进一步定制 Cocos2d(例如自定义显示的像素模式),
    请复写 applicaton:didFinishLaunchingWithOptions: 方法
 */

#import "cocos2d.h"

@interface AppDelegate : CCAppDelegate
@end
//
//  AppDelegate.m
//  31_cocos2D入门
//
//  Created by beyond on 14-9-5.
//  Copyright com.beyond 2014年. All rights reserved.
//  AppDelegate 继承自 CCAppDelegate
/*
 CCAppDelegate 继承自NSObect,遵守协议:<UIApplicationDelegate, CCDirectorDelegate>
 CCAppDelegate 拥有成员:UIWindow *window_

 *  大多 Cocos2d 应用 应该重写 CCAppDelegate, CCAppDelegate作为应用的程序入口.
 至少应该复写 startScene 方法,以便 返回应用要展示的首个场景
 如果想更进一步定制 Cocos2d(例如自定义显示的像素模式),
 请复写 applicaton:didFinishLaunchingWithOptions: 方法
 */

#import "AppDelegate.h"
#import "IntroScene.h"
#import "HelloWorldScene.h"

@implementation AppDelegate

//
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
	// 当继承自CCAppDelegate,唯一要实现的方法就是这一个方法,应用首次启动就会第一个执行本方法

	// 在这儿,可以设置 Cocos2D 各参数的默认值
	// There are a number of simple options you can change.
	// 如果你还嫌调用setupCocos2dWithOptions方法,不够灵活自由,那么你可以自己顶置 Cocos2D
	[self setupCocos2dWithOptions:@{
		// 显示 FPS
		CCSetupShowDebugStats: @(YES),

        // 使用降低了的帧率
        // CCSetupAnimationInterval: @(1.0/30.0),
        // 使用一个加快的帧率
        //		CCSetupFixedUpdateInterval: @(1.0/180.0),

        // 设置屏幕为竖屏模式
        //		CCSetupScreenOrientation: CCScreenOrientationPortrait,

        // 使用16位颜色:
        //		CCSetupPixelFormat: kEAGLColorFormatRGB565,
		// 使用一个统一的固定的坐标系统
        //		CCSetupScreenMode: CCScreenModeFixed,
		// Make iPad's act like they run at a 2x content scale. (iPad retina 4x)
        //		CCSetupTabletScale2X: @(YES),

        // 更多内容请参阅 CCAppDelegate.h
	}];

	return YES;
}

-(CCScene *)startScene
{
	// 本方法返回应用启动时,第一个要展示的场景
	return [IntroScene scene];
}

@end

场景一

//
//  IntroScene.h
//  31_cocos2D入门
//
//  Created by beyond on 14-9-5.
//  Copyright com.beyond 2014年. All rights reserved.
//

// Importing cocos2d.h and cocos2d-ui.h, will import anything you need to start using cocos2d-v3
#import "cocos2d.h"
#import "cocos2d-ui.h"

/**
 *  The intro scene
 *  Note, that scenes should now be based on CCScene, and not CCLayer, as previous versions
 *  Main usage for CCLayer now, is to make colored backgrounds (rectangles)
 *
 */
@interface IntroScene : CCScene

+ (IntroScene *)scene;
- (id)init;
@end
//
//  IntroScene.m
//  31_cocos2D入门
//
//  Created by beyond on 14-9-5.
//  Copyright com.beyond 2014年. All rights reserved.
//

#import "IntroScene.h"
#import "HelloWorldScene.h"

@implementation IntroScene

#pragma mark - 生命周期

+ (IntroScene *)scene
{
	return [[self alloc] init];
}

- (id)init
{

    self = [super init];
    if (!self) return(nil);

    // 创建背景颜色为深灰色
    CCNodeColor *background = [CCNodeColor nodeWithColor:[CCColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:1.0f]];
    [self addChild:background];

    // 创建文字标签
    CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello Beyond" fontName:@"Chalkduster" fontSize:36.0f];
    label.positionType = CCPositionTypeNormalized;
    label.color = [CCColor redColor];
    // 屏幕的正中间 注意这里是笛卡尔坐标系,原点在左下方
    label.position = ccp(0.5f, 0.5f);
    [self addChild:label];

    // 创建一个开始按钮,点击后进入下一个场景
    CCButton *helloWorldButton = [CCButton buttonWithTitle:@"[ Start ]" fontName:@"Verdana-Bold" fontSize:18.0f];
    helloWorldButton.positionType = CCPositionTypeNormalized;
     // 屏幕的中间靠下方  注意这里是笛卡尔坐标系,原点在左下方
    helloWorldButton.position = ccp(0.5f, 0.35f);
     // 监听点击事件
    [helloWorldButton setTarget:self selector:@selector(onSpinningClicked:)];
    [self addChild:helloWorldButton];

    // 返回创建好的场景对象
	return self;
}

#pragma mark - 按钮点击事件,切换至下一场景

- (void)onSpinningClicked:(id)sender
{
    // 动画切换至下一个场景
    [[CCDirector sharedDirector] replaceScene:[HelloWorldScene scene]
                               withTransition:[CCTransition transitionPushWithDirection:CCTransitionDirectionLeft duration:1.0f]];
}

@end

场景二

//
//  HelloWorldScene.h
//  31_cocos2D入门
//
//  Created by beyond on 14-9-5.
//  Copyright com.beyond 2014年. All rights reserved.
//

// Importing cocos2d.h and cocos2d-ui.h, will import anything you need to start using Cocos2D v3
#import "cocos2d.h"
#import "cocos2d-ui.h"

/**
 *  主场景
 */
@interface HelloWorldScene : CCScene

+ (HelloWorldScene *)scene;
- (id)init;

@end
//
//  HelloWorldScene.m
//  31_cocos2D入门
//
//  Created by beyond on 14-9-5.
//  Copyright com.beyond 2014年. All rights reserved.
//

#import "HelloWorldScene.h"
#import "IntroScene.h"

@implementation HelloWorldScene
{
    CCSprite *_sprite;
}

#pragma mark - 生命周期
+ (HelloWorldScene *)scene
{
    return [[self alloc] init];
}

- (id)init
{

    if (!(self = [super init]) ) return(nil);

    // 场景模式下 允许交互
    self.userInteractionEnabled = YES;

    // 创建背景颜色为深灰色
    CCNodeColor *background = [CCNodeColor nodeWithColor:[CCColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:1.0f]];
    [self addChild:background];

    // 添加一个精灵,并设置位置居中
    _sprite = [CCSprite spriteWithImageNamed:@"ColorCircle.png"];

    _sprite.position  = ccp(self.contentSize.width/2,self.contentSize.height/2);
    [self addChild:_sprite];

    // 为精灵创建一个旋转动作,并且调用精灵的runAction方法,重复执行动作
    CCActionRotateBy* actionSpin = [CCActionRotateBy actionWithDuration:1.5f angle:360];
    [_sprite runAction:[CCActionRepeatForever actionWithAction:actionSpin]];

    // 右上方,创建一个返回按钮,点击后,返回至上一个场景
    CCButton *backButton = [CCButton buttonWithTitle:@"[ Back ]" fontName:@"Verdana-Bold" fontSize:18.0f];
    backButton.positionType = CCPositionTypeNormalized;
    // 屏幕的右上方 注意这里是笛卡尔坐标系,原点在左下方
    backButton.position = ccp(0.85f, 0.95f);
    // 监听点击事件
    [backButton setTarget:self selector:@selector(onBackClicked:)];
    [self addChild:backButton];

    // 返回创建好的场景对象
	return self;
}

- (void)dealloc
{
    // clean up code goes here
}

#pragma mark - Enter & Exit
// -----------------------------------------------------------------------

- (void)onEnter
{
    // 必须总是先调用父类的onEnter方法
    [super onEnter];

    // In pre-v3, touch enable and scheduleUpdate was called here
    // In v3, touch is enabled by setting userInteractionEnabled for the individual nodes
    // Per frame update is automatically enabled, if update is overridden

}

- (void)onExit
{
    // 必须总是 最后才调用父类的onExit方法
    [super onExit];
}

#pragma mark - 用户触摸屏幕,精灵跟随手指移动

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchLoc = [touch locationInNode:self];

    // 输出触摸点的坐标
    CCLOG(@"Move sprite to @ %@",NSStringFromCGPoint(touchLoc));

    // 移动精灵到触摸点处 To表示绝对  By表示相对
    CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:1.0f position:touchLoc];
    // 调用精灵的runAction方法执行动作
    [_sprite runAction:actionMove];
}

#pragma mark - 按钮点击事件

- (void)onBackClicked:(id)sender
{
    // 使用转场动画,切换场景至 IntroScene
    [[CCDirector sharedDirector] replaceScene:[IntroScene scene]
                               withTransition:[CCTransition transitionPushWithDirection:CCTransitionDirectionRight duration:1.0f]];
}
@end

时间: 2024-08-29 10:29:01

iOS_31_cocos2d环境搭建的相关文章

一、环境搭建

1 更新到最新版本的pip(这是安装python扩展包的一个插件)命令如下: python -m pip install --upgrade pip 2 使用pip安装virtualenv,命令 pip install virtualenv  如果要指定版本号,pip install virtualenv==15.0.1(安装虚拟环境) 3 创建django虚拟环境,命令 virtualenv django_basic_venv 4 使用虚拟环境 需要进入到安装目录的Scripts文件夹下,运行

Ionic2环境搭建及文件目录介绍

[注]引用自:http://blog.csdn.net/jasonzds/article/details/53821184 1环境搭建 一年前研究混合框架,初步确定了四种方案给公司选择,ionic,hbuilder,wex5,react-native这四个框架各有优缺点,ionic和react-native是国外框架,相对好一点,文档更新很快,就不一一说了,大概的思路都是一样的,js逻辑实现,同时调用原生功能,h5,css3 UI实现,其实他们都有自己的ui框架,当时选择了国内的hbuiler,

Selenium+Java+Eclipse 自动化测试环境搭建

一.下载Java windows java下载链接 https://www.java.com/zh_CN/download/win10.jsp 二.安装Java 安装好后检查一下需不需要配置环境变量,现在java 8已经不用配置环境变量了,直接在命令行输入:java -version 三.下载和安装Eclipse windows Eclipse下载链接 https://www.eclipse.org/downloads/ 你也可以下载绿色版 四.下载selenium,然后解压 selenium

Qt在Mac OS X下的编程环境搭建(配置Qt库和编译器,有图,很清楚)

尊重作者,支持原创,如需转载,请附上原地址:http://blog.csdn.net/libaineu2004/article/details/46234079 在Mac OS X下使用Qt开发,需要配置Qt库和编译器.编译器只能使用苹果公司自主研发的Clang.1.分别下载并安装XCode和Command Line Tools(必须安装),安装完毕后,Clang就有了. https://developer.apple.com/downloads/ 2.下载Qt并默认安装 http://down

基于 Eclipse 的 MapReduce 开发环境搭建

文 / vincentzh 原文连接:http://www.cnblogs.com/vincentzh/p/6055850.html 上周末本来要写这篇的,结果没想到上周末自己环境都没有搭起来,运行起来有问题的呢,拖到周一才将问题解决掉.刚好这周也将之前看的内容复习了下,边复习边码代码理解,印象倒是很深刻,对看过的东西理解也更深入了. 目录 1.概述 2.环境准备 3.插件配置 4.配置文件系统连接 5.测试连接 6.代码编写与执行 7.问题梳理 7.1 console 无日志输出问题 7.2

ICE分布式文件管理系统——ICE环境搭建(其二)

上一博文,我们讲述了ICE这个中间件的基本认识. 接下来我们讲述开发环境搭建. 其过程主要分为三步: 安装GCC-4.4.6.安装ICE-3.4.2.安装QT-4.7.3. (本文是基于LINUX下的ICE-3.4.2的安装,如果已安装了GCC(版本高于GCC-4.4.6亦可),请直接安装ICE) 一.安装GCC: (gcc各版本浏览地址:http://ftp.gnu.org/gnu/gcc/) 一般来说基于linux的操作系统都是默认安装了GCC的.假如说你的电脑没有的话 请百度一哈,可以解决

[Step-By-Step Angular2](1)Hello World与自动化环境搭建

随着rc(release candidate,候选版本)版本的推出,万众瞩目的angular2终于离正式发布不远啦!五月初举办的ng-conf大会已经过去了整整一个月,大多数api都如愿保持在了相对稳定的状态——当然也有router这样的例外,在rc阶段还在大面积返工,让人颇为不解——不过总得说来,现在学习angular2不失为一个恰当的时机. Google为angular2准备了完善的文档和教程,按理说,官网(https://angular.io)自然是学习新框架的最好教材.略显遗憾的是,在B

Linux交叉开发环境搭建 —— 效率之源

楼主今天终于把所有Linux开发环境需要的软件下载完毕了.虽然以前也是搭建过的,时间久了又折腾了一晚上. 交叉环境: Windows.Linux文件共享 SecureCRT 连接虚拟机终端 工具: VirtualBox ubuntu-16.04-desktop-amd64.iso(ubuntu官网下载) SecureCRT Source Insight 虚拟机搭建: 检查bios虚拟技术功能开启 新建虚拟机,选择创建虚拟硬盘,其余均默认 点击新建虚拟机设置->存储->选中没有光盘->点击

Intellij IDEA 14.1.4 Scala开发环境搭建

主要内容 Intellij IDEA开发环境简介 Intellij IDEA Scala开发环境搭建 Intellij IDEA常见问题及解决方案 Intellij IDEA常用快捷键 1. Intellij IDEA开发环境简介 具体介绍请参见:http://baike.baidu.com/link?url=SBY93H3SPkmcmIOmZ8H60O1k4iVLgOmdqoKdGp9xHtU-Pbdsq2cpn75ZPZPWAJxeUlwr0ravraQzOckh777beq Intelli