Results 1 to 17 of 17
Thread: Java Applet (game) Problem
- 12-13-2011, 03:14 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
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):
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.Java Code:public void move(){ super.x += xSpeed; super.y += ySpeed; }
Any ideas on how to fix it?
-thanks
- 12-13-2011, 03:26 PM #2
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.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-13-2011, 03:40 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
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.
Java 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; } }
- 12-13-2011, 04:25 PM #4
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.
- 12-13-2011, 05:22 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
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.
- 12-13-2011, 05:26 PM #6
Re: Java Applet (game) Problem
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-13-2011, 05:27 PM #7
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
Re: Java Applet (game) Problem
Java 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); } } }
- 12-13-2011, 05:27 PM #8
Re: Java Applet (game) Problem
What did you see in the printout?its only adding the xSpeed variable to super.x once and not every time the method is called.
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.
- 12-13-2011, 05:28 PM #9
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
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
- 12-13-2011, 05:30 PM #10
Re: Java Applet (game) Problem
What is the "it" you are talking about?Whenever i try to post it
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.
- 12-13-2011, 05:33 PM #11
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
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);
}
}
}
- 12-13-2011, 05:53 PM #12
Re: Java Applet (game) Problem
What could the code be doing to always print out the same value each time? That is the question you should be asking yourself!!!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
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.
- 12-14-2011, 09:56 PM #13
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Java Game Launching Problem
By PaulMoretti in forum Java AppletsReplies: 8Last Post: 08-18-2011, 08:17 PM -
My java game problem!? Help!!!
By Jcbconway in forum AWT / SwingReplies: 32Last Post: 09-26-2010, 09:33 PM -
Java Applet Help - Making "Snake" Game
By Alphimeda in forum Java AppletsReplies: 15Last Post: 04-04-2010, 05:39 PM -
Need smart solution on java game lag problem..
By Addez in forum New To JavaReplies: 0Last Post: 11-13-2009, 12:48 PM -
Help with game applet in java
By barney in forum Java AppletsReplies: 3Last Post: 02-13-2008, 05:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks