创建游戏重力的Andr​​oid? [英] Creating game gravity in Android?

查看:188
本文介绍了创建游戏重力的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建中的Andr​​oid游戏重力。我创建了一个更新的方法,显示器,和重力。现在应用程序不强制关闭,但球就是不动。我需要用帆布为.getHieght()和.getWidth()方法?

 公共类MainActivity延伸活动{
私人INT C = Color.YELLOW;
私人帆布克;
@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
画();
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
。getMenuInflater()膨胀(R.menu.activity_main,菜单);
返回true;
}
//绘制球
公共无效平局()
{
显示显示= getWindowManager()getDefaultDisplay()。INT宽度= display.getHeight();
INT高度= display.getWidth();位图的位图= Bitmap.createBitmap(宽度,高度,Config.RGB_565);G =新的Canvas(位图);
g.drawColor(C);
涂料粉刷=新的油漆();
paint.setColor(Color.BLACK);
g.drawCircle(50,10,25,油漆); //把你的价值观
更新();//为了显示这个形象,我们需要创建一个新的ImageView,我们可以显示。
ImageView的ImageView的=新ImageView的(本);//设置此的ImageView位图到我们的
imageView.setImageBitmap(位图);//创建一个简单的布局,并添加ImageView的给它。
RelativeLayout的布局=新的RelativeLayout(本);
RelativeLayout.LayoutParams PARAMS =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(ImageView的,则params);
layout.setBackgroundColor(Color.BLACK);//在活动显示布局。
的setContentView(布局);
}
私人无效更新(){
//墙的碰撞;
    如果(X + DX> sp.getWidth() - 半径 - 1){
        X = sp.getWidth() - 半径 - 1;
        //弹开右壁;
        DX = -dx;
        //弹开左壁;
    }否则如果(X + DX℃,+半径){
        X = 0 +半径;
        DX = -dx;
    }其他{
        X + = DX;
    }    // 摩擦;
    如果(γ== sp.getHeight() - 半径 - 1){
        DX * = xFriction;
        如果(Math.abs(DX)LT; 0.8){
            DX = 0;
        }
    }
    //严重性;
    如果(γ> sp.getHeight() - 半径 - 1){
        Y = sp.getHeight() - 半径 - 1;
        DY * = energyloss;
        DY = -dy;
    }其他{
        //速度公式;
        DY + =重力* DT;
        //位置公式;
        Y + = DY * DT + 0.5×重力* DT * DT;    }

}

 }


解决方案

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

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

 公共类球{    私人诠释半径;
    私人诠释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.

这篇关于创建游戏重力的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