Cocos2d-X开发一个简单的小游戏

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

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

调度器(scheduler):

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

  1. 每当Node不再可见或已从场景中移除时,调度器会停止。
  2. Cocos2d-x暂停时,调度器也会停止。当Cocos2d-x重新开始时,调度器也会自动继续启动。
  3. 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)
    {
    }
}

执行结果:




  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值