Results 1 to 6 of 6

Thread: Small game

  1. #1
    ivlatt is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default Small game

    Hi,

    I'm pretty new to Java, I have studied a couple of modules on it at University (even though I'm studyin Networking :confused:) and I like what I see, so plan to continue expanding my knowledge on it :cool:.

    I am developping a small bat and ball game which initially, I thought would be relatively straight forward... How wrong could I be?

    I am having problems repainting the bat.

    Initially my ball moves around the screen and my bat is painted. I have attached a mouselistener to my main applet which gets the x co-ordinate that the mouse is at, which is then passed to a constructor inside my bat class.

    Although, for some reason my bat remains stationary.

    I have added some S.O.P testing and I have found where the issue is, although I can't find a way round it. Any help will be much appreciated :cool:.

    Java Code:
    import java.awt.*;
    
    public class Bat
    {
        public int x;
        public void setx (int x)
        {
            this.x = x;
            System.out.println("X1 = "+this.x);
        }
        public void paint (Graphics h)
        {
            System.out.println("X2 = "+x);
            h.setColor(Color.red);
            h.fillRect(x, 490, 50, 10);
            System.out.println("X3 = "+x);
            System.out.println("Painted");
        }
    
    }
    S.O.P produces this...
    X1 = 224
    X2 = 0
    Painted
    Why is the x value being reset to 0?:confused:

    Thanks in advance
    Last edited by ivlatt; 04-12-2011 at 01:38 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Weird. Are you sure you're calling paint() after you call setx()?

    Without seeing an SSCCE, we're just going to be guessing.
    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
    ivlatt is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Thank you for your response

    Yeah I'm fairly sure because the "X2 = " and "Painted" is produced everytime the mouse is moved

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Quote Originally Posted by ivlatt View Post
    Thank you for your response

    Yeah I'm fairly sure because the "X2 = " and "Painted" is produced everytime the mouse is moved
    Okay. Like I said, without an SSCCE, we're just guessing.

    That doesn't mean paste your whole program here- it would be best if you could simplify things as much as possible, take out anything that doesn't directly relate to the problem. One class containing one mouseListener and one paintComponent() method, added to a JFrame, should be enough.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Wait.. is "X1= " printed every time the mouse is moved?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    ivlatt is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Yeah all 3 of the SOP comments were

    I finally got it working somehow, even though I'm pretty sure it's the same code just rejigged around.

    Too bad I have no hair left now

    Thanks alot guys, I will be sticking around here so look forward to speaking to you in future :)

Similar Threads

  1. a small advice
    By java_newby in forum New To Java
    Replies: 10
    Last Post: 12-04-2010, 09:37 PM
  2. small windows
    By ghostofdarling in forum Java Applets
    Replies: 1
    Last Post: 04-17-2010, 08:55 AM
  3. small problem
    By rawan in forum Java Applets
    Replies: 2
    Last Post: 04-07-2010, 03:32 AM
  4. small question about 'this'
    By GPB in forum New To Java
    Replies: 3
    Last Post: 02-28-2010, 05:46 PM
  5. A small doubt
    By ranganathan in forum New To Java
    Replies: 5
    Last Post: 02-19-2010, 06:18 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
  •