加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

【cocos2dx】新建helloworld.cpp项目代码分析

发布时间:2020-12-14 19:40:17 所属栏目:百科 来源:网络整理
导读:#include "HelloWorldScene.h"//命名空间// #define USING_NS_CC using namespace cocos2dUSING_NS_CC;/************************************************************************//*HelloWorldScene.h 与 HelloWorldScene.cpp这两个文件定义了helloworld




#include "HelloWorldScene.h"


//命名空间
//   #define USING_NS_CC using namespace cocos2d
USING_NS_CC;


/************************************************************************/
/*
HelloWorldScene.h 与 HelloWorldScene.cpp
这两个文件定义了helloworld项目中的默认场景
HelloWorld类继承自CCLayer,因此他本身也是一个层

*/
/************************************************************************/
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()
{

	/************************************************************************/
	/* 
		init()可以简单的划分为4部分

		1.调用父类的init方法来进行最初的初始化
		2.创建关闭程序的菜单并添加到层中
		3.创建一个文本标签并添加到层中,显示“HelloWorld”
		4.用“HelloWorld.png”创建一个精灵,并添加到层中
	*/
	/************************************************************************/
    //////////////////////////////
    // 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.

    // 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));

	//log test  ==   为啥输出的都是0 ?
	CCLOG("origin.x==%d,origin.y==%d",origin.x,origin.y);
	CCLOG("visibleSize.width==%d",visibleSize.width);
	CCLOG("closeItem->getContentSize().width/2==%d",closeItem->getContentSize().width / 2);
	CCLOG("closeItem->getCOntentSize().height/2==%d",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);   //我改成50,原来是1 

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

	//ml @2014.11.25   12:06
	//CCSprite * bird = CCSprite spriteWithFile : @"bird01.png";


	auto fish = CCSprite::create();

	CCLOG("retaincount after init:%d",fish->getReferenceCount());     //  1
	fish->retain();
	CCLOG("retaincount after retain:%d",fish->getReferenceCount());   //  2
	fish->release();
	CCLOG("retainCount after release:%d",fish->getReferenceCount());   //  2
	//fish->autorelease();  //此处和上面的release()不能同时出现,否则会出现断点
	CCLOG("retainCount after autorelease:%d",fish->getReferenceCount());   //  2

//很有趣,若把fish->release();注释掉,下面的那个不注释,则结果是1,2,1,1

	


    // add a label shows "Hello World"
    // create and initialize a label
	// TTF(TrueTypeFont)是一种字库名称
    
    auto label = Label::createWithTTF("Hello World","fonts/Marker Felt.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);

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

    // position the sprite on the center of the screen
    sprite->setPosition(Vec2(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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读