Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  code design help  (Read 1269 times)
0 Members and 1 Guest are viewing this topic.
Offline pi3rc3

Junior Newbie





« Posted 2006-02-21 05:52:01 »

hello. im trying to make it so that in my game when a player clicks a sprite the sprite goes into a state of 'activated' and then moves mouse away and when he releases the left mouse button it sends the sprite flying off in that direction. i set it up so when the mouse sprite is colliding with the sprite and clicked the sprite is set into 'activated' and then called a method on that sprite.

//inside main class
//when mouse sprite collides with sprite

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public void collided(Sprite s1,Sprite s2){
     
      M s3 = (M) s2;
     
      double sX = s1.getX();
      double sY = s1.getX();
      double s2X = s2.getX();
      double s2Y = s2.getX();
      double s3X = s2X - 17.5;
      double s3Y = s2Y - 17.5;
      double s4X = sX - 5.5;
      double s4Y = sY - 5.5;
     
      if( Math.pow(s4X - s3X,2)+Math.pow(s4Y - s3Y,2) < Math.pow(11 + 35,2)) {
         ZenGame.clickable = true;
         ZenGame.clickIt(s3);
         }
      if(ZenGame.leftClick){
         System.out.println("activated!");
         s3.activated = true;
         s3.throwThis(s2X,s2Y);
      }
   }    


//inside the sprite class (thing i want to click)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
public void throwThis(double x, double y){
      while(activated){
         if(!ZenGame.leftClick){
            double mX = ZenGame.mX;
            double mY = ZenGame.mY;
           
            if(x < mX){
               speedX = 1;
               setSpeed(speedX,speedY);
               activated = false;
            }
            if(x > mX){
               speedX = -1;
               setSpeed(speedX,speedY);
               activated = false;
            }
            if(y < mY){
               speedY = 1;
               setSpeed(speedX,speedY);
               activated = false;
            }
            if(y > mY){
               speedY = -1;
               setSpeed(speedX,speedY);
               activated = false;
            }
         }
      }
   }


everything seems to work untill i get to the if(!ZenGame.leftClick), inwhich does not run. i think the problem is that when the user moves the mouse away he is no longer colliding with the sprite so the if statement never gets teh chance to execute. what actually happens when i run teh game is once i click the sprite my mouse sprite freezes over the sprite and nothing happens.

maybe i should be going about this in a tottally diffrent way? any ideas?

thanks.
Offline pi3rc3

Junior Newbie





« Reply #1 - Posted 2006-02-21 06:57:49 »

anyone see whats wrong with my code? anyoen want to give me some idea of how they would have went about this?

i dont mean to seem rushy here but im trying to stay within a schedule and i cant work on anything untill i get this working...
Offline tusaki

Junior Member


Medals: 1


In a mad world only the mad are sane.


« Reply #2 - Posted 2006-02-21 11:46:01 »

Well, I have a few pointers...

first a few general issues:

now, as for your problem:
  • in your class you only allows single-speed, 8-way directions. (combination of -1 and 1 for x and y) I doubt this is what you want to achieve.
  • you don't need to follow the mousepointer (as you are doing in the throwThis method), you are basically interested only in the current position of the sprite and the position of the mouse when the left mouse button is released. This, combined with the length of that vector and the time the mousebutton was held down will give you all the variables to calculate a proper motionVector. (i.e. motionVector = mouseVector / timeMouseButtonWasHeldDown. mouseVector is the vector from coordinates of the sprite to the coordinates of the mouse.)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #3 - Posted 2006-02-21 18:03:51 »

1  
(M ? what is M supposed to be for kind of class. ZenGame?)


M is probably a generic type, in which case one-letter symbols are frequently used. If not, of course, it's pretty bad Smiley
Offline pi3rc3

Junior Newbie





« Reply #4 - Posted 2006-02-21 21:12:43 »

the pieces (sprites)  in my game are m1,m2,m3,m4 so yeah m is just a generic name for now, soon it will become a interface.

i know that i dont have it set to goto where the mouse was yet but that doesnt really matter yet cause i cant even get into the if statement where i check if left mouse was released. what am i doing wrong to make it so  if(!ZenGame.leftMouse) doesnt run even when the leftMouse = false?

oh and if i change that to if(ZenGame.leftMouse) then everything runs fine.
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #5 - Posted 2006-02-22 01:53:35 »

The 'generic' stuff I'm talking about looks like List<T> or similar, and allows declaration of variables of type T without knowing exactly which class T actually is. It's something smart which means we won't have to make so many class casts. Just fix the class name Smiley
Offline pi3rc3

Junior Newbie





« Reply #6 - Posted 2006-02-22 02:01:08 »

aah yes yes, java generics, gotcha. any insight on my problem though? this is the movement in my game so i cant do anything else till i fix this, it is really becoming quiet the bother.
Offline pi3rc3

Junior Newbie





« Reply #7 - Posted 2006-02-22 04:27:56 »

so noone knows the problem here? perhaps i should take this topic to a more advanced section?
Offline tusaki

Junior Member


Medals: 1


In a mad world only the mad are sane.


« Reply #8 - Posted 2006-02-22 10:49:13 »

Advanced? That is not really the problem, the problam is that you can't just copy/paste a small part of your program and say "fix plz ktnx!".

As far as I can tell (and I only have the information you posted here) you are better of handling the problem differently. Don't use sprite collision for this one. If you are using java2d, check with a mouselistener wheter a sprite has been clicked upon. When a click occurs, set a variable to the currentTime (clickTime). Then, on the instance of your main class (don't use static variables, like your ZenGame.leftClick) set a variable like 'spriteClickedUpon' with that sprite. Now, when you get a mouseReleased event, you have the 2 points you need, together with the time the mouse was held down, to calculate the vector. I feel that this is a better way, but there could be others. If I missed anything or presumed something (for example, that you use java2d, not opengl), it is most likely because you didn't tell us yet.
Offline t_larkworthy

Senior Member


Medals: 1
Projects: 1


Google App Engine Rocks!


« Reply #9 - Posted 2006-02-22 14:06:22 »

I think the reason why your game is freezing is becuase when the activated state is set to true, you call the method throwThis, which then just loops while activated is true. As such it is busy waiting.
Probably the thread that is now caught up in busy waiting is your event dispatch thread, or something similar. We can't actually tell from what you have posted the exact nature of your problems. Is you collision code called downstream from a mouseMoved event perhaps?

Runesketch: an Online CCG built on Google App Engine where players draw their cards and trade. Fight, draw or trade yourself to success.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (25 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!