Cocos2d Getting Started--1--related properties or code

Source: Internet
Author: User
Tags addchild

Cocos2d vision:cocos2d-x-3.8.1

Tall buildings, from the tired soil. For a game framework of learning, in fact, the use of the framework function accumulation, learned how to use it on the basis of the Cocos2d game engine to provide various functions, and flexible use,

and learn to check the API documentation provided by COCOS2D official. Believe that you can also open out their favorite or let others envy of the game.

Directory:

1> understand the simple and practical drawnode of origin and Visiblesize and Cocos2d's basic painting class

2> know Cclog ()

3> about the use of schedule

4> about Contentsize and anchor

5> about converting local coordinates to world coordinates

===================================================================================================

1, understanding Origin and Visiblesize and cocos2d basic painting Class Dranode simple and practical
Origin coordinates of the visible area, visiblesize the dimensions of the viewable area

1     Size visiblesize = director::getinstance ()->getvisiblesize (); 2     Vec2 origin = Director::getinstance ()- >getvisibleorigin ();

So if you want to get the location of the viewable area of the screen and the relative width, you can set it on the basis of these two properties.

Instance code:

1      //1, Drawnode object is the base object in Cocos2d, can be used to draw some graphics 2      auto rect = Drawnode::create (); 3      //rect->drawrect ( Const COCOS2D::VEC2 &origin, const COCOS2D::VEC2 &destination, const cocos2d::color4f &color) 4      // The first argument is the starting point of the drawing, and the second argument is the distance drawn. The third parameter is the color, 4 F is the Rgba four color value, and the F floating point number is set, each floating-point range is between (0,1) 5      rect-DrawRect (VEC2 (0,0), VEC2 (), color4f (1.0, 0, 0, 1.0)); 6      addChild (rect); 7       8     rect-setposition (origin.x + visiblesize.width/2-50, 9                         origin.y + VISIBLESIZE.HEIGHT/2-50);//Set the rectangle in the middle of the viewable area//    rect-setposition (ORIGIN.X,ORIGIN.Y);//Set start position in the visual area of the start coordinate

Effect:

2, recognize the Cclog print output.

1 Cclog ("x:%f,y:%f\n", ORIGIN.X,ORIGIN.Y);

Similar to the scanf ("%d", number) statements in the C language. However, Cclog does not have to add a newline symbol "\ n", and Cclog prints the string, and the line wrapping is automatically printed.

  3, about the use of schedule
  1 Auto rect = Drawnode::create (); 2 rect--DrawRect (VEC2 (0,0), VEC2 (100, , color4f (1.0, 0, 0, 1.0)); 3 AddChild (rect); 4 rect-setposition (origin.x + VISIBLESIZE.WIDTH/2, 5 origin.y + VISIBLESIZE.HEIGHT/2);// Sets the rectangle in the middle of the viewable area 6 Auto dot = drawnode::create (); 7 dot-Drawdot (VEC2 (0,0), 4, color4f (1.0, 1.0, 1.0, 1.0)); 8 AddChild (dot); 9 dot-setposition (origin.x + VISIBLESIZE.WIDTH/2, ORIGIN.Y + VISIBLESIZE.HEIGHT/2); 10 11//We need to keep the rectangle spinning , Practical//schedule (< #const std::function<void (float) > &callback#>, < #const std::string &key#> ;) 13//The first parameter is a function (here practical ...) The expression quickly defines this function and gives the float parameter, [] is the capture list, we capture Dot and rect two objects), 14///The second parameter is the name of the operation called in the timesheet, we can cancel the continuous call by this name. Schedule ([Dot,rect] (float f) {+1 rect, setrotation (Rect->getrotation ());//optional This rectangle, "Test"); 

  4. About Contentsize and anchorOnline says contentsize is the logical size meaning.
1     Auto rect = Drawnode::create (); 2     rect--DrawRect (VEC2 (0,0), VEC2 (+), color4f (1.0, 0, 0, 1.0)); 3
    
     addchild (rect); 4     Rect-setposition (origin.x + VISIBLESIZE.WIDTH/2, 5                         origin.y + VISIBLESIZE.HEIGHT/2); 6     Auto dot = Dra Wnode::create (); 7     dot-Drawdot (VEC2 (0,0), 4, color4f (1.0, 1.0, 1.0, 1.0)), 8     addChild (dot), 9     dot-setposition (orig In.x + VISIBLESIZE.WIDTH/2, ORIGIN.Y + VISIBLESIZE.HEIGHT/2);     Now you want to place this rect in the middle of the screen,     rect. Setcontentsize (Size (100,100));     rect, Setanchorpoint (VEC2 (0.5, 0.5));//This setting anchor, the parameter is a proportional value,     schedule ([Dot,rect] (float f) {         setrotation (rect->getrotation () +1)     , "Test");     return true;
    

  5. Convert local coordinates to world coordinates.
  1 Auto rect = Drawnode::create (); 2 rect--DrawRect (VEC2 (0,0), VEC2 (100, , color4f (1.0, 0, 0, 1.0)); 3 AddChild (rect); 4 rect-setposition (origin.x + VISIBLESIZE.WIDTH/2, 5 origin.y + VISIBLESIZE.HEIGHT/2); 6 Auto dot = drawnode::create (); 7 dot-Drawdot (VEC2 (0,0), 4, color4f (1.0, 1.0, 1.0, 1.0)); 8 Rect->addchild (dot);//Here is the previous code instance, originally the scene add (dot), now instead of Rect->add (dot) 9 dot-and SetPosition (VEC2 (1, 1));//Ben Ground Space 10 11//The RECT is now placed in the middle of the screen rect--Setcontentsize (Size (100,100)); Rect, Setanchorpoint (Vec2 (0.5, 0.5)); /This sets the anchor point, the parameter is a proportional value, schedule ([Dot,rect] (float f) {+ rect, setrotation (rect->getrotation () + 1) Auto P = dot->converttonodespace (Vec2 (0, 0));//local coordinates are converted to world coordinates cclog ("%f,%f", p.x,p.y); St "); return true;  

Effect:

  

Cocos2d Getting Started--1--related properties or code

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.