cocos2dx学习笔记(1)

时间:2014-04-30 00:09:07   收藏:0   阅读:693

    久仰cocos2dx的大名想要研习一下一直苦于没有时间,最近不是很忙想起这茬,准备恶补一番,先上网大致查了下资料,发现cocos2dx开发环境的搭建貌似还挺麻烦的(可能与多平台支持有关),在官网下载了最新的cocos2dx 3.0 rc 版,配置方式参考了这篇文章http://blog.csdn.net/star530/article/details/21483729,貌似cocos2dx游戏开发都是通过平台开发在移植的模式,再加上“据称”eclipse CDT+NDK的android开发模式调试困难+打包加密安全性不太好?(小白请轻拍,后续会自己尝试一下,没有实践就没有发言权嘛~~),抱着方便学习的心态先搭建了win平台开发环境----VS2012+python2.7.6(最新官方标配),听说2.x时期还需要安装cygwin等,想来目前版本算是轻松了许多。

    搭建好环境之后,官网推荐的是“HelloWorld”的cocos工程(of course),利用python创建好工程之后用VS打开运行,可以看到一个简单的cocos2dx游戏界面

bubuko.com,布布扣

我们来看下源码HelloWordScene.cpp

bubuko.com,布布扣
#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // ‘scene‘ is an autorelease object
    auto scene = Scene::create();
    
    // ‘layer‘ is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();

    Point 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(Point(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(Point::ZERO);
    this->addChild(menu, 1);

    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    
    auto label = LabelTTF::create("Hello World", "Arial", 24);
    
    // position the label on the center of the screen
    label->setPosition(Point(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(label, 1);

     //add "HelloWorld" splash screen"
    auto sprite = Sprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
    sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));


    // add the sprite as a child to this layer
    this->addChild(sprite, 0);
    
    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
    MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
bubuko.com,布布扣

可以看到当前场景是由一个sprite对象(cocos图标),"Hello world"的label,关机按钮(推出程序按钮和回调处理函数)组成,注意这句sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));因为sprite对象锚点为图像几何中心点,而坐标原点在左上角,因此居中对齐时需要平移屏幕中心点坐标的距离,想要拉伸sprite对象到全屏,我们可以通过

    Size spriteSzie = sprite->getContentSize();
    float scaleX = spriteSzie.width/visibleSize.width;
    float scaleY = spriteSzie.height/visibleSize.height;
    //log("scaleX = %f, scaleY = %f",scaleX,scaleY);
    sprite->setScale(1/scaleX, 1/scaleY);

重新设置sprite对象的长宽比以适配屏幕,如果我们想要sprite对象拥有一定的效果,以达到类似开机动画的东东,我们可以利用cocos的CCAnimation对象实现动画效果,以下是一个缩放的效果

bubuko.com,布布扣
//    ccscaleto   作用:创建一个缩放的动作
//    参数1:达到缩放大小所需的时间
//    参数2 :缩放的比例

//CCAnimation* animation = CCAnimation::create();
CCActionInterval* scaleto = CCScaleTo::create(2,2);
sprite->runAction(scaleto);
bubuko.com,布布扣

这样,在启动helloworld工程时sprite对象会出现一个缩放的动画,如图
bubuko.com,布布扣

 

cocos2dx学习笔记(1),布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!