[solved] Selecting a boolean multiple times.

Hello all,

This is my first post on the forum. I’m coming here for learning more Java, cause I’m kinda new to this. Currently I’m making myself a towerdefence game. It all worked out very good so far. Until I needed to make the upgrade/sell system of towers by pressing on them. At this moment I got all the towers stationed in the class Block. So my question is really much how can I select only 1 tower if there are 10 (as example) on the map and only upgrading/ selling that one. Anyone got a good tutorial/ explenation for letting this work, that would be great

[edit]
Ok, I solved some things. only the problem is, when i remove the // by the store (//store = true;) then the store will always be true, without my script works good only at 1 point it doesn’t work good, a big point: you can press evrywhere on the map and the latest “UpgradeScreen” shows up.

[spoiler]

				for(int y = 0; y<Screen.room.block.length;y++) {
				for(int x = 0; x<Screen.room.block[0].length;x++) {
					if(Screen.room.block[y][x].contains(Screen.mse)){
						store = false;
						if(Screen.room.block[y][x].airID == Value.airTowerLaser1) {UpgradeScreen = 1;}
						if(Screen.room.block[y][x].airID == Value.airTowerLaser2) {UpgradeScreen = 2;}
						if(Screen.room.block[y][x].airID == Value.airTowerLaser3) {UpgradeScreen = 3;}
					} else {
						//store = true; It will never turn on again. Disabled for testing
					}
				}
			}
	}

[/spoiler]
-Roseslayer, my English can be really bad.
Thanks.

The way I’d set it up, which is probably way over-complicated, would be to have a 2D array that stores every tile on the map and then have the towers set to a specific tile when they are built, for example [3][4], and then when you upgrade the tower you’d just tell the program to upgrade the tower at [3][4]. Actually, that doesn’t seem to complicated now that I’ve typed it…

Thx for your post and I worked it out in the new code. it goes now with Screen.room.block[y][ x]*(as example 3 and 4). Only 1 problem either when i remove the // by the store (//store = true;) then the store will always be true, without my script works good only at 1 point it doesn’t work good, a big point: you can press evrywhere on the map and the latest “UpgradeScreen” shows up.

[EDIT]
I changed the code, I debugged few things with System.out.println(""); and it’s running very good! only this part isn’t running very good:

}else{
           UpgradeScreen = 0;
           Screen.room.block[y][x].rangeVisible = false;
           if(store == false) {store = true;}
}

It’s making my other code:

		if(mouseButton == 3) {
			for(int y = 0; y<Screen.room.block.length;y++) {
				for(int x = 0; x<Screen.room.block[0].length;x++) {
					if(!Screen.room.block[y][x].contains(Screen.mse)){
						Screen.room.block[y][x].rangeVisible = false;
					}
				}
			}
			if(holdsItem == true) {holdsItem = false;}
			if(store == false) {store = true;}
		}

not working anymore, I can tell this because without the Store = true; in the first code it’s working good only you will always see the Upgrademenu that’s why it needs to be false, and same with UpgradeScreen =0; then you will never see the good stats. Anyone got 1 idea for this? Am I missing some point here? please tell me

Whole Code:

