我正在编写一个简单的 JavaScript 游戏。带有头像和障碍物。此时我在javascript“矩形”中模拟了一个类。代码在这里:
function rectangle (x,y,width,height,verticalvelocity,dangerous,image)
{
//returns info
this.x=x;
this.y = y;
this.height= height;
this.width=width;
this.verticalvelocity=verticalvelocity
this.jump= jump;
this.image=image
this.dangerous=dangerous
this.drawImg= function() {
context.drawImage(this.image,this.x,this.y,this.width,this.height)}
//getters
this.ycormdd=function () {return (this.y + (this.height /2));} //returns the y coor of the middlepoint
this.xcormdd= function () {return (this.x + (this.width /2));} //returns the x coor of the middlepoint
this.danger= function () {if (this.dangerous == 0) {return true} else {return false}};
//the setters
this.setdangerous= function (dangerous) {this.dangerous = dangerous};
this.setx= function (x) {this.x = x};
this.sety= function (y) {this.y = y};
this.setwidth= function (width) {this.width = width};
this.setheight= function (height) {this.height = height};
this.setimage= function (image) {this.image = image};
this.setverticalvelocity= function (verticalvelocity) {this.verticalvelocity=verticalvelocity};
}
问题是我的头像和障碍物都使用了矩形“类”,所以我输入
var avatar= new rectangle (....)
var obstacle= new rectangle (...)
这不是它的完成方式。据我了解,我需要做 3 节课。一类化身,一类障碍物和一类矩形。由于我的障碍物和头像都由矩形表示,我认为我的头像和矩形“类”都需要访问矩形类。但我完全不知道如何做到这一点:s。有人可以帮忙吗?提前致谢。我认为我未来的矩形“类”应该是这样的:
function rectangle (x,y,width,height,image)
{
//returns info
this.x=x;
this.y = y;
this.height= height;
this.width=width
this.image=image
//draws a rectangle
this.drawImg=function () {
context.drawImage(this.image,this.x,this.y,this.width,this.height)}
//getters
this.ycormdd=function () {return (this.y + (this.height /2));} //returns the y coor of the middlepoint
this.xcormdd= function () {return (this.x + (this.width /2));} //returns the x coor of the middlepoint
//the setters
this.setx= function (x) {this.x = x};
this.sety= function (y) {this.y = y};
this.setwidth= function (width) {this.width = width};
this.setheight= function (height) {this.height = height};
this.setImage = function (image) {this.image = image};
}
但是我需要创建一个头像和障碍类。
我在头像类中需要的功能是:
- 设定垂直速度
- 获得垂直速度
- (+矩形的功能)
对于我的障碍,我需要:
- 危险的
- 变得危险。
- (+矩形的功能)
我希望有人能理解我的问题。:p