Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-25-2008, 06:07 AM
Member
 
Join Date: Jul 2008
Posts: 3
r0binho0d is on a distinguished road
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

Code:
public void attack(){ at=true; udchar(3,"attack1"); sleep(300); repaint(); }
here's the sleep sub
Code:
public void sleep(long l){ try{ Thread.sleep(l); } catch(InterruptedException e){ return; } }
here's the paint sub
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...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-25-2008, 06:29 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-25-2008, 06:46 PM
Member
 
Join Date: Jul 2008
Posts: 3
r0binho0d is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-25-2008, 07:00 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-25-2008, 08:33 PM
Member
 
Join Date: Jul 2008
Posts: 3
r0binho0d is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-26-2008, 05:03 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SWT Graphics Example Java Tip SWT 0 06-28-2008 11:28 PM
How to cancel an individual timer in spite of canceling whole timer Java Tip java.util 0 04-04-2008 04:46 PM
Graphics Joe2003 Advanced Java 1 01-25-2008 08:24 PM
graphics Joe2003 Advanced Java 4 01-18-2008 09:44 PM
Graphics feniger New To Java 1 12-29-2007 06:22 PM


All times are GMT +3. The time now is 07:45 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org