cocos2dx3.0游戏编程1-动作游戏之准备工作
来源:互联网 发布:python控制机器人 编辑:程序博客网 时间:2024/05/21 08:51
本教程由落羽原创,欢迎转载,转载请注明地址http://blog.csdn.net/cqjianx/article/category/2407957
1.建工程
游戏最重要的就是动画,作为本系列教程的第一篇,动画最合适了。这一节主要包含helloworld程序的讲解以及一个简单动画播放。首先新建一个cocos2dx helloworld工程,建好后看起来是这样。
运行下, 如果没问题了运行起来是这样
2.修改AppDelegate.cpp
接下来我们对程序进行下修改,以便于我们后续的开发。
打开AppDelegate.cpp,修改程序初始化函数
bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { glview = GLView::create("My Game"); director->setOpenGLView(glview); } // turn on display FPS director->setDisplayStats(false);//不显示FPSglview->setDesignResolutionSize(1280, 800, ResolutionPolicy::EXACT_FIT);//设置屏幕适配尺寸和策略 // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object auto scene = HelloWorld::createScene(); // run director->runWithScene(scene); return true;}
做的改动就是把显示FPS去掉,设置屏幕适配尺寸为1280*800,适配策略为EXACT_FIT,
为什么要设置成1280*800? 因为以后教程的动画都是基于1280*800的屏幕来画的,所以这里先设置好,方便接下来的开发。至于适配策略,落羽这里简单讲下,不再深入讲解,
主要有三种,分别是
EXACT_FIT:充满整个屏幕,游戏画面有可能变形。
NO_BORDER:按比例拉伸,不留黑边,游戏画面有可能超出屏幕外。
SHOW_ALL:按比例拉伸,在屏幕内显示整个游戏画面,有可能留下黑边。
3.修改helloword.cpp加入动画
打开helloword.cpp修改初始化函数。
把默认添加的label和背景去掉,添加我们自己的背景, 加载动画创建精灵,播放动画。
bool HelloWorld::init(){ ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1);//my codeauto *background = Sprite::create("background.png");//添加背景background->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));this->addChild(background);Vector<SpriteFrame*> frameVector;//动画图片容器char str[30];for(int i = 1; i <= 8; ++i)//加载图片{sprintf(str, "player1_brother_move_%02d.png", i);frameVector.pushBack(SpriteFrame::create(str, Rect(0, 0, 600, 400)));}auto *animation = Animation::createWithSpriteFrames(frameVector, 0.1);auto *animate = Animate::create(animation);auto *action = RepeatForever::create(animate);//创建无限循环动画auto sprite = Sprite::create();sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));sprite->runAction(action);this->addChild(sprite); return true;}
当然创建动画有好几种方式,落羽这里只用了一种方式,以后我们会用到其他的方式,这部分内容比较简单,创建下精灵,播放动画,添加子节点,就好了。最后的效果如下。
图片都是落羽自己画的哦,哈哈
本节源码及图片下载http://pan.baidu.com/s/1bnmyzKz
- cocos2dx3.0游戏编程1-动作游戏之准备工作
- cocos2dx3.0游戏编程2-动作游戏之自定义精灵类
- cocos2dx3.0游戏编程3-动作游戏之类对象交互
- cocos2dx3.0游戏编程-前言
- 游戏设计之我见 —— 动作游戏中的攻防设计
- 【D3D11游戏编程】学习笔记四:准备工作
- cocos2d-x游戏实例(23)-简易动作游戏(1)
- cocos2d-x游戏实例(23)-简易动作游戏(1)
- 日历之准备工作1
- DirectX 9.0c游戏开发手记之RPG编程自学日志之2: Preparing for the Book (准备工作)(上)
- DirectX 9.0c游戏开发手记之RPG编程自学日志之3: Preparing for the Book (准备工作)(中)
- DirectX 9.0c游戏开发手记之RPG编程自学日志之4: Preparing for the Book (准备工作)(下)
- TA游戏推荐:黑暗画风3D动作游戏《恐惧之魂》
- 游戏设计之我见 —— 动作游戏中敌人的受击设计
- 游戏设计之我见 —— 动作游戏中的敌人移动设计
- Swift游戏开发之俄罗斯方块:No.2 准备工作
- cocos2dx3.0戳青蛙游戏(打地鼠)
- 斗地主游戏准备工作.
- 使用Xcode和Instruments调试解决iOS内存泄露
- Selenium命令(一)
- (转)Android TextView的各种属性
- android 常用类解析
- python 使用turtle画国旗
- cocos2dx3.0游戏编程1-动作游戏之准备工作
- WIN7 64位系统安装JDK并配置环境变量
- eclipse配置tomcat
- 甲骨文所见商代官职表
- 【ci框架】config.php配置文件解读
- 仿圆形图片的实现原理,例RoundedImageView
- js for 循环
- C# 操作摄像头
- 黑马程序员_java基础篇之IO流