public void click(int mouseButton) {
		if(mouseButton == 1) {	
			for(int y = 0; y<Screen.room.block.length;y++) {
				for(int x = 0; x<Screen.room.block[0].length;x++) {
					if(Screen.room.block[y][x].contains(Screen.mse)){
						Screen.room.block[y][x].rangeVisible = true;
						store = false;
						if(Screen.room.block[y][x].airID == 2) {UpgradeScreen = 1;}//airTowerLaser1.
						if(Screen.room.block[y][x].airID == 3) {UpgradeScreen = 2;}//airTowerLaser2.
						if(Screen.room.block[y][x].airID == 4) {UpgradeScreen = 3;}//airTowerLaser3.
						if(Screen.room.block[y][x].airID == 5) {UpgradeScreen = 4;}//airTowerArcher1.
						if(Screen.room.block[y][x].airID == 6) {UpgradeScreen = 5;}//airTowerArcher2.
						if(Screen.room.block[y][x].airID == 7) {UpgradeScreen = 6;}//airTowerArcher3.
					}else{
						UpgradeScreen = 0;
						Screen.room.block[y][x].rangeVisible = false;
						//if(store == false) {store = true;}
					}
				}
			}
			for(int i=0;i<button.length;i++) {
				if(button[i].contains(Screen.mse)) {
					if(store == true){
						if(buttonID[i] != Value.airAir) {
							if(buttonID[i] == Value.airTrashCan) { 
								holdsItem = false;
							} else {
								heldID = buttonID[i];
								realID = i;
								holdsItem = true;
							}
						}
					}
				}
				
				if(holdsItem) {
					if(Screen.money >= buttonPrice[realID]) {
						for(int y = 0; y<Screen.room.block.length;y++) {
							for(int x = 0; x<Screen.room.block[0].length;x++) {
								if(Screen.room.block[y][x].contains(Screen.mse)){
									if(Screen.room.block[y][x].groundID == Value.groundRoad){holdsItem = false;}
									if(Screen.room.block[y][x].groundID != Value.groundRoad && Screen.room.block[y][x].airID == Value.airAir) {
										Screen.room.block[y][x].airID =  heldID;
										Screen.money -= buttonPrice[realID];
										holdsItem = false;
										Screen.store.store = false;
										if(Screen.room.block[y][x].airID == 2) {UpgradeScreen = 1;}//airTowerLaser1.
										if(Screen.room.block[y][x].airID == 3) {UpgradeScreen = 2;}//airTowerLaser2.
										if(Screen.room.block[y][x].airID == 4) {UpgradeScreen = 3;}//airTowerLaser3.
										if(Screen.room.block[y][x].airID == 5) {UpgradeScreen = 4;}//airTowerArcher1.
										if(Screen.room.block[y][x].airID == 6) {UpgradeScreen = 5;}//airTowerArcher2.
										if(Screen.room.block[y][x].airID == 7) {UpgradeScreen = 6;}//airTowerArcher3.
									}
								}
							}
						}
					}
				}
			}
		}

-Roseslayer
Thanks already.

I don’t have too much time to look at the code atm; Could you explain what the store boolean is for and what it does? (I’ll be able to help a bit more after that :smiley: )

the store boolean is only for to show if the store is on, I’ll post some pictures about it,

Store true;

http://img820.imageshack.us/img820/4817/storeon.jpg

Store false && UpgradeScreen= 1;

http://img21.imageshack.us/img21/8179/storeoff.jpg

Store draw script:


	public void draw (Graphics g) {
		ImageIcon b = new ImageIcon("res/HUD.png");
		image = b.getImage();
		g.drawImage(image, 0, 0, null);
		
		if(store == false){
			//Makes Buttons
			for(int i=0;i<button.length;i++) {
				//Sell
				if(button[i].contains(Screen.mse)) {
					g.setColor(new Color(255,255,255,120));
					g.fillRect(500,  443+6, 52, 52);
				}
				g.drawImage(Screen.tileset_res[0], 500,  443+6, 52, 52, null);
				g.drawImage(Screen.tileset_air[Value.airSell], 500,  443+6, 52, 52, null);
				//Upgrade
				if(button[i].contains(Screen.mse)) {
					g.setColor(new Color(255,255,255,120));
					g.fillRect(440,  443+6, 52, 52);
				}
				g.drawImage(Screen.tileset_res[0], 440,  443+6, 52, 52, null);
				g.drawImage(Screen.tileset_air[Value.airUpgrade], 440 ,  443+6, 52, 52, null);
			}
			//Showing the tower stats.
			g.setFont(new Font("Courier New", Font.BOLD, 18));
			g.setColor(new Color(41,41,41));
			g.drawRect(198, 443, 154, 64);
			g.drawRect(199, 444, 152, 62);
			g.setColor(new Color(98,98,98,120));
			g.fillRect(200, 445, 151, 61);
			g.setColor(new Color(41,41,41));
			g.drawString(" TOWER STATS", 200, 458);
			g.setFont(new Font("Courier New", Font.BOLD, 14));
			if (UpgradeScreen== 1) {
				g.drawString(" Damage:       20", 200, 470);
				g.drawString(" Range:        180", 200, 480);
				g.drawString(" Rate Of Fire: 100", 200, 490);
				g.drawString(" Sell Price:   10", 200, 500);
			}	
			if (UpgradeScreen== 2) {
				g.drawString(" Damage:       35", 200, 470);
				g.drawString(" Range:        140", 200, 480);
				g.drawString(" Rate Of Fire: 150", 200, 490);
				g.drawString(" Sell Price:   30", 200, 500);
			}
			if (UpgradeScreen== 3) {
				g.drawString(" Damage:       60", 200, 470);
				g.drawString(" Range:        100", 200, 480);
				g.drawString(" Rate Of Fire: 175", 200, 490);
				g.drawString(" Sell Price:   60", 200, 500);
			}
			if (UpgradeScreen== 4) {
				g.drawString(" Damage:       10", 200, 470);
				g.drawString(" Range:        150", 200, 480);
				g.drawString(" Rate Of Fire: 300", 200, 490);
				g.drawString(" Sell Price:   20", 200, 500);
			}	
			if (UpgradeScreen== 5) {
				g.drawString(" Damage:       15", 200, 470);
				g.drawString(" Range:        175", 200, 480);
				g.drawString(" Rate Of Fire: 350", 200, 490);
				g.drawString(" Sell Price:   45", 200, 500);
			}
			if (UpgradeScreen== 6) {
				g.drawString(" Damage:       30", 200, 470);
				g.drawString(" Range:        200", 200, 480);
				g.drawString(" Rate Of Fire: 450", 200, 490);
				g.drawString(" Sell Price:   90", 200, 500);
			}
		}
		if(store == true) {
			for(int i=0;i<button.length;i++) {
				if(button[i].contains(Screen.mse)) {
					g.setColor(new Color(255,255,255,120));
					g.fillRect(button[i].x, button[i].y, button[i].width, button[i].height);
				}
				
				g.drawImage(Screen.tileset_res[0], button[i].x, button[i].y, button[i].width, button[i].height, null);
				if(buttonID[i] != Value.airAir)g.drawImage(Screen.tileset_air[buttonID[i]], button[i].x + itemIn, button[i].y + itemIn, button[i].width - (itemIn*2), button[i].height - (itemIn*2), null);
				if(buttonPrice[i] > 0) {
					g.setFont(new Font("Courier New", Font.BOLD, 14));
					g.setColor(new Color(41,41,41));
					g.drawString("€" +buttonPrice[i] + ",-",  button[i].x + itemIn, button[i].y + itemIn + 62);
				}
			}
		}
		
		g.drawImage(Screen.tileset_res[1], buttonHealth.x, buttonHealth.y, buttonHealth.width, buttonHealth.height, null);
		g.drawImage(Screen.tileset_res[2], buttonCoins.x, buttonCoins.y, buttonCoins.width, buttonCoins.height, null);
		g.drawImage(Screen.tileset_res[3], buttonKilled.x, buttonKilled.y, buttonKilled.width, buttonKilled.height, null);
		g.setFont(new Font("Courier New", Font.BOLD, 14));
		g.setColor(new Color(236, 0, 0));
		g.drawString("" + Screen.health +"/" + Screen.maxHealth, buttonHealth.x + buttonHealth.width + iconSpace, buttonHealth.y + iconTextY);
		g.setColor(new Color(255, 205, 6));
		g.drawString("" + Screen.money, buttonCoins.x + buttonCoins.width + iconSpace, buttonCoins.y + iconTextY);
		g.setColor(new Color(190, 189, 187));
		g.drawString("" + Screen.killed, buttonKilled.x + buttonKilled.width + iconSpace, buttonKilled.y + iconTextY);
		g.setColor(new Color(255, 255, 255));
		
		if(holdsItem) {
			g.drawImage(Screen.tileset_air[heldID], Screen.mse.x - ((button[0].width - (itemIn*2) )/2) + itemIn, Screen.mse.y - ((button[0].width - (itemIn*2) )/2) + itemIn, button[0].width - (itemIn*2), button[0].height - (itemIn*2), null);
		}
	}

You should not be doing this



if (UpgradeScreen== 1) {
            g.drawString(" Damage:       20", 200, 470);
            g.drawString(" Range:        180", 200, 480);
            g.drawString(" Rate Of Fire: 100", 200, 490);
            g.drawString(" Sell Price:   10", 200, 500);
         }   
         if (UpgradeScreen== 2) {
            g.drawString(" Damage:       35", 200, 470);
            g.drawString(" Range:        140", 200, 480);
            g.drawString(" Rate Of Fire: 150", 200, 490);
            g.drawString(" Sell Price:   30", 200, 500);
         }
         if (UpgradeScreen== 3) {
            g.drawString(" Damage:       60", 200, 470);
            g.drawString(" Range:        100", 200, 480);
            g.drawString(" Rate Of Fire: 175", 200, 490);
            g.drawString(" Sell Price:   60", 200, 500);
         }
         if (UpgradeScreen== 4) {
            g.drawString(" Damage:       10", 200, 470);
            g.drawString(" Range:        150", 200, 480);
            g.drawString(" Rate Of Fire: 300", 200, 490);
            g.drawString(" Sell Price:   20", 200, 500);
         }   
         if (UpgradeScreen== 5) {
            g.drawString(" Damage:       15", 200, 470);
            g.drawString(" Range:        175", 200, 480);
            g.drawString(" Rate Of Fire: 350", 200, 490);
            g.drawString(" Sell Price:   45", 200, 500);
         }
         if (UpgradeScreen== 6) {
            g.drawString(" Damage:       30", 200, 470);
            g.drawString(" Range:        200", 200, 480);
            g.drawString(" Rate Of Fire: 450", 200, 490);
            g.drawString(" Sell Price:   90", 200, 500);
         }

It should be:


int damage = getDamage(UpgradeScreen); // you need to create these methods
int range = getRange(UpgradeScreen);
int rate = getRate(UpgradeScreen);
int price = getPrice(UpgradeScreen);

g.drawString(" Damage:       " + damage, 200, 470);
g.drawString(" Range:        " + range, 200, 480);
g.drawString(" Rate Of Fire: " + rate, 200, 490);
g.drawString(" Sell Price:   " + price, 200, 500);

This will save heaps of time when coding.

As an answer to your question, it’s a design problem. We can’t tell you how to design your game.
You have to do that yourself. It’s a part of programming.

As for your other problem, USE THE ELSE-IF STATEMENT!
That will solve a lot of problems.

How you mean use the else-if statement? This will not change a thing to

 					if(Screen.room.block[y][x].contains(Screen.mse)){
						Screen.room.block[y][x].rangeVisible = true;
						store = false;
						if(Screen.room.block[y][x].airID == 2) {UpgradeScreen = 1;}//airTowerLaser1.
						if(Screen.room.block[y][x].airID == 3) {UpgradeScreen = 2;}//airTowerLaser2.
						if(Screen.room.block[y][x].airID == 4) {UpgradeScreen = 3;}//airTowerLaser3.
						if(Screen.room.block[y][x].airID == 5) {UpgradeScreen = 4;}//airTowerArcher1.
						if(Screen.room.block[y][x].airID == 6) {UpgradeScreen = 5;}//airTowerArcher2.
						if(Screen.room.block[y][x].airID == 7) {UpgradeScreen = 6;}//airTowerArcher3.
					}else if(!Screen.room.block[y][x].contains(Screen.mse)){
						UpgradeScreen = 0;
						Screen.room.block[y][x].rangeVisible = false;
						if(store == false) {store = true;}
					}

Or am I using it wrong at some point

-Roseslayer
Thanks

And in that code…

                if(Screen.room.block[y][x].contains(Screen.mse)){
                  Screen.room.block[y][x].rangeVisible = true;
                  store = false;
                  if((Screen.room.block[y][x].airID >= 2) && (Screen.room.block[y][x].airID <= 7)) {
                         UpdateScreen = Screen.room.block[y][x].airID - 1;
                  }
               } else {
                  UpgradeScreen = 0;
                  Screen.room.block[y][x].rangeVisible = false;
                  if(store == false) {store = true;}
               }

Although that code in general seems odd.

It’s a performance improvement.

Also this:

if(store == false) {store = true;}

You are taking more CPU time than if you just went [icode]store = true[/icode]

All true, but still my problem isn’t fixed the CPU is only faster, thanks for that but still I need help at my “main” problem.

-Roseslayer
Thanks

I’m sure the JVM optimizes this.
It’s more a code smell or just senseless to put the if there.
But basically you’re right.

Anyone know how to fix the main problem? you cant go in upgradeStore cause it will always be 0 and store will always be true if you arent pressing. someone know what I am doing wrong here?

-Roseslayer
Thanks already

Do some debugging and find which values are wrong, and where they go wrong.

Put in a few System.out.println()'s showin whatever values you think might be wrong, then if they are, track them back until you find the source of the problem.

In my opinion everybody use Eclipse -> use the debugger
At the beginning it’s not that easy, but u will enjoy it afterwards :slight_smile:

I know what you want to accomplish. When you click on a tower, it shows an upgrade window for that tower. Well, first off, only one tower can be selected at a time. So, you’ll need a way to select only one tower. Since that seems to be the only problem, I’ll show you how to get that effect with this simple code.


public boolean[][] oneOnly;

public void init(){
       oneOnly = new boolean[10][10];
}

public void update(){
       for(int i = 0; i < oneOnly.length(); i++){
              for(int j = 0; j < oneOnly[i].length; j++){
                     if(!oneOnly[i][j]){
                             // Do the basic tower actions here
                     } 
                     else {
                             //Do the menu changes if this button contains a tower
                             //If not, then do the basic actions
                     }     
              }
       }
}

public void click( int button ){
     if( button == 1){
           for(int i = 0; i < oneOnly.length; i++){
                for(int j = 0; j < oneOnly[i].length; j++){
                          oneOnly[i][j] = false;
                }
           }
           //Guarantees that only one object is selected
           oneOnly[mousex][mousey] = true;
     }
}

It isn’t exactly like your code, because you are using buttons. But, this example makes sure that one object is selected at all times, so you can mimic it and probably get it to work.

Ctomni, sorry for my late reaction, but can you explain your script? If you know what I mean, cause I’m kinda new to java and I don’t really know what you’re trying to do there.

-Roseslayer
Thanks

You’re already pretty lucky to get someone to write the code for you, and know you want him to put it in your program too? This just tells me that you don’t understand the code. Look at it and understand it and you will naturally know where to put the code.

I know where to put this code, but I fully want to understand this code, because I want to know what I am doing in my own programm right?

[EDIT]
The only parts I don’t understand are oneOnly.length();
and oneOnly[mousex][mousey] = true;

You mean, the only parts which give errors are these lines :wink:
Okey, sorry for picking around on you :smiley:

I’m sure the first one is supposed to be [icode]oneOnly.length[/icode] and the second one: You need to get the mouse position in your window somehow.
There are several ways to do this with java2D. I guess you should google that, because that was answered often.