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

前端之家收集整理的这篇文章主要介绍了【cocos2dx】新建helloworld.cpp项目代码分析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。




#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
}
原文链接:https://www.f2er.com/cocos2dx/345776.html

猜你在找的Cocos2d-x相关文章