0

シンプルな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

4

2 に答える 2

0

プロトタイプなしで問題を解決しました。まだ長方形の「クラス」がありますが、アバターは次のように定義されています。

function avatar(rectangle,verticalvelocity){
//returns info
this.verticalvelocity=verticalvelocity;
//returnns the rectangle
this.getRect=function () {return rectangle;}
.
.
.
}

たとえば、アバターの x 座標が必要な場合は、次のように入力します。

avatar.getRect().getX()

これが誰かに役立つことを願っています;)

于 2013-10-18T14:13:00.763 に答える
0

コメントで説明されている継承は、恐ろしい多重継承につながる可能性があります。これは、ゲームがどれほど複雑になるかによって異なります。

デコレータと戦略のパターンを見てみましょう。 http://www.ycit-he.org/files/Resources/PHP%20Objects,%20Patterns,%20and%20Practice.pdfにはセクションがあります (これは php ですが、それほど重要ではありません)。

http://addyosmani.com/blog/decorator-pattern/ にはjavascriptコードがあります(私はそれを読んでいないので、それがどれほど関連性があるかわかりません.

PHPコードのリンクには、役に立つかもしれないゲームの「タイル」に関してデコレータを説明するセクションがあります。

わかりました、厳密な意味でデコレータを使用していませんが、すべてを継承しないことを示すためにまとめたコードを次に示します。これは優れたコードを意図したものではありませんが、「疑似デコレータ」を使用する方法を示すために使用できます。

// this is your basic game tile - presuming all tiles will have a name, can move or not and will have some attributes

game_tile = function(n, m, atts) {
    this.name = n;
    this.mobile = m;
    this.attributes = atts; 

    this.say = function() {
        message = 'i am ' + this.name + ' and i can ';
        if ( ! this.mobile ) {
            message += 'not';
        }
        message += ' move around the game board\n';
        for (i = 0; i < this.attributes.length; i++) {
            message += this.attributes[i].message();
        }
        alert(message);
    }
}

/* these next objects are 'attribute' objects for a game tile */

// this sets starting postion on game board
position = function(x, y) { 
    this.x = x;
    this.y = y;

    this.message = function() {
        return '\n i am at x = ' + this.x + ', y = ' +this.y;
    }
}

// this will draw the image - once the code to do it is written !
picture = function(src, w, h) { 
    this.image = src;   
    this.width = w;
    this.height = h;
    // code to draw image 

    this.message = function() {
        return '\n my image is ' + this.image + ' at size x = ' + this.width + ' y = ' + this.height;
    }
}

// stats for a character 
stats = function(hp, dmg) {
    this.health = hp;
    this.damage = dmg;

    this.message = function() {
        return '\n i have health = ' + this.health + ' and do damage = ' + this.damage;
    }
}

// a special ability 
ability = function(n, dmg) {
    this.name = n;
    this.damage = dmg;

    this.message = function() {
        return '\n i will ' + this.name + ' you for damage = ' + this.damage;
    }
}

// a player has a name, can move and position, picture & stats attributes
player1 = new game_tile('houdini', true, [
    new position(12, 12),
    new picture('mage.png', 24, 24),
    new stats(120, 120)
]);

// this river only has an image and a position
river = new game_tile('the river thames', false, [
    new position(25, 36),
    new picture('river.png', 240, 12),
]);

// a boss - same as a player but with a special ability
boss = new game_tile('ming the merciless', true, [
    new position(52, 52),
    new picture('boss.png', 24, 24),
    new stats(1200, 1200),
    new ability('crush', 80)
]); 

// they say something !
player1.say();
boss.say();
river.say();
于 2013-10-03T14:49:31.100 に答える