Javascript 无法使用JS在画布中移动弧

Javascript 无法使用JS在画布中移动弧,javascript,html5-canvas,Javascript,Html5 Canvas,我试图沿x轴移动圆弧,但它给出了一个错误,即x未在更新函数中定义。变量是否放置不正确或缺少某些引用 代码 var myCanvas=document.getElementById(“myCanvas”); var c=myCanvas.getContext(“2d”); var redArc=新圆圈(50,60,10,“红色”); var greenArc=新圆圈(80,60,15,“绿色”); var blueArc=新圆圈(120,60,20,“蓝色”); 函数圆(x、y、半径、颜色){

我试图沿x轴移动圆弧,但它给出了一个错误,即x未在更新函数中定义。变量是否放置不正确或缺少某些引用

代码


var myCanvas=document.getElementById(“myCanvas”);
var c=myCanvas.getContext(“2d”);
var redArc=新圆圈(50,60,10,“红色”);
var greenArc=新圆圈(80,60,15,“绿色”);
var blueArc=新圆圈(120,60,20,“蓝色”);
函数圆(x、y、半径、颜色){
这个.x=x;
这个。y=y;
这个半径=半径;
这个颜色=颜色;
this.draw=函数(){
c、 beginPath();
c、 弧(this.x,this.y,this.radius,0,Math.PI*2);
c、 fillStyle=this.color;
c、 填充();
}
this.update=函数(){
redArc.x+=1;
绿弧x+=1;
蓝弧x+=1;
这个.draw();
}
这个.update();
}        
函数animate(){
请求动画帧(动画);
c、 clearRect(0,0,myCanvas.clientWidth,myCanvas.clientHeight);
update();
greenac.update();
blueArc.update();
}
制作动画();
我如何着手修理它?有什么建议吗
谢谢

将您的
更新方法替换为以下方法:

this.update = function() {
   this.x += 1;
   this.draw();
}
您应该使用
this.x
而不是变量名

var myCanvas=document.getElementById(“myCanvas”);
var c=myCanvas.getContext(“2d”);
var redArc=新圆圈(50,60,10,“红色”);
var greenArc=新圆圈(80,60,15,“绿色”);
var blueArc=新圆圈(120,60,20,“蓝色”);
函数圆(x、y、半径、颜色){
这个.x=x;
这个。y=y;
这个半径=半径;
这个颜色=颜色;
this.draw=函数(){
c、 beginPath();
c、 弧(this.x,this.y,this.radius,0,Math.PI*2);
c、 fillStyle=this.color;
c、 填充();
}
this.update=函数(){
这个.x+=1;
这个.draw();
}
这个.update();
}
函数animate(){
c、 clearRect(0,0,myCanvas.clientWidth,myCanvas.clientHeight);
update();
greenac.update();
blueArc.update();
请求动画帧(动画);
}
制作动画()

用以下方法替换您的
更新
方法:

this.update = function() {
   this.x += 1;
   this.draw();
}
您应该使用
this.x
而不是变量名

var myCanvas=document.getElementById(“myCanvas”);
var c=myCanvas.getContext(“2d”);
var redArc=新圆圈(50,60,10,“红色”);
var greenArc=新圆圈(80,60,15,“绿色”);
var blueArc=新圆圈(120,60,20,“蓝色”);
函数圆(x、y、半径、颜色){
这个.x=x;
这个。y=y;
这个半径=半径;
这个颜色=颜色;
this.draw=函数(){
c、 beginPath();
c、 弧(this.x,this.y,this.radius,0,Math.PI*2);
c、 fillStyle=this.color;
c、 填充();
}
this.update=函数(){
这个.x+=1;
这个.draw();
}
这个.update();
}
函数animate(){
c、 clearRect(0,0,myCanvas.clientWidth,myCanvas.clientHeight);
update();
greenac.update();
blueArc.update();
请求动画帧(动画);
}
制作动画()

正常。。谢谢这是可行的,但是引用变量和这个.x不是一回事吗?如果我在这个.x或变量x位置上设置增量,会有区别吗?在创建对象之前不能使用对象。好的。。谢谢这是可行的,但是引用变量和这个.x不是一回事吗?如果我在这个.x或变量x位置上设置增量会有区别吗?在创建对象之前不能使用对象。