シンプルなJavaScriptゲームを書いています。アバターと障害物。この時点で、javascript「rectangle」でクラスをシミュレートしました。コードは次のとおりです。
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つのクラスを作成する必要があります。1 つのクラスのアバター、1 つのクラスの障害物、および 1 つのクラスの長方形。障害物とアバターの両方が長方形で表されているため、アバターと長方形の両方の「クラス」が長方形クラスにアクセスする必要があると思います.しかし、これを行う方法がまったくわかりません: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