Results 1 to 17 of 17
  1. #1
    ninjaturtlez is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default 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):
    Java 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

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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!

  3. #3
    ninjaturtlez is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default 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;	
    	}
    	
    
    
    
    
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default 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.

  5. #5
    ninjaturtlez is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default 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.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Java Applet (game) Problem

    Quote Originally Posted by ninjaturtlez View Post
    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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    ninjaturtlez is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default 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);
    	}
    	
    
    }
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Applet (game) Problem

    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.

  9. #9
    ninjaturtlez is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default 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

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Applet (game) Problem

    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.

  11. #11
    ninjaturtlez is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default 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. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Applet (game) Problem

    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.

  13. #13
    ninjaturtlez is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: Java Applet (game) Problem

    Thank you i looked through my code again an noticed that the variable was being redeclared every time.

Similar Threads

  1. Java Game Launching Problem
    By PaulMoretti in forum Java Applets
    Replies: 8
    Last Post: 08-18-2011, 09:17 PM
  2. My java game problem!? Help!!!
    By Jcbconway in forum AWT / Swing
    Replies: 32
    Last Post: 09-26-2010, 10:33 PM
  3. Java Applet Help - Making "Snake" Game
    By Alphimeda in forum Java Applets
    Replies: 15
    Last Post: 04-04-2010, 06:39 PM
  4. Need smart solution on java game lag problem..
    By Addez in forum New To Java
    Replies: 0
    Last Post: 11-13-2009, 01:48 PM
  5. Help with game applet in java
    By barney in forum Java Applets
    Replies: 3
    Last Post: 02-13-2008, 06:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •