零基础学Cocos2d
// create a scene. it's an autorelease object Scene *scene = HelloWorld :: createScene (); // run director- runWithScene (scene); 那么接下来,我们看看这场戏到底内部是执行流程的
// create a scene. it's an autorelease object
Scene *scene = HelloWorld::createScene();
// run
director->runWithScene(scene);
那么接下来,我们看看这场戏到底内部是执行流程的啊。
OK,首先看看HelloWorldScene.h 到底有什么东西。
静态创建函数
static cocos2d::Scene* createScene();
初始化
virtualbool init();
菜单的一个回调函数
void menuCloseCallback(cocos2d::Ref* pSender);
这个。。。。看宏定义上面的注释说是创建一个特定的类
CREATE_FUNC(HelloWorld);
/** * define a create function for a specific type, such as Layer * @param \__TYPE__ class type to add create(), such as Layer */ #define CREATE_FUNC(__TYPE__) \ static __TYPE__* create() \ { \ __TYPE__ *pRet = new __TYPE__(); \ if (pRet && pRet->init()) \ { \ pRet->autorelease(); \ return pRet; \ } \ else \ { \ delete pRet; \ pRet = NULL; \ return NULL; \ } \ }
看完后 ,哦,,,,
CREATE_FUNC(HelloWorld);
就是相当于
在 HelloWorldScene.h 的定义
static HelloWorld* create();
在 HelloWorldScene.m 的实现
HelloWorld* HelloWorld::create() { //创建一个 HelloWorld 对象 HelloWorld* helloWorld = new HellWorld(); //判断 HelloWorld 对象是否创建以及初始化成功 if (helloWorld && helloWorld->init()) { //创建成功,初始化成功后,让其自动释放内存 helloWorld->autorelease(); //返回 HelloWorld 实例 return helloWorld; } else { //如果创建失败,将安全删除 HelloWorld 对象 delete helloWorld; helloWorld = NULL; return NULL; } }
好了,有宏的话,让我们剩下了不少代码的工作量啊。
接下来我们看看其他的吧
HelloWorldScene.cpp 的里面的函数的执行顺序是
先
Scene* HelloWorld::createScene();
再
bool HelloWorld::init();
Scene* HelloWorld::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auro layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; }
可以写成
Scene* HelloWorld::createScene() { // 'scene' is an autorelease object Scene* scene = Scene::create(); // 'layer' is an autorelease object Layer* 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(); Point 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(Point(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(Point::ZERO); this->addChild(menu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label auto label = LabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen label->setPosition(Point(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(Point(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; }
虽然这段很长,不过包含了很多要学习的知识。
从表面上看,这段代码都在讲初始化的那些事。
细心观察,这个和Objective-C 的 init 方法多类似啊,只是不是返回对象。
我们精简一下这段代码的框架
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } //初始化的内容 return true; }
接下来看看里面进行的初始化的内容吧
向导演问了相关舞台的数据
Size visibleSize = Director::getInstance()->getVisibleSize(); Point 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(Point(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(Point::ZERO); this->addChild(menu, 1);
// 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 MenuItemImage* closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object Menu* menu = Menu::create(closeItem, NULL); menu->setPosition(Point::ZERO); this->addChild(menu, 1);
MenuItemImage 类 创建一个对象,放入两张图片,和一个回调函数。
第一张图片是正常状态的,第二张是选择状态时的,回调函数,this 应该是目标和iOS 创建按钮很相似,而区别是没有触发事件的手势设置。
接下来就是设置 MenuItemImage 类 实例的位置
通过 MenuItemImage 类 实例 创建一个 Menu 类的实例。
设置坐标
最后,将这个Menu类的实例加入当前 Layer中
接下来就是创建一个Label 类了。
根据官方发布文档所描述。3.0将采用一个Label 类 来创建不同类型的Label,而且优化了很多性能,这些也是后话了。
// 3. add your codes below... // add a label shows "Hello World" // create and initialize a label auto label = LabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen label->setPosition(Point(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);
创建一个LabelTTF类的实例,参数1是内容,参数2是字体,参数3是字体大小
然后就是设置 这个实例的位置
然后加入层
// add "HelloWorld" splash screen" auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(sprite, 0);
首先用精灵创建一个实例,参数是一张图片。
然后设置精灵的位置。
最后把精灵加入层里,
最后我们看看回调函数吧,当点击按钮时,就会触发这个回调函数,因为已经关联上了。
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 }
这里实现的功能很简单,就是退出应用程序而已。
好了,就这样就结束了。接下来就是详情了。
表面上看,Cocos2d-X 真的不难~~~
呵呵

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is a widely used open source server-side scripting language that can handle all tasks in web development. PHP is widely used in web development, especially for its excellent performance in dynamic data processing, so it is loved and used by many developers. In this article, we will explain the basics of PHP step by step to help beginners from getting started to becoming proficient. 1. Basic syntax PHP is an interpreted language whose code is similar to HTML, CSS and JavaScript. Every PHP statement ends with a semicolon; Note

Go language is a statically typed, compiled language developed by Google. Its concise and efficient features have attracted widespread attention and love from developers. In the process of learning the Go language, mastering the basic knowledge of variables is a crucial step. This article will explain basic knowledge such as the definition, assignment, and type inference of variables in the Go language through specific code examples to help readers better understand and master these knowledge points. In the Go language, you can use the keyword var to define a variable, which is the format of the var variable name variable type.

Basic introduction to PHP: How to use the echo function to output text content. In PHP programming, you often need to output some text content to a web page. In this case, you can use the echo function. This article will introduce how to use the echo function to output text content and provide some sample code. Before starting, first make sure you have installed PHP and configured the running environment. If PHP is not installed yet, you can download the latest stable version from the PHP official website (https://www.php.net).

C language function encyclopedia: from basic to advanced, detailed explanation of how to use functions, specific code examples are required Introduction: C language is a widely used programming language, and its powerful functions and flexibility make it the first choice of many developers. In C language, function is an important concept. It can combine a piece of code into an independent module, improving the reusability and maintainability of the code. This article will introduce the use of C language functions from the basics and gradually advance to help readers master the skills of function writing. 1. Definition and calling of functions in C

cannot. The function of the CREATE statement is to create a table structure, but it cannot append new records. You can use the INSERT statement to append new records. The CREATE statement can be used to create a new table in the database and specify the attributes and constraints of the data columns; however, the newly created table is an empty table and requires the use of the INSERT statement to append new records. The INSERT statement is used to insert one or more rows of tuple data into an existing table in the database.

Calling all C# developers! Microsoft and non-profit organization freeCodeCamp announce the launch of a new global free Basic C# certification. This certification is designed to help developers of all levels learn the basics of C#, a popular programming language used to create a variety of applications, and you can display it in your LinkedIn profile. This certification includes 35 hours of Microsoft Learn training courses and an 80-question exam hosted on freeCodeCamp. This course covers topics such as variables, data types, control structures, and object-oriented programming. “Our Basic C# certification provides just that – proof of your ability to master this versatile

PHP is a widely used server-side scripting language used to develop dynamic websites, web applications, and other Internet services. In the process of developing PHP applications, using functions can help simplify code, improve code reusability, and reduce development costs. This article will introduce the basic usage and advanced usage of PHP functions. 1. Basic usage of PHP functions 1. Define functions In PHP, use the function keyword to define functions, for example: functiongreet($name){

PHP study notes: basic syntax and variable definition In today's Internet era, PHP (Hypertext Preprocessor), as a widely used server scripting language, is favored by more and more developers. This article will introduce you to the basic syntax of PHP and the definition of variables, and provide specific code examples to help beginners better understand and master it. 1. Basic syntax of PHP Markings of PHP code In PHP code, we often use "<?php" and "?&
