Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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,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,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.引导的简单流程

对于简单的引导实现,就是在引导开始的地方开始、引导结束的地方结束。而什么时候开始、什么时候结束,如果量小且开始、结束条件都比较特殊的话,

就可以找到相关的地方开始和结束引导。如果量大且条件比较一般化(比如按钮事件结束、滑动事件结束之类的),可以将条件 抽象话,然后进行配置

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

先从文件流获取上次引导的步数吧,这里用的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: '请按住按钮,控制力度,将沙包扔进红色区域。',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:',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对象下)、以及箭头等(文字、箭头的显示@R_262_6963@),然后更新裁剪区域、显示文字、箭头等。在引导结束的时候将当前步数增加。

而实际设计各个引导的时候,比如在第i步的时候,去开始的地方调用guide.start(i),在引导完的时候掉guide.end(i)就可以了。这里设计的是简单的单线引导,对于简单的

新手引导已经够用了。

3.结果

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

下载地址】

《跳房子》游戏下载地址

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





大佬总结

以上是大佬教程为你收集整理的cocos3——8.实现新手引导全部内容,希望文章能够帮你解决cocos3——8.实现新手引导所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。