您的位置:首页 > 其它

学起来就不觉得疲倦和无聊了~~

2014-05-03 17:41 495 查看

1.场景创建的差不多了,我们可以创建玩家了,因为玩家与怪物都可以认为是精灵构成的,所以我们可以为它们两个写一个共同的基类。

转载请注明,原文地址: /content/528576.html

在实体下创建Entity这个类

[cpp] view
plaincopyprint?

//Entity.h文件

#ifndef __ENTITY_H__

#define __ENTITY_H__

#include "cocos2d.h"

using namespace cocos2d;

class Entity : public CCNode {

public:

void setSprite(CCSprite* mSprite);

protected:

CCSprite* mSprite;

};

#endif

然后在cpp中只需要写void Entity::setSprite(CCSprite*mSprite){
这个方法即可
void Entity::setSprite(CCSprite*mSprite){

this->mSprite=mSprite;

然后创建Playey这个类

[cpp] view
plaincopyprint?

//Player.h文件

#ifndef __PLAYER_H__

#define __PLAYER_H__

#include "Entity.h"

class Player : public Entity {

public:

CREATE_FUNC(Player);

virtual bool init();

};

#endif

[cpp] view
plaincopyprint?

//Player.cpp文件

#include "Player.h"

bool Player::init()

{

return true;

}

都有了,此时我们将玩家增加到地图上
打开TollgateScene.cpp
//加载玩家
CCSprite*playSprite=CCSprite::create("");
playerSprite->setPosition(ccp(100,200));
map->addChild(playerSprite);
//将精灵和玩家绑定
Player*mPlayer=Player::create();
mPlayer->setSprite(playerSprite);

3. 固定玩家出生点(对象层的使用)

现在主角的坐标是在代码里写死的,我们还可以更灵活一点,Tiled Map还有一个很厉害的功能,那就是:对象层。

其实没有多神奇,就是在地图上的某个位置设置一个对象,然后可以在代码里获取这个对象的信息,比如坐标,对了,就是要坐标~

来,打开我们的地图编辑器,点击菜单栏的图层,添加对象层,新建一个对象层:



或者在右下角的图层那里点击新建:



然后将图层命名为“objects”,哇,好形象的名字,噗~



现在我们要绘制对象,点击菜单栏上的这个按钮:



然后在地图上绘制一个矩形,它代表一个对象:



接下来,我们要给这个对象命名,在矩形上右键,选择对象属性,然后命名为PlayerPoint:



OK,我们现在只要在代码里取得PlayerPoint对象,再获得它的x、y属性就可以了。

继续打开TollgateScene.cpp文件,修改init函数,在设置玩家精灵坐标的前面,添加以下代码:
分为3步吧。
1.加载到对象层
CCTMXObject
Group*obGroup=map->objectGroupName("objects");
2.加载规定的坐标
CCDirector*playerPointDic=obGroup->objectNamed("PlayerPoint");
float x=playerPointDic->valueForKey("x")->floatValue();
float y=playerPointDic->valueForKey("x")->floatValue();

CCSprite* playerSprite = CCSprite::create("sprite/player1.png");

playerSprite->setPosition(ccp(playerX, playerY));

4.通过动画帧执行跑的动作,比是木头博客那个的优化哦。
void Player::run(){

CCSpriteFrameCache*cache=CCSpriteFrameCache::sharedSpriteFrameCache();

cache->addSpriteFramesWithFile("run.plist","run.png");

//

CCSpriteFrame*frame=NULL;

//创建列表

CCArray*arr=CCArray::create();

//循环实现

for(int i=1;i<15;i++){

frame=cache->spriteFrameByName(CCString::createWithFormat("run%d.png",i)->getCString());

arr->addObject(frame);

}

//根据精灵帧对象创建动画

CCAnimation*animation=CCAnimation::createWithSpriteFrames(arr);

//设置属性

animation->setLoops(-1);

animation->setDelayPerUnit(0.8f);

m_sprite->runAction(CCAnimate::create(animation));

}
5.设置控制器,同意我们设置控制器的基类,第一便看的时候不明白为什么要设置控制器,直接添加到相应的.cpp中多简洁省事,添加这么多东西, 但是现在终于明白了,设置控制器表面负责,实际上为以后改变条件省事了,这样子直接在控制器上改了,不用去找具体的cpp具体的函数

[cpp] view
plaincopyprint?

<span style="font-size:14px;">//Controller.h文件

#ifndef __CONTROLLER_H__

#define __CONTROLLER_H__

#include "cocos2d.h"

#include "ControllerListener.h"

using namespace cocos2d;

class Controller : public CCNode {

public:

/* 设置监听对象 */

void setControllerListener(ControllerListener* mControllerListener);

protected:

ControllerListener* mControllerListener;

};

#endif

</span>

[cpp] view
plaincopyprint?

<span style="font-size:14px;">//Controller.cpp文件

#include "Controller.h"

void Controller::setControllerListener( ControllerListener* mControllerListener )

{

this->mControllerListener = mControllerListener;

}

</span>

ControllerListener就是将要被控制的对象,比如我们的主角,只要继承了ControllerListener接口,就能被控制器控制

看看ControllerListener的代码:

[cpp] view
plaincopyprint?

<span style="font-size:14px;">//ControllerListener.h文件

#ifndef __CONTROLLER_LISTENER_H__

#define __CONTROLLER_LISTENER_H__

#include "cocos2d.h"

using namespace cocos2d;

class ControllerListener {

public:

virtual void setSimplePosition(int x, int y) = 0;

virtual CCPoint getCurPosition() = 0;

};

#endif

</span>

5.实现一个简单的控制器





游戏里面的画面都是由每一帧绘制,从而形成一个丰富多彩的世界,默认是不调用的,
this->scheduleUpdate();进行调用

6.今天学的有点疲惫了呢~是的是的,与主角绑定的控制器就下回在写~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: