定义一个数,它可能为正 也可能为负 var num = Math.pow(-1,parseInt(Math.random() * 2) + 1);

for(var i = 0; i < 60; i++){
// 定义一个随机数范围从0 ~页面宽度
var x = parseInt(Math.random() * myCanvas.width);
// 定义一个随机数 范围从0 ~页面高度
var y = parseInt(Math.random() * myCanvas.height);
// 定义一个数,它可能为正 也可能为负
var num = Math.pow(-1,parseInt(Math.random() * 2) + 1);
// 定义一个随机数 决定star的速度 x方向
var x_speed = num * (parseInt(Math.random() * 10) + 1);
var num2 = Math.pow(-1,parseInt(Math.random() * 2) + 1)
// 定义一个随机数 决定鼠标star的速度 y方向
var y_speed = num2 * (parseInt(Math.random() * 10) + 1);

arr.push(new Star(x, y, x_speed, y_speed));

}

原文地址:https://www.cnblogs.com/oklfx/p/8303537.html