且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

创建游戏重力的Andr​​oid?

更新时间:2023-02-02 19:17:14

在我看来,你的做法是完全错误的,并正在创造的复杂性。

下面就是我会做。创建两个班,一个球,一个是地方画它。沿着这些路线的内容:

 公共类球{    私人诠释半径;
    私人诠释xPosition位置;
    私人诠释yPosition;
    私人诠释色彩;
    私人涂料粉刷;    公共球(INT X,INT Y,INT半径,诠释颜色)
    {
        this.xPosition = X; this.yPosition = Y; this.radius =半径;
        油漆=新的油漆(彩色);
    }    无效moveBall(INT X,int y)对{
         xPosition位置= X; yPosition = Y;
    }    无效的onDraw(帆布油画){
          canvas.drawCircle(X,Y,半径,油漆);
    }}

现在的地方画了。

 公共类游乐场扩展了ImageView的{       公共球球;       @覆盖
       公共无效的onDraw(帆布油画)
       {
           super.onDraw(画布);
           如果(球!= NULL){
               ball.onDraw(画布);
           }
       } }

在你的活动,可能是在调用onStart():

  imageView.ball =新球(startx的,startY,半径Color.BLUE);

也将你的更新的方法进了球阶级和一个计时器调用ball.update()(或当你想更新)。

在操场类(即突然出现在我的脑海的名字),你的布局替换ImageView的。

覆盖onMeasure()在ImageView的扩展获得操场的高度和宽度。

这是给你的基本知识。我写了这关我的头顶部,请原谅错别字等。

此外,检查为您在多个previous问题,关于这个话题的答案和扩展视图类,和的onDraw阅读onMeasure的Andr​​oid文档。

I am trying to create in game gravity in Android. I created an update method, a display, and gravity. Right now the app does not force close, but the ball just doesn't move. Do I need to use a canvas for the .getHieght() and .getWidth() methods?

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;

    }

}

}

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.