2011-12-14 9 views
1

注意が必要、それは私はOOPと継承してグリップになっOOPと継承のアドバイスは

:)上のアドバイスを探しています何の下に掲示コードの多くが、そのほとんどは、すべて同じありますです、私はよ"パドル"クラスと "ボム"クラスで見つかった重複コードをスプライトという別のクラスに分けようとしています。私の爆弾とパドルクラスのメンバ変数を扱っているのは分かりません。私は両方のクラスで同じ変数を使いたいと思っていて、スプライトクラスに移動する必要があります。しかし、後で私のより特殊な爆弾とパドルのクラスでそれらを使用することについて私は確信していません。私はそれらを保護することができますが、これは私がより専門的なすべてのクラスが正確に共有している状況で私を残すので、同じ変数は確かに、私は絶対に望んでいない、私はちょうどそれらにそれらの変数のコピーを持ってほしい。ああ私は知らない、私のクラスは瞬間にはかなり単純です、場合は、それらは、と同じもの(ほとんどゲッター&セッター、描画メソッドと更新メソッド)と私はどのように私は非常に感謝するだろう私のスプライトクラスにコードを移動するつもりだ提案を参照してください!

まず専門のクラス、爆弾:

package biz.hireholly.pirateponggame; 

import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.util.Log; 

public class Bomb { 
    private Bitmap bitmap; //image 
    private int x; //x coordinate 
    private int y; //y coordinate 

    private Speed speed; //the speed with its directions 

    public Bomb(Bitmap bmp, int x, int y){ 
     this.bitmap = bmp; 
     this.x = x; 
     this.y = y; 
     this.speed = new Speed(); 
    } 

    public Bitmap getBitmap(){ 
     return bitmap; 
    } 
    public void setBitmap(Bitmap bmp){ 
     this.bitmap = bmp; 
    } 
    public int getX(){ 
     return x; 
    } 
    public int getY(){ 
     return y; 
    } 
    public void setX(int x){ 
     this.x = x; 
    } 
    public void setY(int y){ 
     this.y = y; 
    } 
    public Speed getSpeed() { 
     return speed; 
    } 
    public void setSpeed(Speed newSpeed) { 
     this.speed = newSpeed; 
    } 

    /** 
    * X&Y += current velocity * current direction 
    */ 
    public void update(){ 
     x+= (speed.getXv() * speed.getxDirection()); 
     y+= (speed.getYv() * speed.getyDirection()); 
    } 

    /** 
    * takes the bitmap it was instantiated with. 
    * Draws it to the canvas at the coordinates the bomb is at in that moment. 
    * @param canvas 
    */ 
    public void draw(Canvas canvas){ 

     canvas.drawBitmap(bitmap, x-(bitmap.getWidth() /2), y-(bitmap.getHeight() /2), null); 

    } 

    public void handleWallCollision(int viewW, int viewH){ 

     //check collision with right wall if heading right 
     if (speed.getxDirection() == Speed.RIGHT //if going right 
       && getX() + (bitmap.getWidth() /2) >= viewW){ //and the centre of the bitmap become greater than the view width 
      //reverse x direction 
      speed.toggleXDirection(); 
     } 
     //check for collision with left wall if heading left 
     if (speed.getxDirection() == Speed.LEFT 
       && getX() - (bitmap.getWidth() /2) <= 0){ 
      //reverse x direction 
      speed.toggleXDirection(); 
     } 
     //check collision with bottom wall if heading down 
     if (speed.getyDirection() == Speed.DOWN 
       && getY() + (bitmap.getHeight() /2) >= viewH){ 
      //reverse y direction 
      speed.toggleYDirection(); 
     } 
     //check collision with top wall if heading up 
     if (speed.getyDirection() == Speed.UP 
       && getY() - (bitmap.getHeight() /2) <= 0){ 
      speed.toggleYDirection(); 
     } 
    } 
    public void handlePaddleCollision(Paddle pad){ 
     //THIS WILL NEED TO WORK FOR BOTH PADDLES 


     int paddleHalfH = pad.getBitmap().getHeight(); 
     int paddleHalfW = pad.getBitmap().getWidth(); 
     //int bombHalfW = bitmap.getWidth(); 
     int bombHalfH = bitmap.getHeight(); 

     //first check if the bombs x is within the paddles x 
     if(x >= (pad.getX() - paddleHalfW) //x > paddle leftside 
      && x <= (pad.getX() + paddleHalfW)) // x < paddle rightside 
     { 

      if((y+bombHalfH) >= (pad.getY() + paddleHalfH)) //base of bomb is touching top of paddle 
      { 
       speed.toggleYDirection(); 
      } 

     } 



    } 

} 

