LoginSignup
1
3

More than 5 years have passed since last update.

テトリスをつくってみました 説明10 この仕事はどっちのオブジェクトだ? tetris2.js

Last updated at Posted at 2016-01-02

render.nullBlock = 'true';
renderオブジェクトのnullBlockプロパティにtrueを入れています。
playerBlockは現在プレイヤーが動かせるブロックです。
そのコピーを作って(ただし色は背景の白)描画させています。
そうやって消します。

オブジェクトの主従関係

render.message
render.draw()

renderオブジェクトとなにかやっています。
上は、プロパティに値を入れています。下は、関数を呼び出しています。
メソッドを使うとき、renderオブジェクトを呼び出した側が利用している感じがします。
呼び出した元 callee
呼び出されたほう caller
moveBlock関数のrender.drawBlockなら、以下のようでしょうか。
callee moveBlock
caller render.drawBlock

moveBlockは、プレイヤーのテトリスブロックを動かすことを担当しています。
その責務における限り、moveBlockは主であるべきです。

render.drawBlock() と moveBlockからやっています。

これはどうでしょうか?

ブロックを描画するのはrenderの責務です。
ブロックを動かすのはmoveBlockの責務です。

どちらにも主になる権利がありそうです。

render.drawaBlock (render.drawBlock() ではない)

これなら少し控えめな気がします。
render.drawBlock = 'down'とでもします。
シグナル、フラグ、イベント見たいな感じです。
ポイントは、この件はcallerに任せます」という意味合いです。

オブジェクト指向で書く場合、オブジェクトの領分をきちんとすべきです。


        var nullBlock ={};
        nullBlock.tetrisBlock = {};
        nullBlock.tetrisBlock.blockColor = INIT_BACKCOLOR;
        nullBlock.tetrisBlock.blockShape = [].concat(playerBlock.tetrisBlock.blockShape );
        render.nullBlock = 'true';
//nullBlock組み立て完了
        render.drawBlockType ( playerBlock.x, playerBlock.y, nullBlock.tetrisBlock );

render.drawBlockメソッドで、nullBlockを渡しています。
nullBlockを組み立てるのはmoveBlockでやるべきだったでしょうか。

render.onestep(x ,y, rotate);
たとえば、x軸方向に+1だけ動かしてください。
render.onestep(1);
こう、書いて終わらしてしまう書き方もできます。(renderオブジェクトに後は任せてしまうわけです)

結論:オブジェクトの責務を意識しよう

エキスパートの方の意見を募集します。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3