码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2d_x_04_计时器_数据存储

时间:2014-10-05 18:10:28      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:cocos2d   cocos2d-x   json   plist   xml   

一、计时器的使用
效果图:点击屏幕,移动头像至指定位置后,停止计时器
bubuko.com,布布扣
类型为SEL_SCHEDULE实质是一个函数指针,指向的是Ref的一个成员方法,参数float,返回值void
bubuko.com,布布扣
场景的.h头文件
bubuko.com,布布扣
场景类的实现.cpp文件
关键代码:
bubuko.com,布布扣
bubuko.com,布布扣
//
//  UpdateScene.cpp
//  01_cocos2d-x
//
//  Created by beyond on 14-10-5.
//
//

#include "UpdateScene.h"
USING_NS_CC;
Scene* UpdateScene::createScene()
{
    // 'scene' 自动释放
    // 创建一个scene
    auto scene = Scene::create();
    // 'layer' 自动释放
    auto layer = UpdateScene::create();
    // 将图层 添加到场景中
    scene->addChild(layer);
    // 返回 填充好图层的 场景
    return scene;
}

// 在 "init" 方法中,实例化自己要用的精灵对象
bool UpdateScene::init()
{
    // 1. 调用父类的init , cpp 没有super,直接写父类名
    if ( !Layer::init() ) return false;
    // 屏幕尺寸
    winSize = Director::getInstance()->getVisibleSize();
    
    // 添加 一个精灵,点击屏幕后,精灵在update方法中 更改位置
    addSprite();
    
    // 添加一个LabelTTF,点击文字后,在updatePosition方法中 更改位置
    addLabelTTF();
    return true;
}
#pragma mark - 初始化
// 添加一个精灵,点击屏幕后,精灵在update方法中 更改位置
void UpdateScene::addSprite()
{
    // 精灵精灵Nana
    nana = Sprite::create("nanaLogo.png");
    nana->setAnchorPoint(Point(0,0));
    nana->setPosition(Point(0,0));
    this->addChild(nana);
    
    // 2.触摸屏幕,开启 时钟update
    
    // 实例化一个触摸监听器 对象
    auto listener = EventListenerTouchOneByOne::create();
    // 当触摸开始时,绑定一个闭包函数;
    // 【】表示 要传入的外界对象,此处是this
    // ()表示参数
    listener->onTouchBegan = [this](Touch *t,Event *e){
        // 开启默认的 时钟方法
        scheduleUpdate();
        return false;
    };
    // 5、获取事件分发器,添加一个事件监听器,到this身上;即监听的是this对象【整个图层Layer】
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
    
}
// 添加一个LabelTTF,点击文字后,在updatePosition方法中 更改位置
void UpdateScene::addLabelTTF()
{
    // Label
    label = LabelTTF::create("Nana","Courier",90);
    label->setAnchorPoint(Point(0,0));
    label->setPosition(Point(0,0));
    label->setName("label");
    addChild(label);
    
    // 2.触摸Label,开启 时钟updatePosition
    // 实例化一个触摸监听器 对象
    auto listener = EventListenerTouchOneByOne::create();
    // 当触摸开始时,绑定一个闭包函数;
    // 【】表示 要传入的外界对象,此处是this
    // ()表示参数
    listener->onTouchBegan = [this](Touch *t,Event *e){
        // 如果 点击 了label,才每隔一秒执行一次 更新位置方法
        LabelTTF *label =(LabelTTF *) e->getCurrentTarget()->getChildByName("label");
        if (label->getBoundingBox().containsPoint(t->getLocation())) {
            // 开启指定时间的 时钟方法;参数是:函数指针,返回值是void,参数是float,指向的是Ref内的一个方法
            schedule(schedule_selector(UpdateScene::updatePosition), 1);
        }
        return false;
    };
    // 5、获取事件分发器,添加一个事件监听器,到this身上;即监听的是this对象【整个图层Layer】
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
    
}
#pragma mark - 时钟方法
// 时钟方法,使用的是默认的帧率 1/60
void UpdateScene::update(float dt)
{
    // 向右上角,移动nana,当位置大于 400时,stop
    nana->setPosition(nana->getPosition()+Point(3,3));
    if (nana->getPosition().x>400) {
        // 停止时钟方法
        unscheduleUpdate();
    }
    
}

// 时钟方法,使用的是 1秒1次
void UpdateScene::updatePosition(float dt)
{
    // 向右上角,移动nana,当位置大于 400时,stop
    label->setPosition(label->getPosition()+Point(50,50));
    if (label->getPosition().x>300) {
        // 停止所有时钟方法
        unscheduleAllSelectors();
    }
    
}




二、用户偏好UserDefault
bubuko.com,布布扣
bubuko.com,布布扣
三、FileUtils文件操作工具类
它屏蔽了不同的平台mac ios android等
bubuko.com,布布扣
bubuko.com,布布扣
FileUtils文件写出和读入演示
如果是Mac平台,则可用目录就是:【/Users/beyond/Documents/
如果是iOS平台,则可用目录就是:【.../app/Documents/
bubuko.com,布布扣
四、Plist文件读取
bubuko.com,布布扣
使用的依然是FileUtils工具类
bubuko.com,布布扣
返回的类型是:字典【ValueMap 数组【ValueVector
bubuko.com,布布扣
五、XML文件读取



六、JSON文件读取























cocos2d_x_04_计时器_数据存储

标签:cocos2d   cocos2d-x   json   plist   xml   

原文地址:http://blog.csdn.net/pre_eminent/article/details/39802839

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!