2

Androidでゲームの重力を作成しようとしています。更新メソッド、表示、重力を作成しました。現在、アプリは強制的に閉じませんが、ボールは動かないだけです。.getHieght() および .getWidth() メソッドにキャンバスを使用する必要がありますか?

public class MainActivity extends Activity {
private int c = Color.YELLOW;
private Canvas g;
@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
draw();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


// draws the ball
public void draw ()
{
Display display = getWindowManager().getDefaultDisplay();

int width = display.getHeight();
int height = display.getWidth();

Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);

g = new Canvas(bitmap);
g.drawColor(c);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
g.drawCircle(50, 10, 25, paint); //Put your values
update();

// In order to display this image, we need to create a new ImageView that we can        display.
ImageView imageView = new ImageView(this);

// Set this ImageView's bitmap to ours
imageView.setImageBitmap(bitmap);

// Create a simple layout and add imageview to it.
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new     RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(imageView, params);
layout.setBackgroundColor(Color.BLACK);

// Show layout in activity.
setContentView(layout);   
}
private void update(){
// wall collisions;
    if (x + dx > sp.getWidth() - radius - 1) {
        x = sp.getWidth() - radius - 1;
        // bounce off right wall;
        dx = -dx;
        // bounce off left wall;
    } else if (x + dx < 0 + radius) {
        x = 0 + radius;
        dx = -dx;
    } else {
        x += dx;
    }

    // friction;
    if (y == sp.getHeight() - radius - 1) {
        dx *= xFriction;
        if (Math.abs(dx) < .8) {
            dx = 0;
        }
    }
    // gravity;
    if (y > sp.getHeight() - radius - 1) {
        y = sp.getHeight() - radius - 1;
        dy *= energyloss;
        dy = -dy;
    } else {
        // velocity formula;
        dy += gravity * dt;
        // position formula;
        y += dy * dt + .5 * gravity * dt * dt;

    }

}

}
4

2 に答える 2

1

In my opinion, your approach is all wrong and is creating complexity.

Here's how I'd do it. Create two classes, one for the ball and one for somewhere to draw it. Something along these lines:

public class Ball{

    private int radius;
    private int xPosition;
    private int yPosition;
    private int color;
    private Paint paint;

    public Ball(int x, int y, int radius, int color)
    { 
        this.xPosition = x; this.yPosition = y; this.radius = radius;
        paint = new Paint(color);
    }

    void moveBall(int x, int y){
         xPosition = x; yPosition =y;        
    } 

    void onDraw(Canvas canvas){
          canvas.drawCircle(x, y, radius, paint);
    }              

}

Now somewhere to draw it.

public class Playground extends ImageView{

       public Ball ball;    

       @Override
       public void onDraw(Canvas canvas)
       {
           super.onDraw(canvas);
           if (ball != null ){
               ball.onDraw(canvas);
           }
       }

 }

In your activity, probably in onStart():

imageView.ball = new Ball(startX, startY, radius, Color.BLUE);

Move your "update" method into the ball class also and call ball.update() on a timer (or whenever you want to update it).

Replace the ImageView in your layout with the Playground class (first name that popped into my head).

Override onMeasure() in the ImageView extension to get height and width of the "playground".

That gives you the basics. I wrote this off the top of my head so please excuse typos etc

Also, review the answers given to you in your multiple previous questions on this topic and read the Android documentation on extending View classes, onDraw and onMeasure.

于 2012-11-04T07:32:06.393 に答える
-1

一度だけ描画していると思います。タイマーを追加して draw() メソッドをトリガーすることができます

于 2012-11-04T04:06:57.423 に答える