cocos3——8.实现初学者指南

1.采用ClippingNode裁剪范围

  写作物接口:

function createClipNode(node, stencil, inverted) {
    var clip_node = new cc.ClippingNode();
    // 设置模板节点(就是要裁剪的区域)
    clip_node.stencil = stencil;
    // 加入要被裁剪掉的节点(显示的内容)
    clip_node.addChild(node);
    if (inverted != undefined) {
        // 设置反转(由于我们须要保留模板区域外面的内容,裁剪掉区域里的内容)
        clip_node.setInverted(inverted);
    }

    clip_node._stencil = stencil;
    return clip_node;
}

  在引导层创建裁剪节点:

    // create clip node
    var mask = cc.LayerColor.create(cc.color(0, 0, 0, ui.mask_a), ui.screen_width, ui.screen_height);
    var stencil = cc.LayerColor.create(cc.color.GREEN, 100, 100);
    stencil.ignoreAnchorPointForPosition(false);
    this.clip_node = createClipNode(mask, stencil, true);
    this.addChild(this.clip_node, ui.mask_z);
  这里是创建了一个全屏的黑色遮罩层,然后在上面裁剪掉stencil的区域。要改变区域,我们仅仅须要改变stencil的位置和大小就能够了。

  然后在引导层中写裁剪的函数:

node.clipNode = function (ref) {
    this.clip_ref = ref;
    var stencil = this.clip_node.stencil;
    if (ref) {
        stencil.setAnchorPoint(ref.getAnchorPoint());
        stencil.setContentSize(ref.getContentSize());
        stencil.setPosition(ref.getParent().convertToWorldSpace(ref.getPosition()));
    } else {
        // set out of screen
        stencil.setPosition(cc.p(10000, 10000));
    }
}
  这个函数就是用传进来的參考节点ref的锚点、大小、位置来设置模板的属性,这样就能按參考节点进行裁剪了。


2.引导的简单流程

  对于简单的引导实现,就是在引导開始的地方開始、引导结束的地方结束。

而什么时候開始、什么时候结束,假设量小且開始、结束条件都比較特殊的话,

就能够找到相关的地方開始和结束引导。

假设量大且条件比較一般化(比方button事件结束、滑动事件结束之类的),能够将条件 抽象话。然后进行配置。

  以下就说简单的方式吧,先准备一下引导開始和结束的接口。

  先从文件流获取上次引导的步数吧,这里用的local storage:

// local storage
var storage = {
    ls: cc.sys.localStorage,
};

storage.set = function (key, value) {
    this.ls.setItem(key, value);
}

storage.get = function (key) {
    var value = this.ls.getItem(key);
    if (value != '') {
        return value;
    }
}

storage.remove = function (key) {
    this.ls.removeItem(key);
}

// global interface
var guide = {
    node: node,
};

// arrow: // 0 down, 1 right, 2 up, 3 left
guide.steps = [
    // 0
    {
        name: 'btn_right',
        str: '请按住button,控制力度,将沙包扔进红色区域。

', arrow: 1, }, // ... ]; // 获取上次引导完毕的步数 guide.cur_step = storage.get('guide') || 0;

  然后准备開始和结束引导的接口:

guide.start = function (step) {
    if (step == this.cur_step) {
        console.log('guide start:', step, ',', this.cur_step);

        this.started = true;
        this._show(true);
        var info = this.steps[this.cur_step];
        this.node.updateData(info);
    }
}

guide.end = function (step) {
    if (!this.started) {
        return;
    }
    this.started = false;

    if (step == undefined) {
        step = this.cur_step;
    }
    if (step == this.cur_step) {
        console.log('guide end:', step, ',', this.cur_step);

        storage.set('guide', ++this.cur_step);
        this._show(false);
    }
}

guide._show = function (show) {
    if (show) {
        if (!this.node.getParent()) {
            this.node.init();
            ui.scene.addChild(this.node);
        }
    }
    this.node.visible = show;
}
  上面guide里的node就是引导界面的根节点。引导開始guide.start的时候,推断步数是当前步。就引导当前步,从上面配置的steps里面获取要引导的文字内容。

以及參考节点的名字(參考节点会挂到guide.start被调用的当前界面node对象下)、以及箭头等(文字、箭头的显示我就不多说了)。然后更新裁剪区域、显示文字、箭头等。在引导结束的时候将当前步数添加。

  而实际设计各个引导的时候,比方在第i步的时候,去開始的地方调用guide.start(i),在引导完的时候掉guide.end(i)就能够了。

这里设计的是简单的单线引导,对于简单的

新手引导已经够用了。

  

3.结果

  各位看官也累了。以下是我游戏《跳房子》里的截图,有兴趣的同学能够下来玩玩吧。多谢啦!

【下载地址】

《跳房子》游戏下载地址

http://zhushou.360.cn/detail/index/soft_id/2766861







版权声明:本文博主原创文章。博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/yxwkf/p/4856192.html