创建android弹跳球程序时出错

时间:2013-12-12 06:44:39

标签: android eclipse

你能帮助解决这段代码中可能出现的错误吗?我需要为我的项目创建弹跳球应用程序。我需要纠正所有的错误。谢谢你的帮助。

package com.awai.bouncingballdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.SurfaceView;
public class GraphThing extends SurfaceView implements
SurfaceHolder.Callback, OnTouchListener { //error is here

protected TutorialThread _thread;
protected int _x=100;
protected int _y=100;

public GraphThing(Context context) { super(context); getHolder().addCallback(this);//error
_thread = new TutorialThread(getHolder(), this); //error
}

@Override
protected void onDraw(Canvas canvas) { canvas.drawColor(Color.WHITE); Paint paint = new Paint(); paint.setColor(Color.RED); paint.setStyle(Paint.Style.FILL); paint.setAntiAlias(true); //error
canvas.drawCircle(_x, _y, 80, paint); //error
}

public boolean onTouch(View view, MotionEvent event) { //error
return true;
}

@Override
public boolean onTouchEvent(MotionEvent event) { //error
_x = (int) event.getX();
_y = (int) event.getY();
return true;
}

public void surfaceChanged(SurfaceHolder holder, int format, int //error
width, int height) {
}

public void surfaceCreated(SurfaceHolder holder) { //error
_thread.setRunning(true);
_thread.start();
}

这里是该应用程序的另一个代码

public void surfaceDestroyed(SurfaceHolder holder) { //error
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join(); retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}

class TutorialThread extends Thread {
private SurfaceHolder _surfaceHolder; //error
private GraphThing _panel;
private boolean _run = false;

public TutorialThread(SurfaceHolder surfaceHolder, GraphThing panel) { //error
_surfaceHolder = surfaceHolder; //error
_panel = panel;
}

public void setRunning(boolean run) {
_run = run;
}

@Override
public void run() { Canvas c;  while (_run) { //error
c = null; try {
c = _surfaceHolder.lockCanvas(null); //error
synchronized (_surfaceHolder) { //error
_panel.onDraw(c);
}
} finally {

if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c); //error
}
}
}
}
}

我的代码的最后一部分......

public class OpenGLGraphics extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);  GraphThing gt = new GraphThing(this); setContentView(gt); //error
}
}

2 个答案:

答案 0 :(得分:0)

Hi kindly find the attached source code ..
Issue was u hv declared two public class in one activity only..

Code.

答案 1 :(得分:0)

我已在Processing中编写此代码,您可以在android .. visualize here

中创建它
    /*
a bouncing ball
*/

float ballX = 100;
float ballY = 0;
float h = 50;

//create a variable for speed
float speedY = 2;

void setup() {
  size(400,400);
  smooth();
  noStroke();

  // change the mode we draw circles so they are
  // aligned in the top left
  ellipseMode(CORNER);

}

void draw() {
  //clear the background and set the fill colour
  background(0);
  fill(255);

  //draw the circle in it's current position
  ellipse(ballX, ballY, h,h);

  //add a little gravity to the speed
  speedY = speedY + 0.5; 

  //add speed to the ball's
  ballY = ballY + speedY;


  if (ballY > height - h) {
    // set the position to be on the floor
    ballY = height - h;
    // and make the y speed 90% of what it was,
    // but in the opposite direction
    speedY = speedY * -0.9;

    //switch the direction
    //speedY = speedY;
  }
  else if (ballY <= 0) {
    // if the ball hits the top,
    // make it bounce off
    speedY = -speedY;
  }


}