【Cocos2d-x3.0学习笔记 10】游戏实例跑跑跑02

【Cocos2d-x3.0学习笔记 10】游戏实例跑跑跑02,第1张

概述1.实现大地图的滚动 设置地图只会横向向左滚动,当主角超过屏幕中点时地图就开始卷动 #ifndef _PLAYER_H_#define _PLAYER_H_#include "Entity.h"class Player : public Entity{public: bool init(); CREATE_FUNC(Player); void run(); void setTMX 1.实现大地图的滚动

设置地图只会横向向左滚动,当主角超过屏幕中点时地图就开始卷动

#ifndef _PLAYER_H_#define _PLAYER_H_#include "Entity.h"class Player : public Entity{public:	bool init();	CREATE_FUNC(Player);	voID run();	voID setTMXTiledMap(TMXTiledMaP* map);	virtual voID setTagposition(int x,int y);private:	voID setVIEwPointByPlayer();	TMXTiledMaP* m_map;};#endif

#include "Player.h"#include "AnimationUtil.h"bool Player::init(){	return true;}voID Player::run(){	SpriteFrameCache* frameCache = SpriteFrameCache::getInstance();	frameCache->addSpriteFramesWithfile("runs.pList","runs.png");	Animation* action = AnimationUtil::createWithStringFramename("run",0.1f,-1);	mSprite->runAction(Animate::create(action));}voID Player::setVIEwPointByPlayer(){	if (mSprite == NulL)	{		return;	}	//获取格子的数量	Size mapTilednum = m_map->getMapSize();	//获取格子的大小	Size tiledSize = m_map->getTileSize();	//地图大小,这点有点不好理解啊	Size mapSize = Size(mapTilednum.wIDth*tiledSize.wIDth,mapTilednum.height*tiledSize.height);	//屏幕大小	Size visibileSize = Director::getInstance()->getVisibleSize();	//主角坐标	Point pos = getposition();	//如果主角坐标小于屏幕的一半,则取屏幕中点坐标,否则去主角坐标	float x = std::max(pos.x,visibileSize.wIDth/2);	float y = std::max(pos.y,visibileSize.height/2);	//如果x,y坐标大于右上角的极限值,则取极限值坐标	x = std::min(x,mapSize.wIDth - visibileSize.wIDth/2);	y = std::min(x,mapSize.height - visibileSize.height/2);	//目标点	Point destPos = Point(x,y);	//屏幕中点	Point centerPos = Point(visibileSize.wIDth/2,visibileSize.height/2);	//计算屏幕中点和所要移动的目的点之间的距离	Point vIEwPos = centerPos - destPos;	//获取父类	Layer* parent = (Layer*)getParent();	parent->setposition(vIEwPos);}voID Player::setTagposition(int x,int y){	Entity::setTagposition(x,y);	setVIEwPointByPlayer();}voID Player::setTMXTiledMap(TMXTiledMaP* map){	this->m_map = map;}

这个函数的功能是让地图所在图层以主角为中心进行移动,也就是让事件的焦点停留在主角身上,屏幕随着主角移动。

每当主角的坐标改变时,都会让地图坐标随着一起改变。

或许地图会出现一些细细的黑边,需要加上这句

Director::getInstance()->setProjection(Director::Projection::_2D);
加哪里都是阔以的。 2.三方移动控制器

实现主角向前,上,下移动

#ifndef _THREED_DIRECTION_CONTRolLER_H_#define _THREED_DIRECTION_CONTRolLER_H_#include "Controller.h"class ThreeDirectionController : public Controller{public:	CREATE_FUNC(ThreeDirectionController);	bool init();	voID setXSpeed(int xSpeed);	voID setYSpeed(int ySpeed);	voID update(float dt);	private:	int xSpeed;	int ySpeed;	voID registtouchListener();};#endif

#include "ThreeDirectionController.h"bool ThreeDirectionController::init(){	this->xSpeed = 0;	this->ySpeed = 0;	registtouchListener();	this->scheduleUpdate();	return true;}voID ThreeDirectionController::setXSpeed(int xSpeed){	this->xSpeed = xSpeed;}voID ThreeDirectionController::setYSpeed(int ySpeed){	this->ySpeed = ySpeed;}voID ThreeDirectionController::update(float dt){	if (m_Listener == NulL)	{		return;	}	Point pos = m_Listener->getTagposition();	pos.x += xSpeed;	pos.y += ySpeed;	m_Listener->setTagposition(pos.x,pos.y);}voID ThreeDirectionController::registtouchListener(){	auto Listenr = EventListenertouchOneByOne::create();	Listenr->ontouchBegan = [](touch* touch,Event* event){		return true;	};	Listenr->ontouchmoved = [&](touch* touch,Event* event){		//获取点击的坐标		Point touchPos = Director::getInstance()->convertToGL(touch->getLocationInVIEw());		//获取主角的位置		Point pos = m_Listener->getTagposition();		int iSpeed = 0;		//当点击精灵上方的时候		if (touchPos.y > pos.y)		{			iSpeed = 1;		}else		{			iSpeed = -1;		}		setYSpeed(iSpeed);	};	Listenr->ontouchended = [&](touch* touch,Event* event){		//停止移动		setYSpeed(0);	};	_eventdispatcher->addEventListenerWithSceneGraPHPriority(Listenr,this);}
在TollgateScene的addplayer中添加
ThreeDirectionController* controller = ThreeDirectionController::create();	controller->setXSpeed(1);	controller->setYSpeed(0);	this->addChild(controller);	player->setController(controller);
总结

以上是内存溢出为你收集整理的【Cocos2d-x3.0学习笔记 10】游戏实例跑跑跑02全部内容,希望文章能够帮你解决【Cocos2d-x3.0学习笔记 10】游戏实例跑跑跑02所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1009692.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存