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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 10个linux常用命令_shell常用的命令

    10个linux常用命令_shell常用的命令文章目录20个linux常用命令1.ls:列出文件list2.cd:切换目录changedirectory3.cp:复制copy4.mv:移动move5.rm:移除,删除remove6.mkdir:创建文件夹makedirectory7.rmdir:移除,删除文件夹removedirectory8.chown:更改所有者changeowner9.chmod:更改文件的权限模式changemode10.find:查找11.|:管道12.grep:按行查找并匹配13.tar:

    2022年10月3日
    2
  • Python写入Excel文件-多种实现方式(测试成功,附代码)

    目录xlsxwriter库储存数据到excel简介示例:写入excel更多添加工作表样式:写入单元格数据插入图片写入超链接插入图表pandas库储存数据到excel简介示例:写入excelopenpyxl库储存数据到excel示例:写入excel更多打开已有文件根据数字得到字母,根据字母得到数字删除工作表查看表名和选择表(sheet)设置单元格风格xlsxwriter库储存数据到excel简介功能比较强:支..

    2022年4月3日
    420
  • react路由懒加载_vue-router实现路由懒加载

    react路由懒加载_vue-router实现路由懒加载路由懒加载是什么意思?在开发中,我们打开开发者工具,会发现我们刚刚打开就会去加载所有页面.路由懒加载就是只加载你当前点击的那个模块按需去加载路由对应的资源,可以提高加载速度(一个页面加载过后再次访问不会重复加载)实现原理:将路由相关的组件,不再直接导入了,而是改写成异步组件的写法,只有当函数被调用的时候,才去加载对应的组件内容方法一:重写首先,新建一个asynccComponent.js,作为公共jsimport{ComponentasReactComponet}fr

    2022年10月7日
    3
  • 线性代数行列式的计算方法(通过代数余子式计算)

    permutation.h/***********************************//*NonstandardPermutationheaderCopyright(C)2021—@情久小羊,#FreeThereisnodataerrorcheck*//***********************************/#pragmaonce#include<initializer_list>#include<xutil.

    2022年4月7日
    68
  • 移动端开发规范

    移动端开发规范移动端开发规范引言:最近得空,整理一些平时工作中要求的开发规范,浅薄之处还请大家多指教。目录移动端开发规范代码规范基本原则代码清晰一致性通用规范类命名方法命名变量命名常量命名枚举类型命名图片命名通用规范通用设计规范开屏页版本号版本检查开屏页广告推送通用测试用例及处理规范规范用例数据埋点规范…

    2022年6月24日
    32
  • Java安装教程

    Java安装教程今日份闲来无事,整理了一些Java的数据库安装教程,请君共阅:其实主要就分三部分:1:下载Java的安装包并安装Java2:电脑环境设置3:验证Java是否安装成功第一步:下载Java的安装包并安装Java:1.首先去官网现在JAVA安装包JAVA安装包下载地址:https://www.oracle.com/technetwork/java/javase/downloads/index.html(官网地址),打开链接后,如下图:2.点击下面椭圆圈起来的“Download”按钮。3:跳转到

    2022年7月9日
    18

发表回复

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

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