zoukankan      html  css  js  c++  java
  • 【cocos2dx 3.2】瓦片地图制作

    使用Tiled编辑地图

    • 每个图层仅仅能放一种瓦片
    • 瓦片的大小最好是32*32的倍数
    • 对象层里面设置路径的坐标
    • 主程序中获取对象层中的坐标,做对应的操作



    设置口袋精灵类:


    Monster.h

    #include "cocos2d.h"
    
    USING_NS_CC;
    
    class Monster : public Sprite
    {
    public:
    	virtual bool init(Vector<Node*> points);
    	static Monster* create(Vector<Node*> points);
    
    	//用于获取对象层的坐标
    	Vector<Node*> p;
    	Vector<Node*>::iterator start;
    
    	//精灵
    	Sprite *s;
    
    	//依照路径移动
    	void moveByPath();
    
    	//种类随机数
    	int ranNum;
    
    };

    Monster.cpp

    #include "Monster.h"
    
    Monster* Monster::create(Vector<Node*> points)
    {
    	auto monster = new Monster();
    	monster->init(points);
    	monster->autorelease();
    	return monster;
    }
    
    bool Monster::init(Vector<Node*> points)
    {
    	Sprite::init();
    
    	//设置随机数,控制出场精灵种类
    	srand(time(NULL));
    	ranNum = rand()%5;
    
    	p = points;
    	start = p.begin();
    
    	switch (ranNum)
    	{
    	case 0 :
    			{
    				s = Sprite::create("1.png");
    				break;
    			}
    	case 1:
    			{
    				s = Sprite::create("2.png");
    				break;
    			}
    	case 2:
    			{
    				s = Sprite::create("3.png");
    				break;
    			}
    	case 3:
    			{
    				s = Sprite::create("4.png");
    				break;
    			}
    	case 4:
    			{
    				s = Sprite::create("5.png");
    				break;
    			}
    	}
    	s->setPosition((*start)->getPosition());
    	addChild(s);
    
    	return true;
    }
    
    //沿着路径移动
    void Monster::moveByPath(){
    	++start;
    	if(start == p.end()){
    		s->removeFromParent();
    	}
    	else{
    		Point a = Point((*start)->getPosition());
    		s->runAction(Sequence::create(MoveTo::create(2,a),CallFuncN::create(CC_CALLBACK_0(Monster::moveByPath,this)),NULL));
    	}
    }

    主场景类


    HelloWorldScene.h

    #ifndef __HELLOWORLD_SCENE_H__
    #define __HELLOWORLD_SCENE_H__
    
    #include "cocos2d.h"
    #include "Monster.h"
    
    USING_NS_CC;
    
    class HelloWorld : public cocos2d::LayerColor
    {
    public:
    
        static cocos2d::Scene* createScene();
    	virtual bool init();  
        CREATE_FUNC(HelloWorld);
      
        void menuCloseCallback(cocos2d::Ref* pSender);
      
    
    	//存放对象层里的坐标
    	Vector<Node*> points;
    	Vector<Node*>::iterator startPoint;
    
    
    	//加入物体
    	void addMonster();
    
    	//用于控制时间间隔
    	int oldTime;
    	int curTime;
    	void resetTime();
    
    	void update(float dt);
    
    	//精灵
    	Sprite *s;
    
    	
    };
    
    #endif // __HELLOWORLD_SCENE_H__
    

    HelloWorldScene.cpp

    #include "HelloWorldScene.h"
    
    USING_NS_CC;
    
    Scene* HelloWorld::createScene()
    {
       
    	auto scene = Scene::createWithPhysics();
    
        auto layer = HelloWorld::create();
    
    	scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
    	scene->getPhysicsWorld()->setGravity(Point(0,-1000));
      
        scene->addChild(layer);
    
        return scene;
    }
    
    // on "init" you need to initialize your instance
    bool HelloWorld::init()
    {
        //////////////////////////////
        // 1. super init first
    	if ( !LayerColor::initWithColor(Color4B(255,255,255,255)) )
        {
            return false;
        }
        
        Size visibleSize = Director::getInstance()->getVisibleSize();
        Point origin = Director::getInstance()->getVisibleOrigin();
    	
    
    	//加入物理边界
    	auto body = PhysicsBody::createEdgeBox(visibleSize,PHYSICSBODY_MATERIAL_DEFAULT,3); 
    	auto node = Node::create();
    	node->setPhysicsBody(body);
    	node->setPosition(visibleSize.width/2,visibleSize.height/2);
    	addChild(node);
    
    	//加入地图文件
    	auto map = TMXTiledMap::create("pokamon.tmx");
    	map->setPosition(200,0);
    	addChild(map);
    	
    
    	//获得对象层中的坐标,存在向量里
    	TMXObjectGroup* objectGroup = map->getObjectGroup("monster");
    	ValueVector object = objectGroup->getObjects();
    	
    	for (ValueVector::iterator it = object.begin(); it != object.end(); it++) {
    		Value obj = *it;
    		ValueMap m = obj.asValueMap();
    		auto node = Node::create();
    		node->setPosition(m.at("x").asFloat()+200,m.at("y").asFloat());
    		points.pushBack(node);
    	}
    	
    	//重置时间
    	resetTime();
    	
    	//开启计时器
    	scheduleUpdate();
    
        return true;
    }
    
    void HelloWorld::update(float dt)
    {
    	++oldTime;
    	if (oldTime == curTime)
    	{
    		resetTime();
    		addMonster();
    	}
    }
    
    void HelloWorld::resetTime()
    {
    	oldTime = 0;
    	curTime = 40;
    }
    
    void HelloWorld::addMonster()
    {
    	auto hero = Monster::create(points);
    	hero->moveByPath();
    	addChild(hero);
    }
    
    
    void HelloWorld::menuCloseCallback(Ref* pSender)
    {
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
    	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
        return;
    #endif
    
        Director::getInstance()->end();
    
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
        exit(0);
    #endif
    }

    效果:



  • 相关阅读:
    【NOI1999、LOJ#10019】生日蛋糕(搜索、最优化剪枝、可行性剪枝)
    【NOI2016】区间
    【CodeForces688A】Opponents
    「LOJ10150」括号配对
    HDU6444(子段和、分情况比较)
    牛客练习赛41E(球的体积并)
    Codeforces 1154G(枚举)
    POJ1830(异或高斯消元)
    Codeforces 1114F(欧拉函数、线段树)
    牛客小白月赛13 G(双向搜索)
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/7401611.html
Copyright © 2011-2022 走看看