Cocos2dx3. X Project rewrite (6) add map and skip optimization at different heights
In the past, my map was a horizontal ground plane. In order to increase playability, I added a random square map with random sizes and heights.
First, prepare four images with the same height but different widths as random map blocks.
The problem is that you must add a map block to the schedule user-defined function, but to be random, first, you must draw the rightmost area of the screen on the rightmost side of the previous map block before the second block can appear. In this gap, you must add a random time and a random next map block. Let me explain my solutions one by one.
First, you must create a map bigblock. cpp and bigblock. h file, my idea is to make bigblock achieve two effects in life, one is to move to the left, the other is to randomly Add a resource (four pictures are prepared before ), the first problem was previously implemented, that is, adding schedule. Not to mention, the second function. to randomly add an image, I first define a create function.
static bigblock* create();
This makes it clear that I add the names of the four images to the array as strings, and then randomly take one and pass it in. The Code is as follows:
bigblock* bigblock::create(){bigblock* bb = new bigblock();bb->init();bb->speed = 5;std::string v[4]={"bigblock1.png","bigblock2.png","bigblock3.png","bigblock4.png"};int r=0+rand()%3;bb->initWithFile(v[r]);bb->setPhysicsBody(PhysicsBody::createBox(bb->getContentSize()));bb->getPhysicsBody()->setDynamic(false);return bb;}
In this way, the functions of different blocks are implemented at random, and then the random time is added.
In stage, I already have the addblock function executed on every frame. At this time, a problem occurs, that is, if the random time is directly added, it is very easy to set a random function, just create a map block, but this will overlap, and that's not good. I have to know that the tail of the previous block has just stepped out of the right of the screen. At this time, I will give it a random time, adding blocks will not overlap, and there will be pitfalls. If the player fails, it will be suspended. But the problem comes again,
The function we added must be auto bb = bigblock: create ();. Each genie is called bb and cannot be distinguished, under normal circumstances, if we add two elves with duplicate names, an error will be reported. However, in the schedule function, if we add one Genie with the same name to each frame, no error will be reported. I checked the information, schedule places these genie in a queue or array. In this way, there may be no duplicate names, but I have not found a way to access them ), I thought it would be too troublesome to add an array. Later I thought of a way to use tags to mark each Genie and use getChildByTag to get the current tag genie, if his tail is out of bounds, add the next tag ++ to a random time. The Code is as follows:
int hh= 0+rand()%55;if (getChildByTag(bigtag)->getContentSize().width/2+getChildByTag(bigtag)->getPositionX()
=b){auto bb=bigblock::create();this->addChild(bb);bb->setPosition(Vec2(visible.width+bb->getContentSize().width/2,hh));bigtag=bigtag+1;bb->setTag(bigtag);restar(); }}
As you can see, when we set the coordinates, the coordinate of the Y axis uses a random function, and we will get a ground that is highly staggered.
The role of a and B has previously been said to be to obtain random time.
Now the problem on the ground has been solved temporarily, and the next is the problem of jumping.
In the past, we decided that the jump was not complete. We decided to check whether the coordinates at the bottom of the player were on the ground. Now, the coordinates on the ground were not fixed. I have been thinking about this for a long time. It would be too troublesome to judge the height of the object under my feet. Later I thought I could use the speed to judge it. If the speed is zero, I would jump again, but the problem is that the speed at the highest point is also zero. This problem once made me very worried. How can I determine whether it is at the highest point or on the ground? I want to say that there must be some methods, and it must be a simple small algorithm, but I found that, because the speed in the air is too short to jump, so it's time to skip. The Code is as follows:
In schedule
if(player->getVy()==0) { ve=1; } else { ve=0; }
When jumping
if (ve==1) { player->getPhysicsBody()->setVelocity(Vec2(0,1200)); }
Finally, there is another problem. An interesting feature is that the jump button will jump higher and fall as soon as it is released. This feature is implemented in this way, set the speed to zero when the jump button is lifted.
void Stage::button_jump_touchup(Ref* pSender,Control::EventType event){auto v=player->getPhysicsBody()->getVelocity();if (v.y>0){ player->getPhysicsBody()->setVelocity(Vect(0,0)); }}
These are my solutions. If you have any better solutions or questions, please leave a message ~