Results 1 to 8 of 8
  1. #1
    tdenning is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Help with Intro Java class: How to make scoreboard

    I got the scoreboard created and it does change the score when the cat touches the bananas and they become invisible. However, it keeps on counting as long as it is still touching the invisible bananas. What is a simple way to make it only respond to the changed state once? Any help GREATLY appreciated! I attached the full game folder in case necessary.



    Java Code:
    class Game extends JFrame {
    
        /* Type your variable declarations here */	
    	
    	int i = 0;
    	
    
    
    	Sprite cat = new Sprite(new ImageIcon("cat.gif").getImage(), 267, 167);
        Sprite bananas1 = new Sprite(new ImageIcon("bananas.png").getImage(), 50, 242);
    	Sprite bananas2 = new Sprite(new ImageIcon("bananas.png").getImage(), 150, 242);
    	Sprite bananas3 = new Sprite(new ImageIcon("bananas.png").getImage(), 400, 242);
    	Sprite bananas4 = new Sprite(new ImageIcon("bananas.png").getImage(), 475, 242);
    	/**
    	 * Called when the program is launched.
    	 */
    	public void initalizeSprites(){
    		//type your sprite initialization code here
    	}
    
        /**
         * paint the current frame of the game
         * @param g handle to the screen
         */
        public void paintFrame(Graphics g) {
    	 /* Type your code here */ 
    	 String s1 = "SCORE  " + i;
    	g.drawString(s1,40,30);
    
    	 g.drawImage(cat.image, cat.x, cat.y, this);
    
        if (bananas1.visible) {
            bananas1.paintComponent(g);
        }
    
        if (bananas2.visible) {
            bananas2.paintComponent(g);
    	}	
    	if (bananas3.visible) {
            bananas3.paintComponent(g);
    	}	
    	if (bananas4.visible) {
            bananas4.paintComponent(g);
        }
    	 if (this.isLeftArrowPressed()) {
            cat.x = cat.x - 10;
        }
    	 if (this.isUpArrowPressed()) {
            cat.y = cat.y - 10;
        }
    	 if (this.isDownArrowPressed()) {
            cat.y = cat.y + 10;
        }
        if (this.isRightArrowPressed()) {
            cat.x = cat.x + 10;
    		}
    	 if (bananas1.intersects(cat)) {
            bananas1.visible=false;
        }
    	
    	if (!bananas1.visible) {
    		i=i+=1;
    		}
        if (bananas2.intersects(cat)) {
            bananas2.visible=false;
        }
    	if (bananas3.intersects(cat)) {
            bananas3.visible=false;
        }
    	if (bananas4.intersects(cat)) {
            bananas4.visible=false;
        }
    
    
        
    	  
        }
    Attached Files Attached Files
    Last edited by tdenning; 11-18-2013 at 12:36 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Help with Intro Java class: How to make scoreboard

    make it only respond to the changed state once?
    How is the state change event handled? If you are using a listener, there is a method to remove a listener.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    tdenning is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: Help with Intro Java class: How to make scoreboard

    We have not learned anything about listeners. I created a string called s1 which is Score + i (i declared above). I have an if statement that adds 1 to i when the bananas1.visible is false(in paintframe). I see that is just keeps adding as long as it's false though. Maybe I am not even on the right track.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Help with Intro Java class: How to make scoreboard

    I assumed that there was a listener for the state change.
    If you are not using listeners, then an if statement condition would be another way to test if the count should be incremented or not.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    tdenning is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: Help with Intro Java class: How to make scoreboard

    I have if bananas.visible = false add one...that just makes it add continously which clearly doesn't work. What is a way to write this that will increment one only once instead of always if it is false? I hope this makes sense. I am extremely frustrated and professor is slow to respond for help.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Help with Intro Java class: How to make scoreboard

    increment one only once
    Use a boolean:
    Java Code:
    boolean firstTime = true; //  define this outside the looping logic perhaps at class level
      ....
    //  at the place you want to do something only once
    if(firstTime) {
       firstTime = false;   // turn off the one time switch
       //  here do the thing once
    }
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    tdenning is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: Help with Intro Java class: How to make scoreboard

    ooooooh! Thank you..I will try that. thank you thank you :):)

  8. #8
    tdenning is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: Help with Intro Java class: How to make scoreboard

    @Norm. YOU ARE A GOD! Can you teach my class please? :) Thank you again!

Similar Threads

  1. Java won't make class file
    By amelia22 in forum New To Java
    Replies: 11
    Last Post: 08-20-2013, 02:12 PM
  2. Need Help with Intro to Programming Class
    By DesertFlower in forum New To Java
    Replies: 5
    Last Post: 10-19-2012, 12:36 AM
  3. Replies: 3
    Last Post: 03-19-2012, 06:17 PM
  4. Can't make a java .class to a .jar
    By TheDarkMagician in forum New To Java
    Replies: 4
    Last Post: 02-20-2011, 12:45 AM
  5. Hints on how to make a Java Class
    By luron31 in forum New To Java
    Replies: 11
    Last Post: 07-09-2009, 06:31 AM

Posting Permissions

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