Cocos2d-iphone学习(一):HelloWorld

用了两周的时间学习objective-c和ios开发的基础知识,昨天听了cocos2d-x的开发大会,很有感触,由于c++忘了很多,所以还是先从cocos2d-iphone开始,等到android的移植的时候再入手cocos2d-x吧,话说用法和机制都差不多。。。


今天总结一下cocos2d-iphone的环境配置和第一个工程----HelloWorld


1.下载安装

首先下载一个cocos2d-iphone的包,我选择的是cocos2d-iphone-1.0.1稳定版,下载地址

下载完成后解压,目录如图


然后打开终端命令行,运行install-templates.sh脚本文件,命令:sudo ./install-templates.sh

完成之后,在xcode中就有coco2d-iphone的模板了,如图



2.HelloWorld

新建一个cocos2d-iphone工程,如上图选中,一切选择默认,创建完成之后运行,如图

这个难免也太Hello World了吧,游戏里最重要的是精灵,加几个精灵吧

先在Resources目录下添加几个图片资源,如图


然后打开HelloWorldLayer.m添加代码,代码很简单,只是在屏幕的四个角上添加了4个静态图片精灵,需要注意的是cocos2d-iphone中是以屏幕的左下角为原点(0, 0),向右向上分别为x, y轴正向,代码如下

// 添加精灵
        int winWidth = size.width;
        int winHeight = size.height;
        
        // 从图片文件中初始化资源
        CCSprite *leftTopSprite = [[CCSprite alloc] initWithFile:@"blue.png"];
        CCSprite *rightTopSprite = [[CCSprite alloc] initWithFile:@"green.png"];
        CCSprite *leftBottomSprite = [[CCSprite alloc] initWithFile:@"pink.png"];
        CCSprite *rightBottomSprite = [[CCSprite alloc] initWithFile:@"orange.png"];
        
        int spriteWidth = [leftTopSprite boundingBox].size.width;
        int spriteHeight = [leftTopSprite boundingBox].size.height;
        
        // 设置位置
        leftTopSprite.position = ccp(spriteWidth / 2, winHeight - spriteHeight / 2);
        rightTopSprite.position = ccp(winWidth - spriteWidth / 2, winHeight - spriteHeight / 2);
        leftBottomSprite.position = ccp(spriteWidth / 2, spriteHeight / 2);
        rightBottomSprite.position = ccp(winWidth - spriteWidth / 2, spriteHeight / 2);
        
        [self addChild:leftTopSprite];
        [self addChild:rightTopSprite];
        [self addChild:leftBottomSprite];
        [self addChild:rightBottomSprite];

效果如图



3.工具推荐

在cocos2d-iphone的官网,搜索文档的时候发现了一个工具,Dash,查看文档非常方便,添加了ios的文档要比在XCode中的查找速度快很多!



源码下载



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值