は、その後で同じものをたくさん持つ私のパドルクラスがあります:

package biz.hireholly.pirateponggame; 

import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.util.Log; 
import android.view.MotionEvent; 

public class Paddle { 
    private static final String TAG = Paddle.class.getSimpleName(); 
    private Bitmap bitmap; //image 
    private Speed speed; //the speed with its directions 

    private int x; //x coordinate 
    private int y; //y coordinate 
    private boolean touched; 

    public Paddle(Bitmap bmp, int x, int y){ 
     this.bitmap = bmp; 
     this.x = x; 
     this.y = y; 
     this.speed = new Speed(); 
    } 

    public void update(){ 
     //x+= (speed.getXv() * speed.getxDirection()); 
    } 

    /** 
    * takes the bitmap it was instantiated with. 
    * Draws it to the canvas at the coordinates the bomb is at in that moment. 
    * @param canvas 
    */ 
    public void draw(Canvas canvas){ 
     //just to make things easier to read 
     int halfX = bitmap.getWidth() /2; 
     int halfY = bitmap.getHeight() /2; 

      canvas.drawBitmap(bitmap, x-halfX, y-halfY, null); 

    } 

    /** 
    * checks if player is touching paddle 
    * @param eventx 
    * @param eventy 
    */ 
    public void handleActionDown (int eventx, int eventy){ 
     int extray = 20; //extra height desired so that paddle can be easily touched 

     if(eventx >= (x - bitmap.getWidth()/2) && //touch within paddles width 
       eventx <= (x + bitmap.getWidth()/2)){ 

       if (eventy >= (y - bitmap.getHeight() + extray) && //within height 
         eventy <= (y + bitmap.getHeight()) + extray) { 

         setTouched(true); 
       } 
       else { setTouched(false); } 
     } 
     else { setTouched(false); } 

    } 

    public void onTouchEvents(MotionEvent e, int viewW){ 
     //DETECT PRESS 
     if (e.getAction() == MotionEvent.ACTION_DOWN){ 
      // delegating event handling to the paddle 
      handleActionDown((int)e.getX(), (int)e.getY()); 
     } 
     //MOVE GESTURES 
     if (e.getAction() == MotionEvent.ACTION_MOVE){ 
      if (isTouched()){ 
       //paddle is being dragged 

       //SETTING NEW POSITION 
       setX((int)e.getX()); 

       if (getX() - (bitmap.getWidth() /2) <= 0) 
       {//Left wall collision 
        setX (0 + (bitmap.getWidth() /2)); 
       } 
       if (getX() + (bitmap.getWidth()/2) >= viewW) 
       {//right wall collision 
        setX(viewW - (bitmap.getWidth()/2));     
       } 

      } 
     } 
     //PRESS RELEASED 
     if (e.getAction() == MotionEvent.ACTION_UP){ 
      if (isTouched()){ 
       //no longer being dragged 
       setTouched(false); 
      } 
     } 
    } 

    public Speed getSpeed() { 
     return speed; 
    } 
    public void setSpeed(Speed newSpeed) { 
     this.speed = newSpeed; 
    } 

