Cocos2d-x tutorial (29)-3. x version mask layer achieves fishing talents scroll digital dial, cocos2d-x-3.x

Source: Internet
Author: User

Cocos2d-x tutorial (29)-3. x version mask layer achieves fishing talents scroll digital dial, cocos2d-x-3.x

Welcome to Cocos2d-x chat group: 193411763

Reprinted please indicate the original source: http://blog.csdn.net/u012945598/article/details/38340845

Source code: http://download.csdn.net/detail/u012945598/7704725


Previously in the eighth tutorial explains the mask layer to achieve fishing Daren scroll digital dial (article link: http://blog.csdn.net/u012945598/article/details/17049419), then many friends asked 3. in Version x, the mask is invalid, so the method cannot be used. In fact, the method can still be used, but the code needs to be slightly modified. I will re-paste the IMPLEMENTATION OF THE ScrollLabel class here. You can replace the copy directly.


ScrollLabel. h file

# Include <iostream>

# Include "cocos2d. h"

# Include <stdlib. h>

USING_NS_CC;

Class ScrollLabel: publicNode {

Public:

Virtual bool init ();

CREATE_FUNC (ScrollLabel );

Std: vector <Label *> labels;/* array used to save the label */

Node * visibleNode;/* Create a Node and add the label to the Node to facilitate moving */

Void setNumber (int var, bool up);/* sets the method to which the number is scrolled. The var parameter is the number to be scrolled */

/* ----- Modify content -------*/

/* Reload the visit method in CCNode. The draw method is not used here */

Virtual void visit (Renderer * renderer, constkmMat4 & parentTransform, bool parentTransformUpdated );

Void onAfterVisitScissor ();

Void onBeforeVisitScissor ();

Public:

CustomCommand _ beforevisit1_scissor;

CustomCommand _ aftervisit1_scissor;

};

ScrollLabel. cpp File


# Include "ScrollLabel. h"

Bool ScrollLabel: init (){

VisibleNode = Node: create ();

This-> addChild (visibleNode );

/* Create ten tags, numbers 0-9 */

For (int I = 0; I <10; I ++ ){

Char str [2];

Str [0] = '0' + I;

Str [1] = '\ 0 ';


Label * single = Label: create ();

Single-> setSystemFontSize (20.0 );

Single-> setColor (Color3B (, 0); // you can specify the font color of a tag.

Single-> setTag (I );

Single-> setString (str );

Single-> setAnchorPoint (Point (0, 0); // set the anchorpoint to 0, 0 to facilitate coordinate setting

Single-> setPosition (Point (0, 20 * I ));

VisibleNode-> addChild (single );

Labels. push_back (single); // store tags in the array

}

Return true;

}


Void ScrollLabel: setNumber (int var, bool up = true ){

This-> stopAllActions ();

/* Obtain the coordinates of the label to be moved */

Label * label = labels [var];

Point moveToPosition = Point (visibleNode-> getPositionX (),-(label-> getPositionY ()));

/* Create an action and move it to the location of the label */

MoveTo * moveAction = MoveTo: create (5, moveToPosition );

VisibleNode-> runAction (moveAction );

}

Void ScrollLabel: visit (Renderer * renderer, constkmMat4 & parentTransform, bool parentTransformUpdated ){

_ Beforevisit1_scissor. init (_ globalZOrder );

_ Beforevisit1_scissor. func = CC_CALLBACK_0 (ScrollLabel: onBeforeVisitScissor, this );

Renderer-> addCommand (& _ beforevisit?scissor );

Node: visit (renderer, parentTransform, parentTransformUpdated );

_ Aftervisit1_scissor. init (_ globalZOrder );

_ Aftervisit1_scissor. func = CC_CALLBACK_0 (ScrollLabel: onAfterVisitScissor, this );

Renderer-> addCommand (& _ aftervisit?scissor );

}

Void ScrollLabel: onBeforeVisitScissor ()

{

Point pos = Point (0, 0 );

Pos = visibleNode-> getParent ()-> convertToWorldSpace (pos );

Rect clippingRect = Rect (pos. x, pos. y, 20, 20 );

GlEnable (GL_SCISSOR_TEST );

Auto glview = Director: getInstance ()-> getOpenGLView ();

Glview-> setScissorInPoints (clippingRect. origin. x, clippingRect. origin. y, clippingRect. size. width, clippingRect. size. height );

}

Void ScrollLabel: onAfterVisitScissor ()

{

GlDisable (GL_SCISSOR_TEST );

}

The usage is the same as before:

ScrollLabel * m_scroll = ScrollLabel: create ();

M_scroll-> setPosition (Point (568,320 ));

This-> addChild (m_scroll );

M_scroll-> setNumber (5, true );

The last one is attached:





Related Article

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.