2

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;

    }

}

}
Jordan
  • 249
  • 3
  • 8

2 Answers2

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.

Simon
  • 14,407
  • 8
  • 46
  • 61
  • Thanks! I have a few problems, though: I created an ImageView in the Ball class, and when I put imageView.ball... it says "ball cannot be resolved or is not a field" Also, how do I replace the Imageview with the Playground class? I really appreciate your help. – Jordan Nov 04 '12 at 15:24
  • Jordan. I thought you'd been away for a while reading some tutorials and learning object oriented programming. It seems not ;) Why did you put an ImageView in the ball class? My example did not show this and the ball class should exist without any knowledge of the ImageView. To use your custom ImageView, Playground, replace ImageView with com.package.whatever.Playground in your layout XML. But please, search here or Google for how to do that. – Simon Nov 04 '12 at 15:28
  • Haha sorry, but it's hard for me to understand what to do when people mix code with actual words :) so I got a little confused. I'll try that right now, thanks – Jordan Nov 04 '12 at 20:32
  • Good luck. And see how much cleaner the code is? You have a ball class from which you can create as many balls as you like. Each one takes care of itself and in your "Playground", you can ask each ball to draw itself. In the future, you can change that to a bird class, or a triangle class or a flying toaster class and create whatever you want. – Simon Nov 04 '12 at 20:35
  • I seem to like Playground class the best ;) and so far, I've been looking up how to change the ImageView in xml, and everything is about the ImageView button? Is that what I want to use, or am I mixing that up with something else? – Jordan Nov 04 '12 at 20:44
  • Well, I figured out how to set a custom view, but now it just force closes. I used [this](http://www.youtube.com/watch?v=vOY2AxbnRyc) video – Jordan Nov 04 '12 at 21:06
  • OK, new question please. And ALWAYS post the logcat and *relevant* code if you are asking about a crash. – Simon Nov 04 '12 at 21:38
  • OK. I put the new code & LogCat in the post. Thanks for all your help – Jordan Nov 04 '12 at 23:03
  • No, I meant create a new question since your edits now do not match with the original question and the answer. Please format the logcat when you paste it in. – Simon Nov 05 '12 at 10:32
-1

I think you're drawing just once, you may add a timer to trigger the draw() method

nico
  • 110
  • 2
  • 8
  • Thanks, how do you add a timer? – Jordan Nov 04 '12 at 04:23
  • Here's some info: http://stackoverflow.com/questions/1877417/how-to-set-a-timer-in-android – nico Nov 04 '12 at 04:25
  • If you look at the code, the ball will always be drawn in the same position. Adding a timer will do nothing except to cause the ball to be drawn in the same place multiple times. Whilst the OP will need a timer at some point, this does not address the question. – Simon Nov 04 '12 at 07:15