cocos2dx学习笔记(1)

    久仰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等,想来目前版本算是轻松了许多。python

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

咱们来看下源码HelloWordScene.cpp安全

#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
}

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

    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对象实现动画效果,如下是一个缩放的效果python2.7

//    ccscaleto   做用:建立一个缩放的动做
//    参数1:达到缩放大小所需的时间
//    参数2 :缩放的比例

//CCAnimation* animation = CCAnimation::create();
CCActionInterval* scaleto = CCScaleTo::create(2,2);
sprite->runAction(scaleto);

这样,在启动helloworld工程时sprite对象会出现一个缩放的动画,如图
函数

相关文章
相关标签/搜索