Cocos2d-x 3.6 version connection start interface 2
The previous article introduced several main elements of the interface, including background, white clouds, reflections, islands and their reflections, and beaches. What is a little difficult is the sequence Frame Animation, the delayed action sequence, such as the ship animation effect. The animation of the boat's wandering back and forth is a little more complicated. It consists of a series of actions, such as flip, moving, reverse movement of movement, sequential packaging, and unlimited repeated action packaging. But the code is very clear. If you cannot understand it, you can leave a message below.
This article describes several difficult actions, which will surely be full of pleasure after learning. Haha ~~
First, it is the wave animation, then the seagull animation, and then a play button and callback.
It may seem simple, but there are still many knowledge points. There is an article on the Internet that uses an animation for callback. The callback function is implemented by another action. If I do not do this here, I will simply follow the action sequence.
This wave is a bit special. Its animation is completed by two textures, one texture when it rises and the other texture when it falls. So how can we use two genie for an animation? Is there a way.
Yes, of course. Otherwise I won't talk about it.
A relatively advanced action, called TargetedAction, is used here. This action has a connection parameter. One is an action, and the other is the target to execute this action. Well, it's easy to do with this thing.
The idea is that wave 1 executes an action sequence, which contains a TargetedAction action. The Performer of this TargetedAction is Wave 2, which then executes its own action sequence, then, the connection is a surging trend. Check the Code:
// Waves 1 auto wave = Sprite: createWithTexture (textureCache-> getTextureForKey (s_backgound_sea1); wave-> setOpacity (0); wave-> setPosition (wSize. width/2,140*2); addChild (wave); // wave 2 auto wave2 = Sprite: createWithTexture (textureCache-> getTextureForKey (s_backgound_sea2); wave2-> setPosition (wSize. width/2,100*2); wave2-> setOpacity (0); addChild (wave2); // two sets of animations with more knowledge points: auto waveMoveBy = MoveBy: create (4.0, vec2 (0,-80); // from dark to bright auto fadeIn = FadeIn: create (3.0); // from Bright to dark auto fadeOut = FadeOut :: create (1.0); // Place it in the initial position auto place1 = Place: create (Vec2 (wSize. width/2,140*2); // The length of time is determined by the longest action. auto spawn = Spawn: create (waveMoveBy, Sequence: create (fadeIn, fadeOut, nullptr), nullptr); // auto spawn2 = Spawn: create (waveMoveBy-> clone ()-> reverse (), Sequence :: create (fadeIn-> clone (), fadeOut-> clone (), nullptr), nullptr); // auto place2 = Place :: create (Vec2 (wSize. width/2,100*2); // target action, specify the artget action auto targetAction = TargetedAction: create (wave2, Sequence: create (spawn2, place2, nullptr )); // delay of 2 s auto delay = DelayTime: create (2.0); wave-> runAction (RepeatForever: create (Sequence: create (spawn, targetAction, place1, delay, nullptr )));
Each line is annotated with many actions, including Place, FadeIn, FadeOut, MoveBy, Spawn, TargetedAction, and RepeatForever. There are many knowledge points.
This combination works well. If you have any questions, you can leave a message.
Then let's look at the seagull action.
The Seagull will fly over the sea. Of course, there is only one sea on the screen here. But I want to let a seagull fly from time to time, and the speed and directions are different, so it looks more realistic. How can this problem be solved.
The design is like this. A seagull genie has a sequence Frame Animation and a fan wing animation. Then, a random image appears at a location, flying over the screen, and the route is also random. From time to time, a random image appears.
First watch the sequence Frame Animation:
// Bird auto birdFrame = spriteFrameCache-> animation); mBird = Sprite: createWithSpriteFrame (birdFrame); auto point = Vec2 (CCRANDOM_0_1 () * wSize. width, 0); mBird-> setPosition (point); // create Frame Animation auto birdAnimation = Animation: create (); // cyclically retrieve the bird's sequence frame for (int I = 1; I <4; I ++) {char bname [64] = {0}; sprintf (bname, seagull1_d.png, i); birdAnimation-> addSpriteFrame (spriteFrameCache-> getSpriteFrameByName (bname);} // set the interval between each animation frame, birdAnimation-> setDelayPerUnit (0.3 ); // keep the first birdAnimation frame after the animation is completed-> setRestoreOriginalFrame (true); addChild (mBird); // create an action auto birdAnimate = Animate: create (birdAnimation ); mBird-> runAction (RepeatForever: create (birdAnimate ));
Here we use a member variable to keep a seagull object. Why? Because there is no way in the action to fly a seagull from time to time.
How can this effect be achieved? From time to time, a single flight occurs every time there is a time interval. So this is not like the system scheduler, right, It is the scheduler.
The seagull on the screen is placed at the point location, which is a bit random. Check the code and use a random function: CCRANDOM_0_1. Then we need to fly the first haishu out.
mBird->runAction(MoveBy::create(2, Vec2(CCRANDOM_0_1() * wSize.width , point.y + wSize.height + 100)));
Enable the Scheduler
// Enable the default scheduler and call the system update function to refresh scheduleUpdate ();
This function will enable the system scheduler, and the update function of the Node will start to refresh. By default, it is called every 0.1s. So we need to implement the update function.
Void StartGame: update (float dt) {// if a seagull leaves the screen, reset its position and re-execute the flying action if (mBird-> getPosition (). y> wSize. height + 20) {auto point = Vec2 (CCRANDOM_0_1 () * wSize. width, 10); mBird-> setPosition (point); mBird-> runAction (MoveBy: create (floor (5 * CCRANDOM_0_1 (), Vec2 (CCRANDOM_0_1 () * wSize. width, point. y + wSize. height )));}}
Then the animation of the bird is complete.
Next, get a play button and click Start game.
// Play button auto play = Sprite: createWithTexture (textureCache-> getTextureForKey (s_button_play); auto playS = Sprite: createWithTexture (textureCache-> getTextureForKey (response )); // The menu button receives a callback function auto menuItem = MenuItemSprite: create (play, playS, CC_CALLBACK_1 (StartGame: play, this); auto Menu = menu :: create (menuItem, nullptr); menu-> setPosition (wSize. width/2, wSize. height/2); addChild (menu );
This is quite simple, but I have a lot of knowledge about the principle of this callback function. Later I will talk about the callback function of the 3.x engine.
There is nothing in this callback function. Here we are going to enter the game scenario, but this scenario is not yet (:.
Screenshot:
Download and study it yourself.