展会信息港展会大全

【cocos2dx 3.2】瓦片地图制作,cocos2dx瓦片 使用Tiled编辑地图 设置口袋精灵类: 主场景类 效果:
来源:互联网   发布日期:2015-09-28 15:35:26   浏览:3288次  

导读: 【cocos2dx 3.2】瓦片地图制作,cocos2dx瓦片 使用Tiled编辑地图 每一个图层只能放一种瓦片 瓦片的大小最好是32*32的倍数 对象层里面设置路径的坐标 主程序中获取对...

【cocos2dx 3.2】瓦片地图制作,cocos2dx瓦片

使用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

}

效果:

cocos2d-x32 WIN32移植到Android 编译时遇到的问题

我的博客上有关于cocos2dx-3.2关于Android项目移植的问题。CSDN,windvix专栏。参考:blog.csdn.net/windvix/article/details/38337003

赞助本站

人工智能实验室

相关热词: android开发 应用开发

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港