Java Applet (game) Problem
Hi,
I am fairly new to java and am attempting to make my first game, which is an applet. This game includes squares/rectangles moving accross the screen. I attempted to make a seperate class to get values for where the squares should be drawn, and then a method (in the second class) that whenever called adds a certain amount to the x and y coordinates of the square.
This is the metod that adds to the x and y (the class extends Rectangle):
Code:
public void move(){
super.x += xSpeed;
super.y += ySpeed;
}
I think the problem is that the game get "stuck in this class" but whenever i try to send it back to the main class it gives me errors.
Any ideas on how to fix it?
-thanks
Re: Java Applet (game) Problem
What errors does it give you?
If you want help, you'll have to provide an SSCCE that demonstrates the problem. Otherwise we're just guessing. For example, what are the values of x and y? What are the values of xSpeed and ySpeed? Don't just answer those questions, but give us a small piece of code that we can copy and paste to compile and run ourselves. Make it easier for people to help you.
Re: Java Applet (game) Problem
This is a simplified version, i am trying to get the red square to move across the screen by calling the move method in the square class.
Code:
import java.applet.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class Test extends Applet implements MouseMotionListener {
Color red = Color.red;
Color cyan = Color.CYAN;
int x = 100;
int y = 100;
int w = 10;
int midx;
int midy;
int dimX = 600;
int dimY = 600;
public int score = 0;
public void init(){
setBackground(cyan);
setSize(dimX,dimY);
addMouseMotionListener(this);
}
public void paint (Graphics g){
midx =x- w/2 ;
midy = y-w/2;
g.fillRect(midx,midy,w,w);
Squares testSq = new Squares();
testSq.move();
g.setColor(Color.BLACK);
g.setColor(red);
g.fillRect(testSq.x,testSq.y,testSq.width,testSq.height);
}
public void update(Graphics g){
paint(g);
}
@Override
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
public void method (){
repaint();
}
}
class Squares extends Rectangle{
int xSpeed = 50;
int ySpeed = 44;
public Squares() {
super.x = 0;
super.y = 0;
super.width = 10;
super.height = 10;
}
public void move(){
super.x += xSpeed;
super.y += ySpeed;
}
}
Re: Java Applet (game) Problem
Try debugging your code by adding printlns to show the values of the variables that you are changing to see when and if their values are changed.
Re: Java Applet (game) Problem
I tried that and I am still not able to fix the problem: that its only adding the xSpeed variable to super.x once and not every time the method is called.
Re: Java Applet (game) Problem
Quote:
Originally Posted by
ninjaturtlez
I tried that and I am still not able to fix the problem: that its only adding the xSpeed variable to super.x once and not every time the method is called.
Like I said, without an SSCCE, we can't really help you.
Re: Java Applet (game) Problem
Code:
Here is a test version of it (normaly its double bufferedimport java.applet.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class Test extends Applet implements MouseMotionListener {
Color red = Color.red;
Color cyan = Color.CYAN;
int x123 = 0;
int x = 100;
int y = 100;
int w = 10;
int midx;
int midy;
int dimX = 600;
int dimY = 600;
public int score = 0;
public void init(){
setBackground(cyan);
setSize(dimX,dimY);
addMouseMotionListener(this);
}
public void paint (Graphics g){
midx =x- w/2 ;
midy = y-w/2;
g.fillRect(midx,midy,w,w);
Squares testSq = new Squares();
if (x123 == 0){
testSq.move();
}
g.setColor(Color.BLACK);
System.out.println(testSq.x);
g.setColor(red);
g.fillRect(testSq.x,testSq.y,testSq.width,testSq.height);
}
public void update(Graphics g){
paint(g);
}
@Override
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
public void method (){
repaint();
}
public static void sizer(){
Squares testSq = new Squares();
testSq.move();
}
class Squares extends Rectangle{
int xSpeed = 5;
int ySpeed = 0;
public Squares() {
super.x = 30;
super.y = 0;
super.width = 10;
super.height = 10;
}
public void move(){
super.x = super.x + xSpeed;
super.y += ySpeed;
System.out.println(super.x);
}
}
}
Re: Java Applet (game) Problem
Quote:
its only adding the xSpeed variable to super.x once and not every time the method is called.
What did you see in the printout?
How do you know that it is not adding the value every time the method is called? Where did you put the println to show you that it is not doing the add every time?
One println should be in the move method.
Re: Java Applet (game) Problem
Whenever i try to post it it says a Moderator needs to approve it. D:
Norm: I put one in the move method and one in the paint method it prints out the same number (which is my super.x +xSpeed). it doesnt continue to add
Re: Java Applet (game) Problem
Quote:
Whenever i try to post it
What is the "it" you are talking about?
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
Re: Java Applet (game) Problem
It is the SSCCE and im using eclipse to run it and am trying to get you the test version.
import java.applet.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class Test extends Applet implements MouseMotionListener {
Color red = Color.red;
Color cyan = Color.CYAN;
int x = 100;
int y = 100;
int w = 10;
int midx;
int midy;
int dimX = 600;
int dimY = 600;
public int score = 0;
public void init(){
setBackground(cyan);
setSize(dimX,dimY);
addMouseMotionListener(this);
}
public void paint (Graphics g){
midx =x- w/2 ;
midy = y-w/2;
g.fillRect(midx,midy,w,w);
Squares testSq = new Squares();
testSq.move();
g.setColor(Color.BLACK);
System.out.println(testSq.x);
g.setColor(red);
g.fillRect(testSq.x,testSq.y,testSq.width,testSq.h eight);
}
public void update(Graphics g){
paint(g);
}
@Override
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
public void method (){
repaint();
}
class Squares extends Rectangle{
int xSpeed = 5;
int ySpeed = 0;
public Squares() {
super.x = 30;
super.y = 0;
super.width = 10;
super.height = 10;
}
public void move(){
super.x = super.x + xSpeed;
super.y += ySpeed;
System.out.println(super.x);
}
}
}
Re: Java Applet (game) Problem
Quote:
Norm: I put one in the move method and one in the paint method it prints out the same number (which is my super.x +xSpeed). it doesnt continue to add
What could the code be doing to always print out the same value each time? That is the question you should be asking yourself!!!
Is your code creating a new object every time so you are seeing the ONLY call to that instance of the object?
The next call to the move method is in a new object which has the default values.
Add a println to the constructor for the class that move() is in and see how many times it prints.
Re: Java Applet (game) Problem
Thank you i looked through my code again an noticed that the variable was being redeclared every time.