Scene and layer of cocos2d-x

Source: Internet
Author: User
Tags addchild

*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************


Continue to learn slowly. Cocos2d-x


This time, say two classes, scene and layer. That is, scene classes and layer classes.

Once learned cocos2d-x, this engine is the same as the film, there are directors, a large background (Scene), and the background of the small modifier (Layer), as well as the character (Sprite).

The general relationship is Jiangzi ~

Another picture, no picture I say an X (shielding bad language)



--------------------------------------------------I am a cutting line. Add a little Stare----------------------------------------------------------------------

Here is a diagram of a relationship that cocos2d-x all classes. It was made with xmin, and I looked great.

Be able to poke this URL to download. 1 points required. Produced by GAMECOCOS2DX. There are some other stares that are pretty good:

http://download.csdn.net/detail/gamecocos2dx/7281687

Suppose you do not want to download xmind, can look at the PNG format of the diagram, on my download page, but some gaze can not be displayed:

http://download.csdn.net/detail/lx417147512/7657073


Learn cocos2d-x still need to see more API,

Assume. Network agree, can see Chinese api:http://cn.cocos2d-x.org/doc/cocos2d-x-3.0/index.html

Of course, you might as well look at the English version: http://www.cocos2d-x.org/reference/native-cpp/V3.2/index.html

At least, people are the latest version of the number =. =


Assume. The network does not agree, there is also an offline version number API. In English only, it is 3.0:

http://download.csdn.net/detail/lx417147512/7656953

Usage: After decompression, search inside index.html file. Open it can ~ (*^__^*) ~

--------------------------------------------------I'm a cutting line, Add a little Stare----------------------------------------------------------------------


OK, here we are.

Today, come and learn the scene and layer (the amount ... This is the second time that O (╯-╰) o)

Scene, which is explained in the API:

Scene is an abstract concept that only acts as a subclass of node,

Scene and node are almost the same, unlike the scene's default anchor point in the center of the screen.

{Ps:what is an anchor point? Can see this http://jingyan.baidu.com/article/a501d80cf003deec630f5e14.html,

Simply put, the anchor is the equivalent of a picture we're going to set on the wall, we just use a nail, the position of the nail is the anchor point}

Other, useless blah ~ (ignore this sentence is OK)

You can also see this inheritance chart:


Well, this diagram shows the parent class and subclass relationships.

Now, come and try. Light says do not practice false bashi.

I'm using the VS2012, create a new class (I'm a new class.) Can only be built in WIN32, not under the class diagram, can only be added to the classes by adding new items. XX was dead. I hope there is a good way for Daniel to guide me. Appreciate! )


Open a new project,

Join the Welcomscene class (. h and. cpp)




This added class is a blank class, the contents of which need to be filled out by ourselves.

First edit the header file (I will HelloWorldScene.h copy of the past, will use the name of the class to change to their own welcomescene)

#include "cocos2d.h" class welcomescene:public cocos2d::layer{public://create Scene function    static cocos2d::scene* Createscene ();    is a macro definition create_func (welcomescene);};

Here Create_func is a macro definition. Equivalent to:

Static welcomescene* Create () {        welcomescene* pRet = new Welcomescene ();        if (PRet && pret->init ()) {            pret->autorelease ();   Voluntarily release resources        }else{            Delete pRet;            PRet = NULL;        }           return pRet;  }  

Well, next define the. cpp file:

#include "WelcomeScene.h" using_ns_cc;//create Scene function scene* Welcomescene::createscene () {Auto scene = Scene::create (); return Scene;}

Include the Welcomescene header file.

USING_NS_CC is the namespace of the application cocos2d,

Just some don't need to write cocos2d: ".


And now you can look at our work. ~

Jump to AppDelegate.cpp

BOOL Appdelegate::applicationdidfinishlaunching () function

The penultimate line creates a scene that changes to:

Auto scene = Welcomescene::createscene ();

(yes, don't forget to add Welcomescene's header file in appdelegate)

Then click Execute ~. ~


When ~ when ~ when! It's dark!

Yes, that's right!

We also said that we just built a scene, but the scene is not a thing ah!



So. Next is the layer thing ~

The layer is described in the API:

The layer is a subclass of the node class. It implements the touch-screen proxy protocol.

In other words, the realization of the touch screen of these things, is on the layer slightly.

Go on, no pictures don't talk ~. ~


This is a bit more shocking than the scene. Hey

All in all, a layer is one. The top diagram also shows:

Scene consists of two sections, Layer and menu, which are interfaces and menus.

Next, in our project to achieve a swelling of the ~. ~


Add a function to the WelcomeScene.h:

virtual BOOL init ();  

Then, edit this function in. cpp:

BOOL Welcomescene::init () {//inference can initialize, if not directly return, exit. if (! Layer::init ())    {        return false;    } Gets the long width of the screen size visiblesize = Director::getinstance ()->getvisiblesize ();    Point origin = Director::getinstance ()->getvisibleorigin ();//Create a Sprite, load a picture    auto sprite = Sprite::create (" Backg.png ");    Sprite->setposition (Point (VISIBLESIZE.WIDTH/2 + origin.x, VISIBLESIZE.HEIGHT/2 + origin.y));    This->addchild (Sprite, 0); return true;}


Ps:

① here to add the image is to find their own, it is best to find a PNG format.

The picture exists under the Resource folder under the project folder. Back to VS2012 right click on resource. Choose to join the existing item, add the image to be OK (seemingly, directly add the image to the resource folder can be directly called. )

② here, the Addchild, is to put this wizard in this layer. The first parameter is which wizard to put, the second one is the first layer, for example, add 1 can fill 0 of the cover off. Assume the same layer. is the same as 0. After the increase will overwrite the front.



Then change the Createscene function to:

Create Scene function scene* Welcomescene::createscene () {Auto scene = Scene::create (); Auto layer = Welcomescene::create (); scene- >addchild (layer); return scene;}
Indicates that we want to add a layer when creating the scene. Adding this layer to call the CREATE function in Welcomescene is the function we edited earlier.


Such Take a look. Our stuff is out. ~


I have this, the size of the display slightly problematic, no adjustment.

Amount

。。 This is not the key, the key is that it comes out.


Next. Take a look at some of Helloworldscene's other things:

Add a text.

Add a button.

These are good to get.

Add something. is three steps:

1. Join

2. Set the increment position, assuming that there is a callback function, to write out the function name

3. Add to the scene


Well, it's here. (⊙o⊙). It's 12:30 (midnight) ....

Go to bed, ⊙﹏⊙ B.


*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************

Scene and layer of cocos2d-x

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.