展会信息港展会大全

Cocos2d-X开发一个简单的小游戏
来源:互联网   发布日期:2015-09-27 15:30:49   浏览:2214次  

导读: 学了这么久Cocos2d-X,今天终于可以做出一个简单的小游戏了,游戏非常简单,通过菜单项控制精灵运动 在做游戏前,先学一个新概念 调度器(scheduler): Cocos2d-x调度器为...

学了这么久Cocos2d-X,今天终于可以做出一个简单的小游戏了,游戏非常简单,通过菜单项控制精灵运动

在做游戏前,先学一个新概念

调度器(scheduler):

Cocos2d-x调度器为游戏提供定时事件和定时调用服务。所有Node对象都知道如何调度和取消调度事件,使用调度器有几个好处:

每当Node不再可见或已从场景中移除时,调度器会停止。Cocos2d-x暂停时,调度器也会停止。当Cocos2d-x重新开始时,调度器也会自动继续启动。Cocos2d-x封装了一个供各种不同平台使用的调度器,使用此调度器你不用关心和跟踪你所设定的定时对象的销毁和停止,以及崩溃的风险。

使用调度器时用到的一些函数:

// 让帧循环调用this->update(float dt)函数

// scheduleUpdate();

// 让帧循环去调用制定的函数,时间还是1/60秒

// schedule(schedule_selector(T19Update::MyScheduleFunc));

// 定时器,每隔2秒调用T19Update::MyScheduleFunc函数

// schedule(schedule_selector(T19Update::MyScheduleFunc), 2.0f);

// 有限次数的定时器,当次数定义得很大时,也认为是无限的,实际次数是制定参数的+1

schedule(schedule_selector(T19Update::MyScheduleFunc), 1.0f, 3, 2.0f);

// 只调度一次的定时器

// scheduleOnce(schedule_selector(T19Update::MyScheduleFunc), 5.0f);

// 停止一个以参数为回调函数的定时器

unschedule(schedule_selector(T19Update::MyScheduleFunc));

// 停止update调度

unscheduleUpdate();

// 停止所有的调度

unscheduleAllSelectors();

熟悉了调度器的概念后就开始我们今天的重头戏,一个简单的游戏

首先在工程目录下的Resource文件中添加一张小球的图片

然后定义一个Update类

在Update.h类中添加下面的代码

#ifndef _Update_H_

#define _Update_H_

#include "cocos2d.h"

USING_NS_CC;

class Update : public CCLayer

{

public:

static CCScene* scene();

CREATE_FUNC(Update);

bool init();

void update(float dt);

CCSprite* _sprite;

void Handle(CCObject* sender);

//表示方向

int _direction;

//窗口的大小

CCSize winSize;

};

#endif

在Update.cpp中添加下面的代码

#include "Update.h"

CCScene* Update::scene()

{

CCScene* s = CCScene::create();

Update* layer = Update::create();

s->addChild(layer);

return s;

}

bool Update::init()

{

//初始化父类

CCLayer::init();

//得到窗口的大小

winSize = CCDirector::sharedDirector()->getWinSize();

//设置坐标

CCPoint center = ccp(winSize.width / 2, winSize.height / 2);

//让帧循环调用this->update(float dt)函数

scheduleUpdate();

//创建精灵

_sprite = CCSprite::create("green_edit.png");

addChild(_sprite);

//设置精灵的位置

_sprite->setPosition(center);

//创建菜单项

CCMenuItemFont* up = CCMenuItemFont::create("up", this, menu_selector(Update::Handle));

CCMenuItemFont* down = CCMenuItemFont::create("down", this, menu_selector(Update::Handle));

CCMenuItemFont* left = CCMenuItemFont::create("left", this, menu_selector(Update::Handle));

CCMenuItemFont* right = CCMenuItemFont::create("right", this, menu_selector(Update::Handle));

CCMenuItemFont* Stop = CCMenuItemFont::create("Stop", this,menu_selector(Update::Handle));

//创建菜单

CCMenu* menu = CCMenu::create(up, down, left, right, Stop, NULL);

addChild(menu);

//对齐菜单项

menu->alignItemsVertically();

//设置菜单项的ID

up->setTag(1);

down->setTag(2);

left->setTag(3);

right->setTag(4);

Stop->setTag(0);

//标记方向

_direction = -1;

return true;

}

void Update::Handle(CCObject* sender)

{

CCNode* node = (CCNode*)sender;

//得到菜单项的ID,ID对应的是精灵移动的方向

_direction= node->getTag();

}

void Update::update(float dt)

{

// _direction == 1表示精灵向上移动

if(1 == _direction)

{

//精灵向上移动

//50表示一秒钟移动50个像素

_sprite->setPositionY(_sprite->getPositionY() + dt * 100);

}

// _direction == 2表示精灵向下移动

if(2 == _direction)

{

//精灵向下移动

//50表示一秒钟移动50个像素

_sprite->setPositionY(_sprite->getPositionY() - dt * 100);

}

// _direction == 3表示精灵向左移动

if(3 == _direction)

{

//精灵向左移动

//50表示一秒钟移动50个像素

_sprite->setPositionX(_sprite->getPositionX() - dt * 100);

}

// _direction == 4表示精灵向右移动

if(4 == _direction)

{

//精灵向右移动

//50表示一秒钟移动50个像素

_sprite->setPositionX(_sprite->getPositionX() + dt * 100);

}

// _direction == 4表示精灵停止移动

if(0 == _direction)

{

}

}

执行结果:

参考资料:http://cn.cocos2d-x.org/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/framework/native/v3/scheduler/zh.md

赞助本站

人工智能实验室
相关内容
AiLab云推荐
展开

热门栏目HotCates

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