cocos2d-x学习笔记

<span style="font-family: Arial, Helvetica, sans-serif;">#include</span><span style="font-family: Arial, Helvetica, sans-serif;"> "HelloWorldScene.h"</span>


USING_NS_CC;//??等价于using namespace cocos2d;


Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease(??自动释放,如果创建失败则break) object
    auto scene = Scene::create();//??auto自由索引变量
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();


    // add layer as a child to scene
    scene->addChild(layer);//??sence调用公有函数


    // return the scene
    return scene;
}


// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
<span style="white-space:pre">	</span>//调用父类的初始化函数,失败则返回false
    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));//??this为禁用时的函数,绑定回调函数
<span style="white-space:pre">	</span>//??设置菜单项位置(Vec2是用来创建坐标点的)
    //??设置图片居中
<span style="white-space:pre">	</span>closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));


    // create menu, it's an autorelease object
<span style="white-space:pre">	</span>//用这个菜单项来创建一个菜单menu
    auto menu = Menu::create(closeItem, NULL);
<span style="white-space:pre">	</span>//设置菜单位于零点x=0,y=0;
    menu->setPosition(Vec2::ZERO);
<span style="white-space:pre">	</span>//把menu加到当前layer
    this->addChild(menu, 1);


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


    // add a label shows "Hello World"
    // create and initialize a label
    // 创建一个文本文档label,内容是“Hello World”字体是“Arial”,字号是24;
    auto label = LabelTTF::create("Hello World", "Cursive", 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(把label加到当前layer)
    this->addChild(label, 1);


    // add "HelloWorld" splash screen"
<span style="white-space:pre">	</span>//使用HelloWorld.png创建一个精灵
    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)
<span style="white-space:pre">	</span>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
}"

USING_NS_CC;//??等价于using namespace cocos2d;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease(??自动释放,如果创建失败则break) object
    auto scene = Scene::create();//??auto自由索引变量
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);//??sence调用公有函数

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
	//调用父类的初始化函数,失败则返回false
    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));//??this为禁用时的函数,绑定回调函数
	//??设置菜单项位置(Vec2是用来创建坐标点的)
    //??设置图片居中
	closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    // create menu, it's an autorelease object
	//用这个菜单项来创建一个菜单menu
    auto menu = Menu::create(closeItem, NULL);
	//设置菜单位于零点x=0,y=0;
    menu->setPosition(Vec2::ZERO);
	//把menu加到当前layer
    this->addChild(menu, 1);

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

    // add a label shows "Hello World"
    // create and initialize a label
    // 创建一个文本文档label,内容是“Hello World”字体是“Arial”,字号是24;
    auto label = LabelTTF::create("Hello World", "Cursive", 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(把label加到当前layer)
    this->addChild(label, 1);

    // add "HelloWorld" splash screen"
	//使用HelloWorld.png创建一个精灵
    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
}

写游戏的四个要素
Sprite:精灵
//精灵其实就是我们说的元素。比如一个游戏里的一个人物啊,一只笔啊什么的。
Layer:层
// 层在cocos2dx项目里是精灵以外出现的最多的元素。比如一个游戏,你需要背景层,一个精灵要运动,
//就要单独一个层,游戏有暂停的页面,那就来个暂停页面的层。层就像是一张透明的玻璃一样,把他们画上东西,
//摞在一起,俯视来看,就是一个完整的游戏了。用过PS的就直接理解成PS里的图层,和那个基本是异曲同工之处。

Scene:[si:n]场面,现场; (戏剧的)一场; 景色,风景; 事件;
//如果们把层理解为玻璃片,那么场景就是一个箱子,我们把玻璃片放在这个箱子里,去组建游戏。
//一个游戏会有很多的场景,比如,主界面,游戏界面,载入界面等等。一个场景包含多个层,一个层可能包含多个精灵。

Director(导演)
// 就和现实中的导演一样,这里的导演也是起到指导的作用的,导演在这里负责的就是让不同的场景切换,
//同时它也是游戏的核心,控制整个游戏的流程,包括开始,继续,暂停等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值