在Android中创建游戏重力?

时间:2012-11-04 03:54:30

标签: android eclipse gravity

我正在尝试在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;

    }

}

}

2 个答案:

答案 0 :(得分:1)

在我看来,你的方法是错误的并且正在创造复杂性。

我是这样做的。创建两个类,一个用于球,一个用于绘制它。这些方面的东西:

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);
    }              

}

现在可以在某处绘制它。

public class Playground extends ImageView{

       public Ball ball;    

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

 }

在您的活动中,可能在onStart()中:

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

将“update”方法也移到ball类中,并在计时器上调用ball.update()(或者每当你想要更新它时)。

使用Playground类(弹出到我脑中的名字)替换布局中的ImageView。

覆盖ImageView扩展中的onMeasure()以获取“playground”的高度和宽度。

这为您提供了基础知识。我把它写在了我的头顶,所以请原谅错别字

另外,请查看之前关于此主题的多个问题中给出的答案,并阅读有关扩展View类,onDraw和onMeasure的Android文档。

答案 1 :(得分:-1)

我认为你只画了一次,你可以添加一个计时器来触发draw()方法