Sign in to follow this  
Zeron

Java problem

Recommended Posts

I am using BlueJ as a compiler. You see I have a basic source code for my game, nothing to complicated. However, when I ad a while loop to the game to make it so when he falls he goes faster and faster, it just crashes, I have no idea what happens, I run the applet and nothing shows up and I am not able to close the applet, it just says applet started, it makes utterly no sense at all, without the loop everything works fine, but no matter where I put it, as long as I have a while loop it does the exact same thing. Please help.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zeron
However, when I ad a while loop to the game to make it so when he falls he goes faster and faster, it just crashes, I have no idea what happens, I run the applet and nothing shows up and I am not able to close the applet, it just says applet started, it makes utterly no sense at all, without the loop everything works fine, but no matter where I put it, as long as I have a while loop it does the exact same thing. Please help.


Well, don't add the while loop, so it won't crash, although I have no idea how to solve it, although you should probably fix it so it won't crash.



Hint: Post some useful details, such as source code, stack trace, core dump, ....

Share this post


Link to post
Share on other sites
Well heres the source code.

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;

public class LessonOne extends Applet implements ActionListener, KeyListener
{
private int x;
private int y;
private int incx;
private int incy;
private Timer timer;
private int color;
private int yspeed = 0;
boolean falling = false;
public void init()
{
x=200;
y=200;
incx=0;
incy=0;
timer = new Timer(20, this);
timer.start();
setBackground(Color.WHITE);
addKeyListener(this);
}
public void actionPerformed(ActionEvent ae)
{
repaint();
}
public void keyReleased(KeyEvent ke)
{
incx=0;
incy=0;
}
public void falling()
{
if(y <= 490)
{
falling = true;
yspeed = 6;
}
else
{
falling = false;
yspeed = 0;
}
}
public void keyPressed(KeyEvent ke)
{
int key = ke.getKeyCode();
if (key == KeyEvent.VK_RIGHT)
{
incx=3;

}
if (key == KeyEvent.VK_LEFT)
{
incx=-3;

}
if (key == KeyEvent.VK_UP)
{
incx=0;
}
if (key == KeyEvent.VK_DOWN)
{
incx=0;
}
if (key == KeyEvent.VK_SPACE)
{
yspeed=-15;
if(y == y+30)
{
yspeed = 15;
if(y <= 490)
{
yspeed = 0;
}
}
}
if (key == KeyEvent.VK_S)
{
incx = incx + 0;
}
if (key == KeyEvent.VK_W)
{
incx = incx + 0;
}
if (key == KeyEvent.VK_A)
{
incx = incx - 3;
}
if (key == KeyEvent.VK_D)
{
incx = incx + 6;
}
if (key == KeyEvent.VK_Q)
{
incx = incx + -6;
}
if (key == KeyEvent.VK_E)
{
incx = incx + 6;
}
if (key == KeyEvent.VK_1)
{
color = 1;
}
if (key == KeyEvent.VK_2)
{
color = 2;
}

repaint();
}
public void keyTyped(KeyEvent ke)
{
}

public void paint(Graphics g)
{
y = y + yspeed-5;
pacman(g, x, y, 50, 50, Color.BLUE);
x+= incx;
y+= incy;
if (color == 1)
{
g.setColor(Color.BLUE);
}
if (color == 2)
{
g.setColor(Color.YELLOW);
}
}
public void pacman(Graphics g, int x, int y, int width, int height, Color col)
{
falling();
System.out.println((y-yspeed+","+yspeed));
g.setColor(col);
g.fillOval(x+2*width/3,y,width/6,height/6);
g.setColor(Color.GREEN);
g.fillRect(0, 500, 1000000, 500);
g.setColor(Color.YELLOW);
g.fillRect(0, 0, 130, 15);
g.setColor(Color.BLACK);
g.drawString("Move: Arrow Keys", 10, 10);
g.setColor(Color.GREEN);
g.fillRect(0, 15, 130, 15);
g.setColor(Color.BLACK);
g.drawString("Move faster: WASD.", 10, 25);
g.setColor(Color.RED);
g.fillRect(0, 30, 130, 15);
g.setColor(Color.BLACK);
g.drawString("Move Diagnoly: Q&E", 10, 40);
}

}




The while loop I usually add it in the falling void.

Share this post


Link to post
Share on other sites
Is the while loop without an end condition? If so .. every time paint() is called you call pacman(..) where you call falling() which has the loop. You should add another thread if you want to loop something outside the main program cycle.

Share this post


Link to post
Share on other sites
Use the source code tag to make your code more readable.

The while loop you "usually" add that crashes your program? It is difficult to help you solve a problem with your code when you remove the problem. :)

What does your while loop look like? I would guess your loop is not correctly terminating - which probably causes some undesirable result.

Share this post


Link to post
Share on other sites
so your saying if you have this code it breaks?

Quote:

bool flag = false;
while(!flag){
flag = true;
}


If thats the case then theres probably something wrong with your build enviroment because that is perfectly valid code, otherwise check your stack trace, logs and try debugging, its hard to believe that a simple while loop wont work unless your programming it wrong.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this