Quantcast
Channel: Cocos中文社区 - 最新主题
Viewing all articles
Browse latest Browse all 88862

实现超车的功能。大家看看这方法是不是很笨。

$
0
0

@wansuiwht 写道:


如上图。做了这么一个超车的检测。
在我的车 前部 和后部 各放一个碰撞组件。
我用了一个全局的数组。

当我的车追上小红车时,碰到前部的碰撞组件,然后在数组里存入信息。
我存的是 小红车的UUID + "in" + 碰撞组件节点的名称。
代码是这样的。
onCollisionEnter: function (other,self) {
if (-1 == overtake.indexOf(other.node.uuid+"in" + self.node.name))
overtake.push(other.node.uuid+"in" + self.node.name);
},
碰撞停留时存入一个信息。
onCollisionStay: function (other,self) {
if (-1 == overtake.indexOf(other.node.uuid+"true" + self.node.name))
overtake.push(other.node.uuid+"true" + self.node.name);
},
离开前部碰撞组件时
onCollisionExit: function (other,self) {
if (-1 == overtake.indexOf(other.node.uuid+"out" + self.node.name))
overtake.push(other.node.uuid+"out" + self.node.name);
overtake.splice(overtake.indexOf(other.node.uuid+"true" +self.node.name),1);
},

下面是后部碰撞组组件的脚本。
onCollisionEnter: function (other,self) {
if (-1 == overtake.indexOf(other.node.uuid+"in" + self.node.name))
overtake.push(other.node.uuid+"in" + self.node.name);
},

onCollisionStay: function (other) {
    // console.log('on collision stay');

},

onCollisionExit: function (other,self) {//后部碰撞离开时,表明已经超过小红车,
    if ((overtake.indexOf(other.node.uuid+"in" +"ColliderFront") != -1) &&
        (overtake.indexOf(other.node.uuid+"out" +"ColliderFront") != -1) &&
        (overtake.indexOf(other.node.uuid+"true" +"ColliderFront") == -1) && //如果这条信息存在,说明小红车还未离开前部碰撞组件。
       ( overtake.indexOf(other.node.uuid+"in" + self.node.name != -1)))// 通过四个条件判断车辆已经超过小红车,
        {
           this.la.string = "你超过了" + other.node.uuid;

           overtake.splice(overtake.indexOf(other.node.uuid+"in" +"ColliderFront"),1);//完成超车,删除所有碰撞信息
        }
},

这种方法是不是比较笨啊。是不是通过坐标计算更好点。

帖子: 1

参与者: 1

阅读整个主题


Viewing all articles
Browse latest Browse all 88862

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>