Astar777
2017-09-15 22:33

不是在函数内部吗 怎么可以传出来呢
init中的
this.x[i] 代表 aneObj.x[i], this其实就是指 aneObj, 而aneObj相对于 init 和draw 是外部的变量,所以他们都能访问的到 aneObj
无法传递,导致海葵出不来,后来干脆
//定义一个海葵对象的类
var aneObj = function()
{
};
//定义海葵的宽、高属性
var x = [];
var y = [];
//定义海葵个数
aneObj.prototype.num = 50;
//初始化确定每一个海葵的位置
aneObj.prototype.init = function()
{
for(var i = 0; i < this.num; i++) {
//宽
x[i] = i * 20 + Math.random() * 20;//Math.random()随机[0,1]
//高
y[i] = 200 + Math.random() * 50;
}
console.log(x[i]);
};
//绘制海葵
aneObj.prototype.draw = function()
{
//指定api区间样式生效
ctx2.save();
ctx2.globalAlpha = 0.6;
ctx2.lineWidth = '20';
ctx2.lineCap = 'round';
ctx2.strokeStyle = '#3b154e';
for(var i = 0; i < this.num; i++) {
//beginPath,moveTo,lineTo,strokeStyle,stroke,lineWidth,lineCap,globalAlpha
ctx2.beginPath();
ctx2.moveTo(x[i],canHeight);
ctx2.lineTo(x[i],canHeight - y[i]);
ctx2.stroke();
}
ctx2.restore();
};HTML5小游戏---爱心鱼(上)
92341 学习 · 583 问题
相似问题