code design help

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

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)

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.

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…

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.)
(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 :slight_smile:

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.

The ‘generic’ stuff I’m talking about looks like List 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 :slight_smile:

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.

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

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.

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?