Cocos2d-x Plants vs. Zombies (1) (Map loading)

Source: Internet
Author: User
Tags addchild bool

//I am here to write an article is want to share experience with you, but I personally feel, the front I write is part of the code, even if you see, may feel I write too messy, no chapter can follow; I started writing, didn't think too much of the structure and hierarchy, so that the code seems very redundant When I look at someone else's blog, especially Jack's blog, I appreciate his structural pattern, and the level is very distinct, although I have written the fourth part about plants vs. Zombies. But I'm going to write it anew;

and the implementation of the code to write a word to the top of the blog, to facilitate the novice's learning is also conducive to master's guidance; look forward to more exchanges this time, I do not intend to use the background map, but the use of maps, the structure of the game is like this: it temporarily first contains a main scene; Today, to achieve is to load the map and change the size of the window, (the following work must be done in the Cocos2d-x game engine set up conditions, I use 2.2v);

First create the game scene class: Gamescene inherit Ccscene;

GameScene.h

#pragma once
#include "e:\cocos2d-x\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\layers_scenes_transitions_nodes\ Ccscene.h "
#include" cocos2d.h "
class Gamescene:p ublic cocos2d::ccscene
{public
  :
	Gamescene ( void);
	~gamescene (void);
	Create_func (Gamescene); Creates a create method that contains the Initialize init () method of
	virtual BOOL init ();
};
GameScene.cpp

#include "GameScene.h"
using_ns_cc;
Gamescene::gamescene (void)
{
}
gamescene::~gamescene (void)
{
}
bool Gamescene::init ()
{
	if (! Ccscene::init ())//Overriding the Init () method in the base class
	{
            return false;
	}
	return true;
}

Next we modify the AppDelegate.cpp,

First include GameScene.h

#include "AppDelegate.h"
#include "HelloWorldScene.h"//We still try to keep the engine comes with HelloWorld files without having to change or delete it
#include " GameScene.h "//Is here (including GameScene.h)
using_ns_cc;
Appdelegate::appdelegate () {

}
appdelegate::~appdelegate () 
{
}

Change one More place:

BOOL Appdelegate::applicationdidfinishlaunching () {
    //Initialize director
    ccdirector* pdirector = Ccdirector :: Shareddirector ();
	cceglview* Peglview = Cceglview::sharedopenglview ();
	Pdirector->setopenglview (Peglview);
    Turn on display FPS
    pdirector->setdisplaystats (true);
    Set FPS. The default value is 1.0/60 if you don ' t call this
    pdirector->setanimationinterval (1.0/60);
    Create a scene. It's an Autorelease object
    ccscene *pscene = Gamescene::create ();///It's Here (change Helloworld::scene () to Gamescene::create ( ));
    Run
    pdirector->runwithscene (pscene);
    return true;
}

OK here can produce an empty black window;

The following is to write the main game layer, plus a class gamelayer; The following is his. H. CPP file

GameLayer.h

#pragma once
#include "E:\cocos2d-x\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\layers_scenes_transitions_nodes\cclayer.h"
#include "cocos2d.h"
class Gamelayer:p ublic  cocos2d::cclayer
{public
:
	gamelayer (void);
	~gamelayer (void);
	virtual BOOL init ();
	Create_func (Gamelayer);
};
GameLayer.cpp

#include "GameLayer.h"
using_ns_cc;
Gamelayer::gamelayer (void)
{	
}
gamelayer::~gamelayer (void)
{
}
bool Gamelayer:: Init ()
{
	if (! Cclayer::init ())
	{
		return false;
	}
	return true;
}
below we add the main game layer to the gamescene scene;

Just add the following code to the GameScene.h;

#include "GameLayer.h"
void Initgamelayer (); This declares a function of initializing the game layer.
we define in GameScene.cpp; void Initgamelayer ();

void Gamescene::initgamelayer ()
{
	gamelayer* gamelayer = Gamelayer::create ();
	This->addchild (Gamelayer);
}
Finally, add the defined function to the bool Gamescene::init ().

This->initgamelayer ();

Next we will create a map layer Maplayer, create a map in the layer, and add to the Gamelayer main layer;

MapLayer.h

#pragma once
#include "e:\cocos2d-x\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\layers_scenes_transitions_nodes\ Cclayer.h "
#include" cocos2d.h "
class Maplayer:p ublic cocos2d::cclayer
{public
:
	Maplayer ( void);
	~maplayer (void);
	Create_func (Maplayer);
	virtual BOOL init ();
	void Initmap ();//Initialize map function
	cocos2d::cctmxtiledmap* _map;//Declare global variables of map
};
MapLayer.cpp

#include "MapLayer.h"
using_ns_cc;
Maplayer::maplayer (void)
{
}
maplayer::~maplayer (void)
{
}
bool  maplayer::init ()
{
	if (! Cclayer::init ())
	{
	    return false;
	}
	This->initmap ();
	return true;
}
void Maplayer::initmap () 
{
	this->_map =cctmxtiledmap::create ("MAP_LIST.TMX");//Map files are made by the tile software, This piece should not be difficult, this map                                                           uses two layers of
	this->addchild (THIS->_MAP);
}


Let's add our map layer to our main game layer.

Add the following code to the GameLayer.h

#include "MapLayer.h"
maplayer* _maplayer; Declare map layer global variable
void Initmaplayer ();//Declare Initialize Map

Defined in GameLayer.cpp

In the GameLayer.cpp constructor: perform the following initialization

Gamelayer::gamelayer (void)
{
	this->_maplayer =null;
}

define void Initmaplayer ();
void Gamelayer::initmaplayer ()
{
	this->_maplayer =maplayer::create ();
	This->addchild (This->_maplayer);
}

Adding an initialization map to the bool Gamelayer::init ()

This->initmaplayer ();

Good to this one can show a map of the game: look at the effect:


It's so big, I want to show all the maps, and then we need to change the AppDelegate.cpp,

BOOL Appdelegate::applicationdidfinishlaunching () {
    //Initialize director
    ccdirector* pdirector = Ccdirector :: Shareddirector ();
	cceglview* Peglview = Cceglview::sharedopenglview ();
	Peglview->setframesize (1350, 500);//Is Here we modify the drawing window, note that the position can not be mistaken
	Pdirector->setopenglview (Peglview);
	

Here is the effect diagram after modifying the window:



, loading the map section is over here.


Game Resources Links: http://download.csdn.net/detail/u010296979/6685103














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.