Results 1 to 6 of 6
  1. #1
    r0binho0d is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default Problems with Graphics and a Timer...

    Alright so what I'm trying to do is when my character attacks (after the user pushes the ctrl button) I want the attack sprite to show, along with a simple slash graphic, then after less than a second I want the graphic gone and the sprite to return to standing mode. the problem is that it doesn't return, it just stays with the graphic and the attack sprite until the user moves.

    here's the attack sub

    Java Code:
    public void attack(){
                    at=true;
                    udchar(3,"attack1");
                    
    		sleep(300);
    		repaint();
     
                    
            }
    here's the sleep sub
    Java Code:
    public void sleep(long l){
                    try{
                            Thread.sleep(l);
                    }
                    catch(InterruptedException e){
                            return;
                    }
            
            }
    here's the paint sub
    Java Code:
    public void paint(Graphics g){
                    
     
                    bufferGraphics.drawImage(background,0,0,10000,600,this);
                    bufferGraphics.drawImage(sidebar,720,0,200,600,this);
                    bufferGraphics.drawImage(chara,px,py,128,128,this);
                    bufferGraphics.drawImage(sworda,px,py,128,128,this);
                    bufferGraphics.drawImage(hi,6,507,96,96,this);
                    bufferGraphics.drawImage(li,200,507,96,96,this);
                    bufferGraphics.drawImage(mi,394,507,96,96,this);
                    if(at==true){
                            if(dir.equals("right"))
                                    bufferGraphics.drawImage(slice,px+100,py+20,128,128,this);
                            else
                                    bufferGraphics.drawImage(slice,px-100,py+20,128,128,this);
                            at=false;
     
                    }
                    g.drawImage(offscreen,0,0,this);
                    
            }
    Help would be much appreciated. I'm not sure if I need to include any other parts of my code or not. If I do just let me know and I'll post that part of the code when I get home from work. Thanks in advance.

    p.s. I'm not using swing. I was ridiculed in some other forum because I posted my "swing" question in the wrong forum and this isn't even swing...

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Add some println() to show the value of at and dir in paint() to see how they change.

    Where in the code does it draw the sprite in standing mode?
    And where does it show the attack sprite?

    Some programmers put comments in their code that would explain what the code is doing.

  3. #3
    r0binho0d is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    ha. yeah I guess I should put comments in now before I post code asking for help. However, I managed to fix it myself and it's the weirdest thing... Everything that was in paint...I copied and pasted into a new sub called makeit(). THen instead of calling repaint() I called makeit()....really weird. New problem though, I have a keylistener and it works fine. But when I'm jumping, I can't move left or right until he lands. I think multithreading is what I want but I don't really understand how to instill that in my game since it's not a simple little function that prints a line. Can anyone kinda give me a quick overview of multithreading for my project? I can provide the full code if needed. I'll keep reading tutorials and try to figure it out.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    A tutorial is more than I can type in here.
    Having multiple threads allows different execution paths in your program to execute at the same time(sort of). Writing code to do that and not have the threads step on each others toes can be tricky.
    What two or more execution paths do you want to be happening at the same time?

  5. #5
    r0binho0d is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    Yeah I figured a tutorial was more than anyone could put there and that's all good. I need to be able to read keypresses while it's already in a loop for the first keypress.

    for instance.

    i press space bar to jump and he goes straight up and down. I try pressing left or right while he's in the air and it won't move until he lands.

    I don't know if i really need a tutorial because I understand the way multithreading works for a small program that i find examples of on the internet, where the main function prints a line...but I don't understand what the main function of a game is since all it does is sit there and wait for keypresses.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    i press space bar to jump and he goes straight up and down. I try pressing left or right while he's in the air and it won't move until he lands.
    Not sure I follow. Is this the way you want it to work?
    Or did you want to change it to work another way?

    It sounds like your listener for the keypress calls code to do the moving of the ??? rather than start a thread that calls the code to do the moving. You should minimize code paths called from listeners to allow the GUI interface to react to further GUI stuff.
    In the listener, start a thread and have the thread call the code to do the moving stuff. If future key presses are to change the motion of the ???? while the thread is moving it, then your code will have to be sensitive to that.

Similar Threads

  1. SWT Graphics Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:28 PM
  2. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  3. Graphics
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-25-2008, 06:24 PM
  4. graphics
    By Joe2003 in forum Advanced Java
    Replies: 4
    Last Post: 01-18-2008, 07:44 PM
  5. Graphics
    By feniger in forum New To Java
    Replies: 1
    Last Post: 12-29-2007, 04:22 PM

Posting Permissions

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