Scene and Layer, cocos2d-x for cocos2d-xscene

Source: Internet
Author: User

Scene and Layer, cocos2d-x for cocos2d-xscene

Continue Learning, cocos2d-x


This time we will talk about two classes: Scene and Layer, that is, Scene class and Layer class.

I have heard of this engine when I was studying cocos2d-x. It's just like making movies. There's a Director, a Scene, and a Layer ), and Sprite ).

The general relationship is Taipa ~

Next, let's look at the figure. I'm not going to figure X (blocking poor languages)



-------------------------------------------------- Even the split line, add a small comment ----------------------------------------------------------------------

There is a graph of all types of cocos2d-x, which is made of Xmin. I like it very much,

You can click this URL to download the file. One point is required, which is made by gamecocos2dx. There are some comments in it, which is quite good:

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

If you do not want to download Xmind, you can view the graph in PNG format. On my download page, some notes cannot be displayed:

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


Learning cocos2d-x still need to look at more API,

If the network permits, you can look at the Chinese API: http://cn.cocos2d-x.org/doc/cocos2d-x-3.0/index.html

Of course, it is better to see the English version of: http://www.cocos2d-x.org/reference/native-cpp/V3.2/index.html

At the very least, people are the latest version =. =


If the network is not allowed, there are also offline API versions, only English, which is 3.0:

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

How to Use: Search for the index.html file and open it ~ (* ^__ ^ *)~

-------------------------------------------------- Even the split line, add a small comment ----------------------------------------------------------------------


OK. Let's get down to the truth!

Today, let's take a look at Scene and Layer... This is the second time that I have mentioned o (begin □lead) o)

Scene, which is explained in the API as follows:

Scene is an abstract concept. It is used only as a subclass of Node,

Scene and Node are almost the same. The difference is that the default anchpoint of Scene is in the center of the screen.

{PS: What is the anchor? You can see this http://jingyan.baidu.com/article/a501d80cf003deec630f5e14.html,

In short, the anchor is equivalent to placing a picture on the wall. We only use one nail, and the position of the nail is the anchor}

There are also some, useless barabara ~ (Ignore this sentence)

You can also see this inheritance diagram:


Well, this figure shows the relationship between the parent class and the subclass.

Now, let's give it a try. Just say you don't need to practice fake tricks.

In VS2012, I used to create a new class (this new class can only be created in WIN32, not under the class graph, and can only be added to the Classes by adding new items, XX is dead, I hope there are some good ways for Daniel to guide me. Thank you !)


Open a new project,

Add WelcomScene class (. h and. cpp)




The added class is a blank class, and the content in it needs to be filled in by ourselves.

Edit the header file first (I copied HelloWorldScene. h and changed all the class names to my own WelcomeScene)

# Include "cocos2d. h "class WelcomeScene: public cocos2d: Layer {public: // create a scenario function static cocos2d: Scene * createScene (); // It is a macro-defined CREATE_FUNC (WelcomeScene );};

CREATE_FUNC is a macro definition, which is equivalent:

Static WelcomeScene * create () {WelcomeScene * pRet = new WelcomeScene (); if (pRet & pRet-> init () {pRet-> autorelease (); // automatically release resources} else {delete pRet; pRet = NULL;} return pRet ;}

Well, next we will define the. cpp file:

# Include "WelcomeScene. h" USING_NS_CC; // create the scenario function Scene * WelcomeScene: createScene () {auto scene = Scene: create (); return scene ;}

Contains the WelcomeScene header file,

USING_NS_CC is the namespace for cocos2d applications,

Some do not need to write cocos2d.


Now let's take a look at our work ~.~

Jump to AppDelegate. cpp

Bool AppDelegate: applicationDidFinishLaunching () function

Create a scenario in the last row and change it:

Auto scene = WelcomeScene: createScene ();

(By the way, do not forget to add the header file of WelcomeScene in AppDelegate)

Then click Run ~.~


When ~ When ~ Dangdang! Dark!

That's right!

As we mentioned above, we just set up a scenario, but the scenario is safe!



So the next step is Layer ~

Layer description in API:

Layer is a subclass of the Node class, which implements the touch screen proxy protocol.

That is to say, the implementation of the touch screen is on the Layer.

Continue. Do not speak without a picture ~.~


This is more shocking than Scene .. Hey

In short, Layer is a Layer, and the top figure also shows:

Scene consists of two layers and menus: interfaces and menus.

Next, we will try to figure out how to make it swollen in our project ~.~


Add a function in WelcomeScene. h:

 virtual bool init();  

Then, edit the function in. cpp:

Bool WelcomeScene: init () {// determines whether Initialization is possible. If not, return directly and exit. If (! Layer: init () {return false;} // obtain the screen length and width. visibleSize = Director: getInstance ()-> getVisibleSize (); Point origin = Director :: getInstance ()-> getVisibleOrigin (); // create an Genie and load an image 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:

① The image added here is your own search. It is best to find a PNG format.

Save the image to the Resource Directory in the project folder, return to VS2012, right-click Resource, select Add existing item, and add the image to it. (It looks like, you can directly call the image by adding it to the Resource Directory .)

② Here, addChild is to place the genie in this layer. The first parameter is the sprite, and the second parameter is the layer, for example, if you add 1, you can overwrite the value of 0. If the same layer is 0, the added layer overwrites the previous one.



Then, modify the createScene function:

// Create the scenario function Scene * WelcomeScene: createScene () {auto scene = Scene: create (); auto layer = WelcomeScene: create (); scene-> addChild (layer); return scene ;}
When creating a scenario, we need to add a layer. To add this layer, we need to call the create function in WelcomeScene, which is the previously edited function.


In this way, run it and our stuff will come out ~.~


I have a slight problem with the display size, but I have not adjusted it. Amount .... This is not the key. The key is it.


Next, let's take a look at some other things of HelloWorldScene:

Add a text,

Add a button,

Let's just get it done.

Add things in three steps:

1. Add

2. Set the position to be added. If a callback function exists, write the function name.

3. Add to the scene


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

Go to bed.



How do I add two layers in the cocos2d-iphone? scene addchild: layer1 z: 1 then scene addchild: layer2 z: 2

You use [director _ pushScene: [HelloWorldLayer scene];
Now you have the HelloWorldLayer + scene method, but there is nothing in the scenario.
Because HelloWorldLayer inherits from CCLayer, this layer is inserted into scene by convention.
HelloWorldLayer * layer = [HelloWorldLayer node];
[Scene addChild: layer z: 1];
You can understand that this class generates a scenario and adds an empty layer for free.
You want to add anything in this layer and write it in init.
Therefore, in your scenario, you can add a new layer, but pay attention to setting the z value when the hierarchical link addchild is used.

Add a new Layer in the top Layer of the cocos2d-x, how to shield all the touch of other layers

You can set the swallowsTouches of the TouchHandle of this layer to YES, so that the touch is swallowed, and other layers will no longer respond, just like the CCMenuItem method. You can try it.
Hope to help you.

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.