cocos2d-x step by step(1) First Blood

下了cocos2d-x 源码,开搞!

首先,笔者本身

1)      5年没有摸过c++了

2)     没用过cocos2d-x

3)      有强烈的求知欲望(这条每个简历个人介绍不都这么写么,哈哈)

来开搞!

首先运行cocos源码,笔者vs2013打开的

喏,有图有真相

cpptest和emptytest都是能运行的,直接运行,编译直接过的,咱们说的是emptytest,也就是说空项目的那个,原因很简单,cpptest编译慢,empty快,哈哈

这是运行的界面,恩,然后。我们看一下代码:

首先,AppDelegate这个类,是一个类似于入口函数的东西,我暂且叫他这个东西,然后,我们找到

    // set searching path
    FileUtils::getInstance()->setSearchPaths(searchPath);
    
    // turn on display FPS
    director->setDisplayStats(true);

    // 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::scene();
    auto fish = FishTestScence::scene();
    // run
    director->runWithScene(scene);

    return true;

这里,auto scene就是它自带的HelloWorld::Scence的一个例子画布,

 director->runWithScene(scene);

这里,就是显示当前的场景,然后进入HelloWorldScene.cpp中

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    auto visibleSize = Director::getInstance()->getVisibleSize();
    auto 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(origin + Vec2(visibleSize) - Vec2(closeItem->getContentSize() / 2));

    // create menu, it's an autorelease object
    auto menu = Menu::create(closeItem, nullptr);
    menu->setPosition(Vec2::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", TITLE_FONT_SIZE);
    
    // position the label on the center of the screen
    label->setPosition(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(Vec2(visibleSize / 2) + origin);

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

这段代码就是它显示在界面上的一个label和一个image,什么 setposition 什么addchild啦,这些每一个程序狗都是能看懂的

大概就是这个样子。

然后呢,作为一个有尊严的程序狗,我不能老是照抄源码吧,你要写一些自己的东西,然后,笔者照猫画虎写了两个文件

FishTestScence.h 和FishTestScence.cpp

内容会传上去

我在 画布上花了一个button ,和一个简单的图片,然后鼠标点击button事件的触发,

bool FishTestScence::init()
{
    if (!Scene::init())
    {
        return false;
    }

    auto visibleSize = Director::getInstance()->getVisibleSize();
    auto origin = Director::getInstance()->getVisibleOrigin();


    auto button = ui::Button::create("dangban.png", "ball.png");

    button->setPosition(Vec2(visibleSize / 2) + origin);
    button->setPressedActionEnabled(true);
    button->setTitleText("lou zhu sb");

    button->addTouchEventListener(CC_CALLBACK_2(FishTestScence::onButtonClicked, this));
    this->addChild(button);

    return true;
}

这是我照猫画虎的代码

点击button 然后能够接收到按钮的click事件

感觉还是不错的,毕竟只是花了一天时间开始试着搞

友情提示:ball.png和dangpan.png这两张图片如果读取不对,你要跟一下他的那个读取函数,看filepath然后把图片放到路径下

源代码:test.rar

原文地址:https://www.cnblogs.com/fish124423/p/4387479.html