Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: invisible box game

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation invisible box game

    i am creating a program so that a user can click on three invisible boxes and get points for clicking on them. I am having trouble getting the points to be correct.

    points are supposed to be as follows:
    * 200 points: Hitting all three boxes
    * 150 points: Hitting the small box and the medium box
    * 125 points: Hitting the small box and the large box
    * 110 points: Hitting the medium box and the large box
    * 100 points: Hitting only the small box
    * 75 points: Hitting only the medium box
    * 50 points: Hitting only the large box
    * -1 point: For each mouse click

    here is my code, what is wrong?


    smallclick=smallbox.contains(e.getX(),e.getY());
    mediumclick=mediumbox.contains(e.getX(),e.getY());
    largeclick=largebox.contains(e.getX(),e.getY());

    public void mouseExited(MouseEvent e)
    {

    if(smallclick && mediumclick && largeclick)
    {
    score=score+200;
    }

    else if(smallclick && mediumclick)
    {
    score= score + 150;
    }
    else if(smallclick && largeclick)
    {
    score= score + 125;
    }
    else if(mediumclick && largeclick)
    {
    score= score +110;
    }

    else if( smallclick)
    {
    score=score + 100;

    }
    else if(mediumclick)
    {
    score=score +75;
    }
    else if(largeclick)
    {
    score=score+50;
    }

    else if (!smallclick &&!mediumclick &&!largeclick)
    {
    score=score -1;
    }


  2. #2
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default Re: invisible box game

    I think the problem is that you define smallclick, mediumclick and largeclick outside of the method that acctualy uses them.
    Another problem is that you are using mouseExited() method and not mouseClicked()

    package Random;
     
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
     
    public class MouseActions implements MouseListener {
     
    	public void mouseClicked(MouseEvent e) {
     
    		boolean smallclick=smallbox.contains(e.getX(),e.getY());
    		boolean mediumclick=mediumbox.contains(e.getX(),e.getY());
    		boolean largeclick=largebox.contains(e.getX(),e.getY());
     
    		if(smallclick && mediumclick && largeclick)
    		{
    		score=score+200;
    		}
     
    		else if(smallclick && mediumclick)
    		{
    		score= score + 150;
    		}
    		else if(smallclick && largeclick)
    		{
    		score= score + 125;
    		}
    		else if(mediumclick && largeclick)
    		{
    		score= score +110;
    		}
     
    		else if( smallclick)
    		{
    		score=score + 100;
     
    		}
    		else if(mediumclick)
    		{
    		score=score +75;
    		}
    		else if(largeclick)
    		{
    		score=score+50;
    		}
     
    		else if (!smallclick &&!mediumclick &&!largeclick)
    		{
    		score=score -1;
    		} 
     
    	}
     
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    }

    Also somehow you will have to define score variable and objects: smalbox, mediumbox and largebox
    So you will be able to use them in this class.
    Last edited by Dalisra; September 27th, 2009 at 12:50 PM.

Similar Threads

  1. Programmer for a Java based game project
    By Takkun in forum Project Collaboration
    Replies: 4
    Last Post: June 14th, 2010, 05:47 PM
  2. Job offers to program Hobo Wars
    By MooncakeZ in forum Paid Java Projects
    Replies: 7
    Last Post: September 17th, 2009, 09:41 PM
  3. [SOLVED] Fixing of bug for Pong game
    By phoenix in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 14th, 2009, 01:19 PM
  4. Problem while programming a simple game of Car moving on a road
    By rojroj in forum Java Theory & Questions
    Replies: 3
    Last Post: April 2nd, 2009, 10:24 AM
  5. Replies: 1
    Last Post: March 28th, 2009, 07:21 AM