Cocos2d-x use Ccscrollview to implement the checkpoint selection instance _c language

Source: Internet
Author: User
Tags addchild

Similar to the level of the selection of the function of the game is often seen, such as Help scene, select a checkpoint, by sliding the way to choose some other things and so on. Today, our choice to implement the checkpoint is to use the Ccscrollview class. Of course, there are other methods, such as the use of Cocostudio Page view can also. I would like to say the whole idea, Ccscrollview this class is inherited from the Cclayer, its own touch event some bugs, so the internet will generally treat this layer of touches event to false, and use its parent node to handle touch events, we also adopt this approach. First define a Levelscene class, join the Ccscrollview, and then define a layer layer, which contains a number of points in the picture, and then layer this layer as Ccscrollview content added. OK, now look at the code.

 * * The header file of the Select class * * * * #ifndef _level_scene_h_ #define _LEVEL_SCENE_H_ #include "cocos2d.h"//included to
The header file under #include "Cocos-ext.h" using namespace cocos2d::extension;

using namespace cocos2d;
	Class Levelscene:public Cclayer {public:bool init ();
	Create_func (Levelscene);
	The following is the registration of touch events and the realization of various touches function void Registerwithtouchdispatcher ();
	BOOL Cctouchbegan (Cctouch * touch,ccevent * pevent);
	void Cctouchmoved (Cctouch * touch,ccevent * pevent);
	void cctouchended (Cctouch * touch,ccevent * pevent);
Finally this function to check the location of each level, is the various checkpoints are located in the central void Adjustscrollview (float offset);
	Private://Add Ccscrollview as your own layer Ccscrollview * m_scrollview;
	The position of the touch point Ccpoint m_touchpoint;
	Ccscrollview the cheap quantity ccpoint m_offsetpoint;
Currently for the first several hurdles int m_ncurpage;
}; #endif 
/* The specific implementation of the Select class * * * #include "LevelScene.h" #include <math.h>//use Fabs () function, to find the absolute value of bool Levelscene::init () {bool BRe
	T = false; do {cc_break_if (!

		Cclayer::init ());

		Ccsize winsize = Ccdirector::shareddirector ()->getwinsize (); Ccscrollview inherits from Cclayer, and the incoming argument is the size of the view sizes//view the size that the person sees, and the content size, which is the contents, is set to the size of the entire screen,
		That is, we go through the entire screen of the device to see what's inside Ccscrollview * ScrollView = Ccscrollview::create (Ccsize (winsize.width,winsize.height));
		Equivalent to the following statement/*ccscrollview * ScrollView = Ccscrollview::create (); Scrollview->setviewsize (Ccsize (winsize.width,winsize.height)); *//below are some of the common functions of ccscrollview, but we don't use them here,
		Implementation of the idea of different//set whether there is a rebound effect, the rebound is when the size of the ScrollView to go back to the original position//scrollview->setbounceable (true);
		Ccscrollview The default anchor point is//scrollview->ignoreanchorpointforposition (false) at (0,0);
		Scrollview->setposition (CCP (WINSIZE.WIDTH/2,WINSIZE.HEIGHT/2)); Set sliding direction//kccscrollviewdirectionhorizontal--horizontal sliding//kccscrollviewdirectionvertical--vertical sliding//scrollview-&Gt;setdirection (Kccscrollviewdirectionboth);
		Create a Cclayer, add the content to the Cclayer, and then add the layer to scrollview cclayer * layer = cclayer::create ();
			for (int i = 0;i<5;i++) {ccstring * string = Ccstring::createwithformat ("%d.jpg", i+1);
			Ccsprite * Sprite = ccsprite::create (string->getcstring ()); Put all the sprites in the middle of the screen display sprite->setposition (Ccpadd (WINSIZE.WIDTH/2,WINSIZE.HEIGHT/2), CCP (winsize.width*i,0))
			;
		Layer->addchild (sprite);
		}//set content in ScrollView, the content must be set before the size of the content scrollview->setcontainer (layer);

		Setcontentsize () Sets the size of the content area scrollview->setcontentsize (ccsize (winsize.width*5,winsize.height));
		We shield ScrollView this layer of touch, using other implementation methods scrollview->settouchenabled (FALSE);

		Sets the offset of the inside content Scrollview->setcontentoffset (Ccpoint (0,0));

		Allow this layer to accept the touch event this->settouchenabled (true);

		This->addchild (ScrollView);
		M_scrollview = ScrollView;

		this->m_ncurpage = 0;
	BRet = true;

	while (0);
