javascript misc


function ChatTransport(win){
this.cc=1

this.test1=function(){
this.test11()
}
this.test11=function(){
alert(this.cc)
}
this.test2=function(){
test22();
}
var test22=function(){
alert(this.cc)
}

}

var ct = new ChatTransport();
ct.test1();
ct.test2();

js中的私有方法(用var定义的,非this)是取不到公有属性(this定义的属性)的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常抱歉,我之前的回答有误。在Cocos Creator中,确实没有提供`cc.randomMinusTo1()`和`.misc.degreesToRadians()`方法。 以下是修改后的示例代码: ```javascript cc.Class({ extends: cc.Component, properties: { player: { default: null, type: cc.Node }, speed: 100, // AI移动速度 maxRotation: 30, // 最大旋转角度(度) }, onLoad() { // 在游戏开始时,启动AI跟踪逻辑 this.schedule(this.trackPlayer, 0.1); }, trackPlayer() { // 获取敌人和玩家的位置 let enemyPos = this.node.position; let playerPos = this.player.position; // 计算敌人指向玩家的向量 let direction = playerPos.sub(enemyPos); // 获取一个随机的旋转角度(弧度制) let rotation = cc.math.degreesToRadians(Math.random() * this.maxRotation * 2 - this.maxRotation); // 将方向向量旋转一个随机的角度 direction.rotateSelf(rotation); // 标准化向量并乘以速度得到移动的位移 let distance = direction.normalize().mul(this.speed * 0.1); // 更新敌人的位置 this.node.setPosition(enemyPos.add(distance)); }, }); ``` 在上述示例代码中,我们使用`Math.random()`方法生成一个0到1之间的随机数,并将其乘以`this.maxRotation`的两倍,再减去`this.maxRotation`,从而得到一个范围在`-this.maxRotation`到`this.maxRotation`之间的随机角度(度)。然后,我们使用`cc.math.degreesToRadians()`方法将角度转换为弧度制。最后,我们将方向向量旋转该角度,并进行后续的位置计算。 希望这次能正确地解决你的问题。如果还有其他问题,请随时提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值