Cocos2d-x 3.6 version connection start Interface
In the previous section, we talked about a loading animation interface. In fact, there is no loading resource.
How to load resources and how to pre-load resources.
Directly Add code
// Create an animation loading-> runAction (RepeatForever: create (Sequence: create (fadeIn, fadeIn-> reverse (), NULL ))); // loading resources {// frame cache auto spriteFrameCache = SpriteFrameCache: getInstance (); // texture cache auto textureCache = Director: getInstance ()-> getTextureCache (); textureCache-> addImage (s_backgound); textureCache-> addImage (container); textureCache-> container (container); textureCache-> addImage (container ); textureCache-> addImage (s_beach_adornment); textureCache-> addImage (s_island); spriteFrameCache-> loads (s_boat_plist); // load the sound resource SimpleAudioEngine: getInstance () -> preloadBackgroundMusic (response); SimpleAudioEngine: getInstance ()-> preloadBackgroundMusic (s_music_class); SimpleAudioEngine: getInstance () -> preloadBackgroundMusic (audio); SimpleAudioEngine: getInstance ()-> preloadEffect (s_music_2); SimpleAudioEngine: getInstance ()-> preloadEffect (s_music_3); SimpleAudioEngine: getInstance () -> preloadEffect (s_music_4); SimpleAudioEngine: getInstance ()-> preloadEffect (effect); SimpleAudioEngine: getInstance ()-> preloadEffect (s_music_14); SimpleAudioEngine: getInstance () -> preloadEffect (s_music_15); SimpleAudioEngine: getInstance ()-> preloadEffect (s_music_16); SimpleAudioEngine: getInstance ()-> preloadEffect (s_music_19 );}
This section of the preload resource is written in the init () function, followed by the code described in the previous chapter.
We can see that the loaded resources here are synchronously loaded. After init, resources are loaded one by one.
You can set a time point at the start of loading and a time point at the end of loading to calculate the time required to load resources.
Obtains the system time, in seconds.
utils::gettime();
The loading of resources is fast, and the loading is completed in less than one second.
After loading, you need to enter the scenario, that is, the start scenario.
How can we implement this mechanism.
scheduleOnce(SEL_SCHEDULE(&StartGame::initUi), 2);
After loading, a user-defined scheduler is directly used to call a function, initUi, with a latency of 2 seconds.
Simple and crude.
In this initUi () function, I will implement the scenario before the game starts. There is probably a sea background, several clouds, reflections, islands, ships, etc.
Void StartGame: initUi (float t) {removeAllChildren (); auto textureCache = ctor: getInstance ()-> getTextureCache (); auto spriteFrameCache = SpriteFrameCache: getInstance (); {// loading background auto background = Sprite: createWithTexture (textureCache-> getTextureForKey (s_backgound); background-> setPosition (wSize. width/2, wSize. height/2); addChild (background) ;}{// 1 auto cloud1 = Sprite: createWithTexture (textureCache-> getTextureForKey (s_backgound_cloud1); // set the anchor, cloud1-> setAnchorPoint (Vec2 (0, 0) in the lower left corner; cloud1-> setPosition (0, wSize. height-cloud1-> getContentSize (). height); addChild (cloud1); // auto cloudShadow = Sprite: createWithTexture (textureCache-> getTextureForKey (s_backgound_cloud1); cloudShadow-> setAnchorPoint (Vec2 (0, 0); cloudShadow-> setFlippedY (true); cloudShadow-> setOpacity (40); cloudShadow-> setPosition (40, wSize. height-cloud1-> getContentSize (). height * 2); addChild (cloudShadow); // Baiyun 2 auto cloud2 = Sprite: createWithTexture (textureCache-> getTextureForKey (s_backgound_cloud2); // set the anchor, cloud2-> setAnchorPoint (Vec2 (0, 0) in the lower left corner; cloud2-> setPosition (cloud1-> getContentSize (). width, wSize. height-cloud2-> getContentSize (). height); addChild (cloud2); // island auto land = Sprite: createWithTexture (textureCache-> getTextureForKey (s_island); land-> setAnchorPoint (Vec2 (1, 0); land-> setPosition (wSize. width-40, wSize. height-land-> getContentSize (). height-47*2); addChild (land); // island reflection auto landShadow = Sprite: createWithTexture (textureCache-> getTextureForKey (s_island )); landShadow-> setAnchorPoint (Vec2 (1, 0); landShadow-> setFlippedY (true); landShadow-> setOpacity (40); landShadow-> setPosition (wSize. width-40, wSize. height-land-> getContentSize (). height-78*2); addChild (landShadow); // obtain the first frame auto frame = spriteFrameCache-> watermark); auto boat = Sprite: createWithSpriteFrame (frame ); boat-> setPosition (wSize. width-boat-> getContentSize (). width-50*2,230*2); // create a frame animation auto Animation = animation: create (); for (int I = 1; I <4; I ++) {char bname [64] = {0}; sprintf (bname, sailing_boat%d.png, I); animation-> addSpriteFrame (spriteFrameCache-> getSpriteFrameByName (bname ));} animation-> setDelayPerUnit (0.5); animation-> setRestoreOriginalFrame (true); addChild (boat); auto animate = Animate: create (animation); boat-> runAction (RepeatForever :: create (animate); // The boat will wander back and forth, and will turn around auto flipxAction = FlipX: create (true); auto moveBy = MoveBy: create (10, vec2 (-240, 0); auto action = Sequence: create (moveBy, flipxAction, moveBy-> reverse (), flipxAction-> reverse (), NULL ); boat-> runAction (RepeatForever: create (action); // The second ship auto boat2 = Sprite: createWithSpriteFrame (frame); boat2-> setFlippedX (true ); boat2-> setPosition (100,400); addChild (boat2); boat2-> runAction (animate-> clone (); auto moveBy2 = MoveBy: create (12, Vec2 (300, 0); auto action2 = Sequence: create (moveBy2, flipxAction-> clone ()-> reverse (), moveBy2-> reverse (), flipxAction-> clone (), NULL); boat2-> runAction (RepeatForever: create (action2 ));}}
The Motion System of version 3.x is very different from that of version 2.x. For example, if the MoveTo action does not have the reverse () method, it means that the MoveTo action cannot be used as an inverse action. Frame animations are much simpler.
It will probably look like this:
There are also some elements, which will be explained in the next section. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPtTnyc/G8MC009a808HL0rvQqdeiys2jrNXi0fm/tMbwwLTH5bP + 0rvQqaGjPC9wPg0KPHByZSBjbGFzcz0 = "brush: java ;" Void StartGame: initUi (float t) {// kill all child nodes. Here, the main task is to kill the loading Genie and Its Action removeAllChildren (); // texture cache, pre-loaded resources can be obtained directly from the cache auto textureCache = ctor: getInstance ()-> getTextureCache (); // frame cache, directly getting frames from the cache auto spriteFrameCache = SpriteFrameCache:: getInstance (); {// loading background auto background = Sprite: createWithTexture (textureCache-> getTextureForKey (s_backgound); background-> setPosition (wSize. width/2, wSize. height/2); addChild (background) ;}{// 1 auto cloud1 = Sprite: createWithTexture (textureCache-> getTextureForKey (s_backgound_cloud1); // set the anchor, cloud1-> setAnchorPoint (Vec2 (0, 0) in the lower left corner; cloud1-> setPosition (0, wSize. height-cloud1-> getContentSize (). height); addChild (cloud1); // auto cloudShadow = Sprite: createWithTexture (textureCache-> getTextureForKey (s_backgound_cloud1); cloudShadow-> setAnchorPoint (Vec2 (0, 0); // vertical flip cloudShadow-> setFlippedY (true); // set the transparency cloudShadow-> setOpacity (40); cloudShadow-> setPosition (40, wSize. height-cloud1-> getContentSize (). height * 2); addChild (cloudShadow); // Baiyun 2 auto cloud2 = Sprite: createWithTexture (textureCache-> getTextureForKey (s_backgound_cloud2); // set the anchor, cloud2-> setAnchorPoint (Vec2 (0, 0) in the lower left corner; cloud2-> setPosition (cloud1-> getContentSize (). width, wSize. height-cloud2-> getContentSize (). height); addChild (cloud2); // island auto land = Sprite: createWithTexture (textureCache-> getTextureForKey (s_island); // set the anchor, land-> setAnchorPoint (Vec2 (1, 0); land-> setPosition (wSize. width-40, wSize. height-land-> getContentSize (). height-47*2); addChild (land); // island reflection auto landShadow = Sprite: createWithTexture (textureCache-> getTextureForKey (s_island )); landShadow-> setAnchorPoint (Vec2 (1, 0); landShadow-> setFlippedY (true); landShadow-> setOpacity (40); landShadow-> setPosition (wSize. width-40, wSize. height-land-> getContentSize (). height-78*2); addChild (landShadow); // get the first frame auto frame = spriteFrameCache-> getSpriteFrameByName(sailing_boat1.png); // create a ship genie auto boat = Sprite :: createWithSpriteFrame (frame); boat-> setPosition (wSize. width-boat-> getContentSize (). width-50*2,230*2); // create a frame animation auto Animation = animation: create (); // cyclically fetch the ship's sequence frame for (int I = 1; I <4; I ++) {char bname [64] = {0}; sprintf (bname, sailing_boat%d.png, I ); animation-> addSpriteFrame (spriteFrameCache-> getSpriteFrameByName (bname);} // set the interval of each animation frame to animation-> setDelayPerUnit (0.5 ); // keep the first animation frame after the animation is completed. The value is setRestoreOriginalFrame (true); addChild (boat); // create an action auto animate = Animate: create (animation ); boat-> runAction (RepeatForever: create (animate); // move to the left auto moveBy = MoveBy: create (10, Vec2 (-240, 0 )); // horizontal flip, ship Turn auto flipxAction = FlipX: create (true); // combine the animation to wander back and forth, and turn around auto action = Sequence: create (moveBy, flipxAction, moveBy-> reverse (), flipxAction-> reverse (), NULL); boat-> runAction (RepeatForever: create (action); // The second ship, the animation is the same as above, but on the left is auto boat2 = Sprite: createWithSpriteFrame (frame); boat2-> setFlippedX (true); boat2-> setPosition (100,400); addChild (boat2 ); // you cannot directly use animate here. Because boat1 is already used, you must use another action here, So clone boat2-> runAction (animate-> clone (); auto moveBy2 = MoveBy:: create (12, Vec2 (300, 0); // The flipx here is opposite to boat1, so reverse first, and no reverse auto action2 = Sequence :: create (moveBy2, flipxAction-> clone ()-> reverse (), moveBy2-> reverse (), flipxAction-> clone (), NULL); boat2-> runAction (RepeatForever:: create (action2 ));}}
This code is actually very clear. However, there are many knowledge points, such as texture caching, combined action, frame animation, and anchor. I will explain them one by one later. Let's look at the code first.