cocos2dx 的Hello world的简单分析

cocos2dx 的Hello world的简单分析

Node之间的关系:

cocos2dx 的Hello world的简单分析

场景AppDelegate.cpp又要由导演去调用然后进行表演:

    // create a scene. it's an autorelease object
    auto scene = HelloWorld::createScene();

    // run
    director->runWithScene(scene);

这里层里面的场景HelloWorldScene.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();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.
    //每一个addChild会创建出一个添加的层
    // 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);

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

    // add a label shows "Hello World"
    // create and initialize a label
    
    auto label = Label::createWithTTF("Hello sunzhiyan", "fonts/arial.ttf", 24);
    
    // position the label on the center of the screen
    label->setPosition(Vec2(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);

    auto label1= Label::createWithTTF("88888888888888", "fonts/arial.ttf", 40);

    // position the label on the center of the screen
    label1->setPosition(Vec2(origin.x + visibleSize.width / 3,
        origin.y + visibleSize.height - label1->getContentSize().height));

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

    //创建一个精灵
    auto sprite = Sprite::create("sun.jpg");

    //设置精灵的显示位置
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // 将精灵添加到层里面
    this->addChild(sprite, 0);
    
    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109300.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 机器学习:代价函数cost function

    机器学习:代价函数cost function本文系转载,咯有修改原博客地址:http://blog.csdn.net/u012162613/article/details/44239919在此,向原作者表达感谢,致敬!1.从方差代价函数说起代价函数经常用方差代价函数(即采用均方误差MSE),比如对于一个神经元(单输入单输出,sigmoid函数),定义其代价函数为:C=(y−a)22C=\frac{(y-a)^2}{2}其中yy是我们期望

    2022年5月15日
    50
  • 字节转换_1MB等于多少字节?

    字节转换_1MB等于多少字节?字节:计算机中最小的存储单位就是字节,所有的存储数据都以bit为形式存储;以下简单介绍关于bit相关的单位运算规则;8bit(比特)=1B-Byte(字节)1024B-Byte(字节)

    2022年8月3日
    10
  • 关于左值和右值的一些问题总结[通俗易懂]

    在C语言当中,我们经常会遇见一些平时感觉怎么用都不会出错的小知识点,但是再将它的难度提高一点点的时候,或者将它改变一点点,我们就不再将它用起来那么的得心应手。左值和右值正是一个这样的十足十的例子。在学习了指针知识之后,高度理解左值与右值便不再显得那么的无聊。这个解释看起来有点傻,但是不得不说:左值就是那些能够出现在赋值符号左边的东西,右值就是那些能够出现在赋值符号右边的东西。例如:a=b+25;这…

    2022年4月10日
    61
  • 蓝牙音频传输格式:ACC,SBC,APTX和LDAC_蓝牙sbc和aac的区别

    蓝牙音频传输格式:ACC,SBC,APTX和LDAC_蓝牙sbc和aac的区别先转载一部分通用的知识众所周知,蓝牙耳机的音频解码主要分为sbc,aac,aptx,ldac,Ihac等几种。下面是简单的介绍。、、1、sbc是通用的最基本的解码方式,蓝牙耳机都支持,支持44khz/16bit的音频,最高码率是328kbps,延时大约220ms,所以音质一般。2、aac是苹果产品通用的解码方式,跟sbc差不多,支持44khz/16bit的音频,最高码率512kbps,延时大约100ms,音质略好于sbc。3、aptx是高通的专利,支持48khz/16bit的音频,最高码率352kb

    2025年9月1日
    3
  • Flutter BaseWidget 实现onResume、onPause()

    Flutter BaseWidget 实现onResume、onPause()最近用Flutter开发的项目算是完成了开发到上线第一阶段了。任何一个项目开始了,若想追求的是更好,那么就需要下功夫对项目用户体验和代码效率深入的研究了。作为用户和产品经理、老板、UI、不懂技术的其他人员看到的产品只是表面的。…

    2022年5月5日
    144
  • PyCharm 2021.5.3 x64激活码[在线序列号]

    PyCharm 2021.5.3 x64激活码[在线序列号],https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
    54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号