I am starting to do some java programming for my android app development course, and I am starting with creating a pong game to start to learn the basics. I have written all this code my self only using the documentation I found online, so it may be sloppy, but I just started
Anyways, on to my issue. When I run this program, I instantly crash. at first I though it was because I wasn't specifying an fps, but that wasn't the case, because even when I got it to run only once every second it was still crashing. I also got it so my ball didn't move and all my game loop is doing is adding to a variable and it still crashed. Seeing as how eclipse is not very useful in telling me where my code is having troubles in, and I can't think of anything else, I ask you!
Here is the java code.
package com.example.pong;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
int player_X, player_Y, touch_Y, ball_X, ball_Y, ballSpeedX, ballSpeedY,fps;
ImageView playerPaddle, computerPaddle, ball;
RelativeLayout rlMain;
TextView debug;
View playingScreen;
Boolean isRunning;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callVariables();
playerPaddle.setX(player_X);
playerPaddle.setY(player_Y);
runGame();
}
private void runGame() {
// TODO Auto-generated method stub
new Thread(new Runnable() {
public void run() {
while (isRunning) {
onTouchEvent();
//moveBall();
try {
Thread.sleep(fps);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ballSpeedY += 1;
debug.setText(Integer.toString(ballSpeedY));
}
}
}).start();
}
public void moveBall() {
if (ball_X < 100) {
ballSpeedX *= -1;
} else if (ball_X > 1000) {
ballSpeedX *= -1;
}
if (ball_Y < 50) {
ballSpeedY *= -1;
} else if (ball_Y > 600) {
ballSpeedY *= -1;
}
ball_X += ballSpeedX;
ball_Y += ballSpeedY;
debug.setText(Integer.toString(ballSpeedY));
//ball.setX(ball_X);
//ball.setY(ball_Y);
}
private void onTouchEvent() {
// TODO Auto-generated method stub
playingScreen.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
touch_Y = (int) event.getY();
movePaddle();
return true;
}
});
}
private void movePaddle() {
if ((player_Y + 75) > (touch_Y + 25)
|| (player_Y + 75) < (touch_Y - 25)) {
if ((player_Y + 75) < touch_Y) {
player_Y += 10;
} else if ((player_Y + 75) > touch_Y) {
player_Y -= 10;
}
}
playerPaddle.setX(player_X);
playerPaddle.setY(player_Y);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void callVariables() {
// TODO Auto-generated method stub
player_X = 50;
player_Y = 350;
ball_X = 600;
ball_Y = 350;
playerPaddle = (ImageView) findViewById(R.id.ivPlayerPaddle);
rlMain = (RelativeLayout) findViewById(R.id.mainLayout);
debug = (TextView) findViewById(R.id.debug);
playingScreen = findViewById(R.id.mainLayout);
ball = (ImageView) findViewById(R.id.ivBall);
computerPaddle = (ImageView) findViewById(R.id.ivComputerPaddle);
isRunning = true;
ballSpeedX = 1;
ballSpeedY = 1;
fps = 1000;
}
}

New Topic/Question
Reply


MultiQuote








|