Objective
The previous article introduced the HelloWorld program, this time we will introduce a very important element-the Genie (Sprites).
What is a genie?
The Genie (Sprites) is a core element of any game. We need them. In the game we often like to use it for playing and play characters. It can move, rotate, scale, perform animations, and accept other transformations. The COCOS2DX sprite is made up of texure,frame and animation and is rendered by Openes. The simple process can be described as: using texture2d loading pictures, you can use texture2d to generate corresponding Spriteframe (Sprite frame), add spriteframe to animation to generate animation data, Use animation to generate animate (which is the final animation action) and then perform this action with a sprite. Sometimes, the genie can play the game background, can be the protagonist in the game, can also be a monster without evil. In fact, it's a picture, but its performance overhead is often used to set most of the graphics resources in the game.
Creating sprites
Several ways to create sprites:
Auto Sprite = sprite::create ("Helloworld.png"); This->addchild (sprite,0);
- Use textures to create sprites
Sometimes, in order to make the picture resource smaller, it will create different ccsprite according to a picture, which need to load the image to the image texture's cache first through Cctexturecache, then get the Cctexture2d object of this picture from the cache, According to this object to create the Ccsprite, the code is as follows:
Auto sprite1 = Sprite::createwithtexture (Texturecache::getinstance ()->addimage ("Helloworld.png");this-> AddChild (sprite1, 0);
- Use sprite frames to create sprites
use the name of a frame in the frame cache to claim an object that is suitable for plist packaged files
Ccspriteframecache::sharedspriteframecache ()->addspriteframeswithfile ("Test.plist"); Auto Sprite2=Sprite:: Createwithspriteframename ("Helloworld.png"); This->addchild (sprite2, 0);
The basic process for implementing sprite display in COCOS2DX is as follows:
Create a Sceneauto scene = Scene::create ();//create layer Auto layer = helloworld::create ();//Add layer to the scene scene->addchild (layer);// Create a Sprite Auto Sprite = sprite::create ("Helloworld.png");//Add sprites to the layer Layer->addchild (Sprite, 0)
Let's start dancing!
Before introducing the action, the concept of a coordinate system in a cocos2d-x is described: In the scene with the lower left corner as the coordinate origin, from left to right for the x direction, from the bottom to the top for the y direction, understand this, let's briefly introduce some of the common members of the action genealogy.
- MoveTo (Move the node object to position x, Y. X, y is an absolute coordinate, changing its value by modifying its Position property)
/* Create Sprite */sprite* sprite=sprite::create ("test.png");/* Create an action to move to the x:250,y:150 point after 0.9 seconds */moveto* MoveTo = Moveto::create ( 0.9f, point (250, 150));/* Perform action */sprite->runaction (MOVETO);
- Scaleto (Zoom action)
/* Create Sprite */sprite* sprite=sprite::create ("test.png");/* Create an action of twice times width and height after 3 seconds */scaleto* Scaleto = Scaleto::create (3.0f , 2.0f, 2.0f);/* Perform action */sprite->runaction (Scaleto);
- Bezierto (Bézier curve action)
/* Create sprite */sprite* sprite = sprite::create ("Sprite.png"); Sprite->setposition (point (+)); This->addchild ( Sprite, 1, tag_sprite);/* Create Bézier curve configuration */ccbezierconfig bezier;bezier.controlpoint_1 = point (100, 0); Trough bias Value bezier.controlpoint_2 = point (200, 250);//crest bias value Bezier.endposition = points (300, 0);//End of action//Create Ccbezierto Action Object */ bezierto* Bezierto = bezierto::create (4.0f, Bézier);/* Perform action */sprite->runaction (Bezierto);this-> Scheduleupdate ();
- RepeatForever (repeat)
/* Create a sprite */sprite* sprite = sprite::create ("Sprite.png"); Sprite->setposition (Point (VISIBLESIZE.WIDTH/2, VISIBLESIZE.HEIGHT/2) This->addchild (sprite);/* Create a Ccjumpby Action object */jumpby* jumpby = Jumpby::create (3.0f, point ( 50, 1), 100, 1);/* Create a permanent repeat action with Jumpby as parameter */repeatforever* repeatforeveraction = Repeatforever::create (JUMPBY);
Sprite->runaction (repeatforeveraction);
Sequence (combined action, executed in sequence)
/* Create a sprite */sprite* sprite = sprite::create ("Sprite.png"); Sprite->setposition (Point (VISIBLESIZE.WIDTH/2, VISIBLESIZE.HEIGHT/2) This->addchild (sprite);/* Create a Move Action object */moveby* moveby = Moveby::create (2.2f, point (40, 20)) ;/* Create a Bounce Action object, bounce height 100, bounce times 5 */jumpby* jumpby = Jumpby::create (3.0f, point (50, 1), 100, 5);/* Create a Rotation action object */rotateby* rotat Eby = Rotateby::create (2.5f, 220, 10);/* Creates a combined action object that connects all actions */action* actions = Sequence::create (Moveby, Jumpby, Rotateby, NULL); sprite->runaction (actions);
In general, each action is in pairs, there are corresponding to and by, the two have absolute and relative points. Finally, a family tree of action families:
Windows Development Cocos2d-x Series (3)-Dance your elves