Cocos2d-x study notes (3.2) Layer

1 Introduction

Layer directly inherited Node. Similar in concept Layer Ps layer may also be understood as a transparent glass. Scene Ps in the one similar image can also be understood piled glass box.

Layer can receive touch events, keyboard events, accelerators event.

Only one director class once Scene display, such as the presence Login scene, the scene menu, game scene, a scene at a time can only be run.

A Scene can contain multiple Layer, for example, the game may contain scenes figure layer, the background layer, layer navigation menu bar. Each Layer contains a number of elements.

Each element has its own action.

2. Layer and touch

Layer in touch associated members:

    BOOL _touchEnabled; // whether to enable touch 
    EventListener * _touchListener; // touch the listener 
    Touch :: DispatchMode _touchMode; // touch type single-point or multi-point 
    BOOL _swallowsTouches; // engulf

Layer There are two kinds of callbacks touch a total of eight touch the listener, it can be inherited Layer subclass rewrite.

void setTouchEnabled(bool value)

This method modifies whether layer can receive touch events, the main logic is that?:

  1. Parameters and _touchEnabled not the same as the _touchEnabled modify parameters.
  2. If the modification is true, it enables the touch event is received, _touchMode create the appropriate type of single-point touch / multi-touch monitors, set four callback function, and the new listener assigned _touchListener.
  3. If the modification is false, then close the touch event sink, remove _touchListener from the dispenser in the event, and the _touchListener empty.

void setTouchMode(Touch::DispatchMode mode)

void setSwallowsTouches(bool swallowsTouches)

The two method calls to the above methods. Methods setTouchMode two years, when enabled, touch, setSwallowsTouches method, in the modified variables to be set, it will perform:

            setTouchEnabled(false);
            setTouchEnabled(true);

The first line can delete the current listener, the second line can reset the listener according to _touchMode.

3. LayerColor

Color layer is directly inherited the layer Layer, color mixing BlendProtocol.

Color layer on the base layer, added: opacity, the RGB colors, color mixing.

There are three ways to create create a color layer. Parameter may be provided in the window size and RGBA values:

LayerColor * LayerColor::create(const Color4B& color, GLfloat width, GLfloat height)

When no setting create parameters, default value, i.e., a transparent background parent Layer:

    Size s = Director::getInstance()->getWinSize();
    return initWithColor(Color4B(0,0,0,0), s.width, s.height);

Constructor was installed with the default color mixing:

LayerColor::LayerColor()
{
    // default blend function
    _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
}

You can modify the properties of the color layer:

void changeWidthAndHeight(GLfloat w ,GLfloat h)
void changeWidth(GLfloat w)
void changeHeight(GLfloat h)

BlendFunc& getBlendFunc()void setBlendFunc(const BlendFunc& blendFunc)void Node::setOpacity(GLubyte opacity)
void Node::setColor(const Color3B& color)

4.  LayerGradient

Color gradient layer LayerGradient directly inherited the color layer LayerColor.

In addition RGBA color layer, color mixing, graded layer as well as these properties: gradient direction, from the end of the color gradient, the gradient from the end of transparency.

Is converted from the color gradient to _startColor _endColor, the gradient direction vector Vec2 _alongVector.

There are three ways to create create. You can take parameters from the end of the color gradient and the direction vector to achieve:

LayerGradient* LayerGradient::create(const Color4B& start, const Color4B& end, const Vec2& v)

When the parameter is not provided create, from the end of the default color is black, the direction from top to bottom, from the transparency of the end 255, an effect similar to layer Layer:

initWithColor(Color4B(0, 0, 0, 255), Color4B(0, 0, 0, 255));
initWithColor(start, end, Vec2(0, -1));

5. LayerRadialGradient

Color conversion achieved radially.

6. LayerMultiplex

You can manage a plurality of layers.

Container Vector <Layer *> _layers storage layer, the recording layer with _enabledLayer currently last executed.

With this-> addChild (_layers.at (n)) the method of a layer to the current scene.

create ways to pass more than one layer:

LayerMultiplex* LayerMultiplex::createWithArray(const Vector<Layer*>& arrayOfLayers)
LayerMultiplex * LayerMultiplex::create(Layer * layer, ...)

Can switch the current layer, add a layer:

LayerMultiplex::switchTo(int n)
LayerMultiplex::addLayer(Layer* layer)

-

Guess you like

Origin www.cnblogs.com/deepcho/p/cocos2dx-layer.html