return bRet; } void Levelscene::registerwithtOuchdispatcher () {ccdirector::shareddirector ()->gettouchdispatcher ()->addtargeteddelegate (this,0,true);} BOOL Levelscene::cctouchbegan (Cctouch * touch,ccevent * pevent) {//Initialize the following member variables with the starting touch point and scroll offset this->m_touchpoint
	= Touch->getlocation ();

	This->m_offsetpoint = This->m_scrollview->getcontentoffset (); The following particular attention should be paid to the you can comment out the following words and then run the program, you will find that if the touch is not very fast/no problem, but if the touch is fast, the position of the checkpoint will not be correct, the following code is to solve this problem to if ((int)
	this->m_offsetpoint.x% ((int) ccdirector::shareddirector ()->getwinsize (). width) = = 0) {return true;
return false; }/* The whole meaning of the following code is that when the finger moves, let the level follow the finger to move, when the move is over, judge the end point and start the position of the touch point, the position of the level to do the corresponding processing///Set the level to follow the direction of the finger move void
	Cctouchmoved (Cctouch * touch,ccevent * pevent) {Ccpoint point = Touch->getlocation ();

	Ccpoint Direction = ccpsub (Point,this->m_touchpoint);
	Ccpoint spritedirection = Ccpadd (this->m_offsetpoint,direction);
	Offset Ccpoint spritedirection = Ccpoint (direction.x+this->m_offsetpoint.x,0) only in x direction; This-> M_scrollview->setcontentoffset (spritedirection); //The following code is the key, when the touch is over, in order for the level to appear in the middle of the screen, we need to do this void levelscene::cctouchended (Cctouch * touch,ccevent * pevent) {ccpoint en
	Dpoint = Touch->getlocation ();
	float distance = endpoint.x-this->m_touchpoint.x;
	When the finger moves less than 20, the offset is treated as 0 if (fabs (distance) <) {This->adjustscrollview (0);
	else {//The offset is passed in as a parameter this->adjustscrollview (distance); The final position of the adjustment level void Levelscene::adjustscrollview (float offset) {ccsize winsize = Ccdirector::shareddirector ()->get
  Winsize ();
  We judge the movement effect according to the actual situation of the offset//if the finger goes to the left, the offset is greater than 0, the page is reduced, and the right increase if (offset < 0) m_nCurPage + +;

	else if (Offset > 0) m_ncurpage-;
  Not allowed to exceed leftmost page and rightmost page if (m_nCurPage < 0) m_nCurPage = 0;

  else if (m_nCurPage > 4) m_ncurpage = 4;
	Ccpoint adjustpoint = CCP (-winsize.width * m_ncurpage, 0); This function has one more parameter than Setcontentoffset, and the second parameter is to set the time, that is, how long it takes to change the offset this->m_scrollview->setcontentoffsetinduration (Adjustpoint, 0.3f); }
 bool Helloworld::init () {////////////////////////////////1. Super Init ( !
  Cclayer::init ()) {return false;

	} ccsize visiblesize = Ccdirector::shareddirector ()->getvisiblesize ();
	Add a background picture Ccsprite * Sprite = ccsprite::create ("Background.png");
	Sprite->setposition (CCP (VISIBLESIZE.WIDTH/2,VISIBLESIZE.HEIGHT/2));

	This->addchild (sprite);
	Add Ccscrollview layer Levelscene * ScrollView = Levelscene::create ();

  This->addchild (ScrollView);
return true; }

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.