Cocos2d-x 3.6 version connection start interface, cocos2d-x3.6
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); // get the first frame auto frame = spriteFrameCache-> getSpriteFrameByName ("sailing_boat1.png"); 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.