跟踪导弹
function(targetPosition){
// 让物体朝目标移动的方法
var speed = 5;
var targetPoint = targetPosition;
var thisPoint = cc.p(this.x, this.y);
//求两点的差值,事实上就是两点的坐标相减
var delta = cc.pSub(targetPoint, thisPoint);
// 求当前对象和目标两点间的距离
var distance = cc.pDistance(thisPoint, targetPoint);
// 计算行走后的点xy坐标
var x2 = thisPoint.x +speed *delta.x /distance;
var y2 = thisPoint.y + speed *delta.y/distance;
if(100>=distance){
return true;
}
// 改动当前对象的位置
var newPosition = cc.p(x2, y2);
this.setPosition(newPosition);
// 旋转对应的角度
var x1 = thisPoint.x;
var y1 = thisPoint.y;
var deltaRotation = 90-Math.atan2(y2-y1,x2-x1)*180/Math.PI;
this.setRotation(deltaRotation);
return false;
}效果请看游戏地址 http://www.seraph-fd.cn/games/1/index.html
时间: 2024-10-09 16:00:32