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

0 0
原创粉丝点击