    public int getX() { 
     return x; 
    } 
    public void setX(int x) { 
     this.x = x; 
    } 
    public int getY() { 
     return y; 
    } 
    public void setY(int y) { 
     this.y = y; 
    } 
    public Bitmap getBitmap(){ 
     return bitmap; 
    } 
    public void setBitmap(Bitmap bmp){ 
     this.bitmap = bmp; 
    } 
    public boolean isTouched() { 
     return touched; 
    } 
    public void setTouched(boolean touched) { 
     this.touched = touched; 
    } 
} 

だから私はSprite.javaはこのようになります数えます..

package biz.hireholly.pirateponggame; 

import android.graphics.Bitmap; 
import android.graphics.Canvas; 

public class Sprite { 
    private Bitmap bitmap; //image 
    private int x; //x coordinate 
    private int y; //y coordinate 

    private Speed speed; //the speed with its directions 

    public Bitmap getBitmap(){ 
     return bitmap; 
    } 
    public void setBitmap(Bitmap bmp){ 
     this.bitmap = bmp; 
    } 
    public int getX(){ 
     return x; 
    } 
    public int getY(){ 
     return y; 
    } 
    public void setX(int x){ 
     this.x = x; 
    } 
    public void setY(int y){ 
     this.y = y; 
    } 
    public Speed getSpeed() { 
     return speed; 
    } 
    public void setSpeed(Speed newSpeed) { 
     this.speed = newSpeed; 
    } 


    public void update(){ 
     x+= (speed.getXv() * speed.getxDirection()); 
     y+= (speed.getYv() * speed.getyDirection()); 
    } 


    public void draw(Canvas canvas){ 

     canvas.drawBitmap(bitmap, x-(bitmap.getWidth() /2), y-(bitmap.getHeight() /2), null); 

    } 

} 

答えて

2

私がしようとしています 「パドル」クラスと「ボム」クラスで見つかった重複したコードをスプライトとは別のクラスに分けます。何が 私の 爆弾とパドルクラスで現在メンバー変数を処理しているか分かりません。 の両方のクラスで同じ変数を使用したいと思っていて、スプライトクラスに移動する必要があります。しかし、 それから私のより特殊な爆弾と 後でパドルクラスでそれらを使用することについて私は確信していない私はそれらを保護することができますが 私は状況に私を残すだろう 私のより専門クラス正確に同じ変数を共有しています 私は間違いなく、私はそれらの変数のコピー を持ってほしいです。ああ、私は、彼らが同じ変数を共有ないある

知りません。上記の例では

abstract class Sprite { 
    protected Field a; 
    protected Field b; 
} 

class Bomb extends Sprite { 

} 

class Paddle extends Sprite { 

} 

、作成した各BombインスタンスまたはPaddleインスタンスがab独自のを持っています。 でフィールドを共有したい場合は、staticと宣言する必要があります。

+0

OHHHよく私のために多くをクリアして、どこからその印象を得たかわからない。どうもありがとうございました!私はより良い勉強/練習する:)。あなたは私の問題を解決するために使用するべき方法は、保護された変数を変更すると言うでしょうか?私は理解していないより良い選択肢があるかもしれないので尋ねるだけです – Holly

+1

あなたは正しい質問をしています。 OOPでは、メンバーフィールドの範囲を可能な限り縮小することが一般に考えられています。あなたの特定の例では、スーパークラスの保護されたフィールドが正しい方法だと私は思っています。 –

1

Spriteクラスを作成するのは良いステップですOOPの精神です。あなたも、さらに行くと、あなたが状況に応じて、あなたがSprite直接、または特別な変種から継承することができることをしたらSprite

から継承SpecialPaddleSpriteSpecialBombSpriteを持つことができます。この方法で、コードをOOPで再利用できます。

だから、クラス階層が

  ------> SpecialPaddleSprite ------> SpecialPaddle 
    /
Sprite --------> SpecialBombSprite ------> SpecialBomb 
     \ 
     -------> Paddle 
     \ 
      -------> Bomb 

ように見えるかもしれませんあなたは特別なパドルや爆弾の多くの種類を持っていた場合は、この方法では、彼らの共通の行動はSpecialPaddleSpriteにすることができ、SpecialBombSprite