Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中随机定位正方形?_Javascript_Random_Components - Fatal编程技术网

在JavaScript中随机定位正方形?

在JavaScript中随机定位正方形?,javascript,random,components,Javascript,Random,Components,代码不起作用,我需要随机定位一个正方形组件。我试着运行一些代码,但什么也没发生。这是我的密码: 功能组件(宽度、高度、颜色、x、y、类型){ this.type=type; 如果(类型==“图像”){ this.image=新图像(); this.image.src=颜色; } 这个分数=0; 这个。宽度=宽度; 高度=高度; 这是0.speedX=0; 该值为0; 这个.x=x; 这个。y=y; 这个重力=0; 这个重力速度=0; this.update=函数(){ ctx=myGameAr

代码不起作用,我需要随机定位一个正方形组件。我试着运行一些代码,但什么也没发生。这是我的密码:

功能组件(宽度、高度、颜色、x、y、类型){
this.type=type;
如果(类型==“图像”){
this.image=新图像();
this.image.src=颜色;
}
这个分数=0;
这个。宽度=宽度;
高度=高度;
这是0.speedX=0;
该值为0;
这个.x=x;
这个。y=y;
这个重力=0;
这个重力速度=0;
this.update=函数(){
ctx=myGameArea.context;
if(this.type==“image”){
drawImage(this.image,this.x,this.y,this.width,this.height);
}否则{
ctx.fillStyle=颜色;
ctx.fillRect(this.x,this.y,this.width,this.height);
}
}
}
函数startName(){
随机=数学地板((数学随机()*100)+1);
文件。书写(随机);
random2=数学地板((数学随机()*100)+1);
文件编写(2);
平方=新组件(随机、随机2、“绿色”、随机、随机2);
myGameArea.start();
返回广场
}
var myGameArea={
画布:document.createElement(“画布”),
开始:函数(){
this.canvas.width=450;
this.canvas.height=270;
this.context=this.canvas.getContext(“2d”);
document.body.insertBefore(this.canvas,document.body.childNodes[0]);
this.interval=setInterval(updateGameArea,1);
},
清除:函数(){
this.context.clearRect(0,0,this.canvas.width,this.canvas.height);
}
}
函数updateGameArea(){
myGameArea.clear();
square.update();

}
您可以通过如下方式应用代码使其正常工作,例如:

功能组件(宽度、高度、颜色、x、y、类型){
this.type=type;
如果(类型==“图像”){
this.image=新图像();
this.image.src=颜色;
}
这个分数=0;
这个。宽度=宽度;
高度=高度;
这是0.speedX=0;
该值为0;
这个.x=x;
这个。y=y;
这个重力=0;
这个重力速度=0;
这个。颜色=颜色;
this.update=函数(){
随机=数学地板((数学随机()*100)+1);
random2=数学地板((数学随机()*100)+1);
函数getRandomColor(){
变量字母='0123456789ABCDEF';
var color='#';
对于(变量i=0;i<6;i++){
颜色+=字母[Math.floor(Math.random()*16)];
}
返回颜色;
}
randcolor=getRandomColor();
ctx=myGameArea.context;
if(this.type==“image”){
ctx.drawImage(this.image,random,random2,random,random2);
}否则{
ctx.fillStyle=randcolor;
ctx.fillRect(随机,随机2,随机,随机2);
}
这个.x=随机;
这个.y=2;
这个。宽度=随机;
这个。高度=2;
this.color=randcolor;
}
}
函数startName(){
随机=数学地板((数学随机()*100)+1);
random2=数学地板((数学随机()*100)+1);
平方=新组件(随机、随机2、“绿色”、随机、随机2);
myGameArea.start();
返回广场
}
var myGameArea={
画布:document.createElement(“画布”),
开始:函数(){
this.canvas.width=450;
this.canvas.height=270;
this.context=this.canvas.getContext(“2d”);
document.body.insertBefore(this.canvas,document.body.childNodes[0]);
this.interval=setInterval(updateGameArea,1);
},
清除:函数(){
this.context.clearRect(0,0,this.canvas.width,this.canvas.height);
}
}
函数updateGameArea(){
myGameArea.clear();
square.update();
}
document.getElementById(“开始”).addEventListener(“单击”,开始名称)

按启动
您必须调用startName函数
startGame()
如果一切正常,你可能想尝试接受答案:)顺便说一句,H.Figueiredo做了一个很好的编辑,添加了颜色并自动重启游戏