Cocos2d-x map with the genie unlimited rolling and edge detection ---- game development "Zhao cloud to fight", Edge Detection

Source: Internet
Author: User
Tags map class

Cocos2d-x map with the genie unlimited rolling and edge detection ---- game development "Zhao cloud to fight", Edge Detection

This chapter in the previous Cocos2d-x Custom button control genie attack ---- game development "Zhao cloud to fight" on the basis of the implementation of the genie to the right to the middle of the map, the map can follow the movement, however, in this case, the genie only plays the running animation. In addition, when the map moves to the edge, the map does not move, but the genie can move and play the running animation at the same time. The current project is being modified. If you need to leave your mailbox, you can upload each project without modifying it.

Directory

1. Add a function in the Hero class to determine whether the hero has moved to the center position of the window.

Ii. Custom Map

3. Move a map or a mobile genie based on the size of the hero Genie and window

Iv. Summary


1. Add a function in the Hero class to determine whether the hero has moved to the center position of the window.

Add another function in the previous Hero class:

<Pre name = "code" class = "cpp"> // determines whether the hero has moved to the center of the window. visibleSize is the size of the current window. bool JudgePositona (CCSize visibleSize );

Then this is its implementation: 

Bool Hero: JudgePositona (CCSize visibleSize) {if (this-> getPositionX ()! = VisibleSize. width/2) // return false to the left of the genie; else return true; // reach the intermediate position}


Ii. Custom Map

Here, I designed another map class to enable other functions for the back-end map. It can determine whether to move the map based on the hero's movements.

In fact, the map here is a CCSprite, and then adds it to the current class, which is derived from CCNODE.

The Code Map. h:

# Ifndef _ MAP_H __# define _ MAP_H __# include "cocos2d. h" USING_NS_CC; class Map: public CCNode {public: Map ();~ Map (); // initialize the Map. window_sizeo is the console size void InitMap (const char * map_name, const CCSize & window_size); // move the Map based on the location of the genie, visibleSize is the size of the current window void MoveMap (CCNode * hero, CCSize visibleSize); // determines whether the map has reached the bool JudgeMap (CCNode * hero, CCSize visibleSize ); // virtual void update (float delta); CREATE_FUNC (Map); private: CCSprite * m_map; // Map Genie}; # endif // _ MAP_H __

Then this is its implementation Map. cpp:

# Include "Map. h" Map: Map (): m_map (NULL) {} Map ::~ Map () {} void Map: InitMap (const char * map_name, const CCSize & window_size) {this-> m_map = CCSprite: create (map_name ); m_map-> setAnchorPoint (ccp (); // set the anchorpoint this-> addChild (m_map );} void Map: MoveMap (CCNode * hero, CCSize visibleSize) // {if (hero-> getPositionX () = visibleSize. width/2) // when the sprite moves to the center, the map moves {if (this-> getPositionX ()! =-(M_map-> getContentSize (). width-visibleSize.width) // prevent the Map from moving left beyond the edge this-> setPosition (this-> getPositionX ()-1, this-> getPositionY ();} bool Map :: judgeMap (CCNode * hero, CCSize visibleSize) {if (this-> getPositionX ()! =-(M_map-> getContentSize (). width-visibleSize.width) // prevent the map from moving left beyond the edge after return false; else // the map has been moved to the edge return true ;}


3. Move a map or a mobile genie based on the size of the hero Genie and window
How can I use the custom map class above?

Add the header file # include "Map. h" to HelloWorldScene. h"

Define a member variable at the same time

Private: Map * mymap; // Map
Then you can use it directly. In the init () function:

Originally I used: (I wrote it at the beginning of article 1-virtual joystick)

// Modify the background image CCSprite * pSprite = CCSprite: create ("background_1.jpg"); pSprite-> setPosition (ccp (visibleSize. width/2 + origin. x, visibleSize. height/2 + origin. y); this-> addChild (pSprite, 0); // The value 0 indicates that it is placed at the bottom layer.

Remove the above and change it:

// Change the Map mymap = Map: create (); mymap-> InitMap ("12.png", visibleSize); this-> addChild (mymap, 0 );

Then modify the updata () event in HelloWorldScene. cpp:

Void HelloWorld: update (float delta) {CCSize visibleSize1 = CCDirector: sharedDirector ()-> getVisibleSize (); // determines whether to press the joystick and its type switch (rocker-> rocketDirection) {case 1: // hero-> SetAnimation ("attack1_animation.plist", "attack1_animation.png", "attack _", 6, rocker-> rocketRun ); hero-> SetAnimation ("run_animation.plist", "run_animation.png", "run _", 8, rocker-> rocketRun ); // "run _" indicates the public name of each image in the run_animation.png set image if (hero-> GetPositionX () <= visibleSize1.width-8) // you can change the value of {if (! Hero-> JudgePositona (visibleSize1) | mymap-> JudgeMap (hero, visibleSize1) // The genie can move only when they do not reach the middle of the window or the map has been moved to the edge, otherwise, only play the animation hero-> setPosition (ccp (hero-> getPosition (). x + 1, hero-> getPosition (). y); // move to the right. // The following figure shows the mobile map mymap-> MoveMap (hero, visibleSize1);} break; case 2: hero-> SetAnimation ("run_animation.plist ", "run_animation.png", "run _", 8, rocker-> rocketRun ); // "run _" indicates the public name of each image in the run_animation.png set. hero-> setPosition (ccp (hero-> getPosition (). x, hero-> getPosition (). y + 1); // go up to break; case 3: hero-> SetAnimation ("run_animation.plist", "run_animation.png", "run _", 8, rocker-> rocketRun); // "run _" indicates the public name of each image in the run_animation.png set. if (hero-> getPositionX ()> = 8) // do not let the genie go beyond the left. You can change 8 to your favorite hero-> setPosition (ccp (hero-> getPosition (). x-1, hero-> getPosition (). y); // take break to the left; case 4: hero-> SetAnimation ("run_animation.plist", "run_animation.png", "run _", 8, rocker-> rocketRun ); // "run _" indicates the public name of each image in the run_animation.png set. hero-> setPosition (ccp (hero-> getPosition (). x, hero-> getPosition (). y-1); // walk down break; case 0: hero-> StopAnimation (); // stop all animations and motion break;} if (btn-> isTouch) hero-> AttackAnimation ("attack1_animation.plist", "attack1_animation.png", "attack _", 6, rocker-> rocketRun );}

In this way, we can see the effect:


First, when the genie moves to the left edge, it cannot move any more,


Next, when the hero moves to the middle of the right (the map has not reached the rightmost), the map moves, and the genie only plays the animation but does not change its position.


When the map is moved to the rightmost.



This is the effect. It was originally intended that the map could be moved around with the genie, but it was a little troublesome. I had to judge whether it was left or right, so I stole it,

After the map is moved, it cannot be moved back. I want to move it back. I have some ideas, but it takes a lot of trouble to write the code. I will try again later.


Iv. Summary

To put it simply:

1. The genie has not moved to the right in the middle of the window.

When playing the running animation, the genie moves the position at the same time, but the map position does not change at this time.

2. The genie moves right to the middle of the window and the map has not been moved to the rightmost

The genie only plays the running animation, but does not change its position. However, the map must be moved to the left, resulting in the illusion that the genie is moving.

3. The map is moved to the rightmost side.

At this time, do not change the location of the map. The sprite needs to move the running animation at the same time.

As the current project is being modified, if you need to leave the mailbox, you can upload each project without modifying it.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.