cc.follow 简单使用

小白菜一枚,今天简单测试了一下cc.follow的用法。个人感觉是“移动摄像头”,比直接移动节点简单些。

测试例子已经传到码云上了。

地址https://gitee.com/wj12318/followTest.git
 

制作步骤:

1.层级管理器中结构如下,其中hero是bg的子节点。

2.添加两个脚本文件,follow挂在bg下,gameCtrl.js挂在节点gameCtrl节点下。

其中gameCtrl.js代码如下,作用是游戏开始时让主角向右边移动一段距离(挂在gameCtrl节点下)

//gameCtrl.js

cc.Class({
    extends:cc.Component,
    properties:{
        hero:cc.Node,
    },
    onLoad:function(){
        this.hero.runAction(cc.moveTo(3,2000,0));
    },
});

cc.follow的用法有两种,一种是 cc.follow(target,cc.rect());一种是cc.follow(target);我用的是第二种(貌似第一种是限定在一个矩形区域内进行跟踪,第二种没有这种限制)。如下是follow.js代码,挂在bg节点下。

//follow.js
cc.Class({
    extends: cc.Component,

    properties: {
        hero:cc.Node,
    },

    onLoad:function(){
        this.node.runAction(cc.follow(this.hero));
    },
    start () {

    },
});

 挂载情况如下:

 测试结果(跟随还不错,就是有些时候有点卡顿。。。可能我还没掌握到其中的奥秘)

猜你喜欢

转载自blog.csdn.net/anywn_future/article/details/81394555
cc