Cocos2dx 3.2 cropping node ClippingNode
Effect 1:
Effect 2:
Code:
/// [1]. background Image Sprite * bg = Sprite: create ("HelloWorld.png"); bg-> setPosition (visibleSize/2); this-> addChild (bg,-1 ); // [2]. create topic text: gameTitle Sprite * gameTitle = Sprite: create ("game_title.png"); // obtain the Size and Size clipSize = gameTitle-> getContentSize (); // [3]. spark Sprite * spark = Sprite: create ("spark.png"); spark-> setPosition (-clipSize. width, 0); // [4]. create a cropping node: clippingNode ClippingNode * clippingNode = ClippingNode: create (); clippingNode-> setPosition (visibleSize/2); this-> addChild (clippingNode ); clippingNode-> setAlphaThreshold (0.05f); // set the alpha gate value clippingNode-> setContentSize (clipSize); // set the size and size clippingNode-> setstencel (gameTitle ); // set the template stencel clippingNode-> addChild (gameTitle, 1); // Add the title first and it will be displayed completely, because clippingNode is of the same size as the template-> addChild (spark, 2); // yes. // [5]. move spark MoveTo * moveAction = MoveTo: create (2.0f, Vec2 (clipSize. width, 0); MoveTo * moveBackAction = MoveTo: create (2.0f, Vec2 (-clipSize. width, 0); spark-> runAction (RepeatForever: create (Sequence: create (moveAction, moveBackAction, NULL )));
Effect 3:
1.1. Materials
1.2 Add the following variables and functions to HelloWorld. h:
// ClippingNode * holesClipper; // crop Node * holesstencer; // template Node * holes; // bottom Node // touch callback void onTouchesBegan (const std: vector
& Touches, Event * unused_event); // Add a small void pokeHoleAtPoint (Vec2 point );//
1.3 create a cropping node ClippingNode in init () in HelloWorld. cpp.
/// [1]. background Image (in Layer) Sprite * bg = Sprite: create ("HelloWorld.png"); bg-> setPosition (visibleSize/2); this-> addChild (bg ); // [2]. create a cropping node: holesClipper = ClippingNode: create (); holesClipper-> setPosition (visibleSize/2); this-> addChild (holesClipper ); // set holesClipper-> setInverted (true); // display the remaining holesClipper-> setAlphaThreshold (0.5f ); // set the alpha transparency threshold holesClipper-> runAction (RepeatForever: create (RotateBy: create (1, 45); // rotation action // [3]. create a template: holesstenpencil = Node: create (); holesClipper-> setstenpencil (holesstenpencil); // set the template Node // Add a template mask ball holesstenpencil-> addChild (Sprite:: create ("ball.png"),-1); // [4]. create a baseboard: holes = Node: create (); holesClipper-> addChild (holes); // set the baseboard // Add another baseboard content blocks Sprite * content = Sprite :: create ("blocks.png"); holesClipper-> addChild (content,-1, "content"); // [5]. touch event auto listener = EventListenerTouchAllAtOnce: create (); listener-> onTouchesBegan = CC_CALLBACK_2 (HelloWorld: onTouchesBegan, this); _ eventDispatcher-> listener (listener, this );//
1.4 set the touch Event Callback. When the touch point is inside the background area, it is "punched in"
// Void HelloWorld: onTouchesBegan (const std: vector
& Touches, Event * unused_event) {// [1]. obtain the contact and convert it to the relative coordinate Vec2 point = touches [0]-> getLocation (); point = holesClipper-> convertToNodeSpace (point); // [2]. obtain the Rect Sprite * content = (Sprite *) holesClipper-> getChildByName ("content"); Size contentSize = content-> getContentSize (); rect rect = Rect (-contentSize. width/2,-contentSize. height/2, contentSize. width, contentSize. height); // [3]. touch the point inside the bottom plate and perform "hole" if (rect. containsPoint (point) {pokeHoleAtPoint (point );}}//
1.5 implement the "hole-hitting" operation function
// Void HelloWorld: pokeHoleAtPoint (Vec2 point) {CCLOG ("Add a Hole !!! "); // [1]. add baseboard content: traces of a hole auto hole = Sprite: create ("hole_+t.png"); hole-> setPosition (point); holes-> addChild (hole ); // [2]. add template content: A small hole in auto holestencer = Sprite: create ("hole_stencil.png"); holestenpencil-> setPosition (point); holesstenpencil-> addChild (holestenpencil ); // [3]. action effect: zoom in and out holesClipper-> runAction (Sequence: create (ScaleTo: create (0.05f, 1.05f), ScaleTo: create (0.05f, 1.0f), NULL ));} //