Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  Problem when using break  (Read 602 times)
0 Members and 1 Guest are viewing this topic.
Offline xsi3rr4x

Junior Devvie





« Posted 2014-05-11 17:30:29 »

Hey guys, iv got a problem in my code where in swapping items in my inventory, here's my code for it

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
   for(int i=0; i<inventoryCopy.length;i++){
               if(inventoryCopy[i].getX()-GameLoop.mousePX<8&&inventoryCopy[i].getY()-GameLoop.mousePY<8&&inventoryCopy[i].getX()-GameLoop.mousePX>-8&&inventoryCopy[i].getY()-GameLoop.mousePY>-8){
                  one = i;
                  inventoryCopy[i].setX(GameLoop.mouseX); inventoryCopy[i].setY(GameLoop.mouseY);
                  g.drawImage(inventoryCopy[i].getSprite(), inventoryCopy[i].getX(), inventoryCopy[i].getY(), this);
                  selected=true;}break;}
            for(int i=0; i<inventoryCopy.length;i++){
               if(selected==true&&inventoryCopy[i].getX()-GameLoop.mousePX<8&&inventoryCopy[i].getY()-GameLoop.mousePY<8&&inventoryCopy[i].getX()-GameLoop.mousePX>-8&&inventoryCopy[i].getY()-GameLoop.mousePY>-8&&inventoryCopy[i]!=inventoryCopy[one]){
                   selected2=false;
                   System.out.println("works");
                  two=i;
                  if(one>-1&&two>-1){
                    inventoryCopy = swap(inventoryCopy,one, two);
                    one=-1;
                    two=-1;
                  }}}


1  
2  
3  
4  
5  
6  
7  
8  
 //this method is called when an item is to be swapped around.
public ItemEntity[] swap(ItemEntity[] c, int first, int second){
   ItemEntity temp = c[first];
   c[first] = c[second];
   c[second] = temp;
   selected=false;
   return c;
}


the problem im having is that to get items to swap properly I have to have that break statement in the first 'for loop' so that the statement goes to the next loop and does the swapping, it works properly but it only works once, meaning after I swap an item around for the first time than it wont work again. The first 'for loop' isnt executing anymore and therefor I cant swap anything around after I swapped my first item.
Iv been messing around with this for awhile, but am pretty stumped, any ideas would be extremely helpful (:
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #1 - Posted 2014-05-11 17:38:59 »

Well.. I would start with debugging your code and going through it step by step. Honestly, that code is formatted in a way that hurts my eyes... I can't begin to try to figure out what you're doing. So you need to use your debugger and figure out what might be going wrong, and then ask us a more specific question.

Offline UprightPath
« Reply #2 - Posted 2014-05-11 19:16:38 »

Try using continue instead of break:

break- ends the loop it was called within.
continue- continues at the start of the next iteration.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline xsi3rr4x

Junior Devvie





« Reply #3 - Posted 2014-05-11 19:49:26 »

Try using continue instead of break:

break- ends the loop it was called within.
continue- continues at the start of the next iteration.

I actually have no experience with 'continue statements' I'm having a hard time figuring out how to use them, but if I used a continue wouldnt it just go through the next iteration of the same loop? What I'm wanting to do is jump to the next loop, do all the stuff in the next loop and then have it so that I can go back to the first loop after the 2nd loop is finished......Im not sure if I'm wording everything correctly, but yeah...
Offline UprightPath
« Reply #4 - Posted 2014-05-11 20:07:02 »

To understand what break and continue are doing you have to under the idea of iterations. A loop is a series of iterations which typically do the same thing. In this case, it performs the body of the for, or while, loop until the exit condition is met. Sometimes you want to be able to interrupt the normal execution of a loop so you can use a break, which completely exits the loop, or a continue which starts the next iteration immediately. Let's say you want to do something silly like "Take an int array of length ten and subtract one from each."

You can do:
1  
2  
3  
4  
5  
int[] array = new int[]{10, 1, 0, 1, 2, 5, 7, 2, 3, 4};
for(int i = 0; i < array.length; i++) {
   array[i]  -= 1;
}
System.out.println(array);

This would print out something like: [9, 0, -1, 0, 1, 4, 6, 1, 2, 3]

Let's say you decide what you really want to do is subtract one until you get to a value that's already one, you'd do:
1  
2  
3  
4  
5  
6  
7  
8  
int[] array = new int[]{10, 1, 0, 1, 2, 5, 7, 2, 3, 4};
for(int i = 0; i < array.length; i++) {
   if(array[i] == 0) {
      break;
   }
   array[i]  -= 1;
}
System.out.println(array);

This chunk of code would break at i = 2 and print out something like: [9, 0, 0, 1, 2, 5, 7, 2, 3, 4].

Let's say instead you want to do the subtraction but you don't want any value reduced to a negative number. You'd do:
1  
2  
3  
4  
5  
6  
7  
8  
int[] array = new int[]{10, 1, 0, 1, 2, 5, 7, 2, 3, 4};
for(int i = 0; i < array.length; i++) {
   if(array[i] == 0) {
      continue;
   }
   array[i]  -= 1;
}
System.out.println(array);

This chunk would skip the array -= 1; for i = 2 only (Basically halt the rest of that iteration) but continue the loop and output something like: [9, 0, 0, 0, 1, 4, 6, 1, 2, 3]

Offline Cero
« Reply #5 - Posted 2014-05-11 22:39:21 »

I think he already understood the difference between break and continue

from what you have been saying I would assume that you were talking about a loop within a loop, right ?
looking at that mess of a code snippet you posted it also looked like it.

So I formatted it properly (the way I like it...)

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  
30  
31  
32  
33  
34  
35  
36  
for(int i = 0; i < inventoryCopy.length;i++)
{
       if(inventoryCopy[i].getX() - GameLoop.mousePX < 8 &&
        inventoryCopy[i].getY() - GameLoop.mousePY < 8 &&
        inventoryCopy[i].getX() - GameLoop.mousePX >-8 &&
        inventoryCopy[i].getY() - GameLoop.mousePY>-8)
       {
              one = i;
              inventoryCopy[i].setX(GameLoop.mouseX);
              inventoryCopy[i].setY(GameLoop.mouseY);
              g.drawImage(inventoryCopy[i].getSprite(), inventoryCopy[i].getX(), inventoryCopy[i].getY(), this);
              selected = true;
       }
break;
}
       
       
for(int i = 0; i < inventoryCopy.length;i++)
{
   if(selected == true && inventoryCopy[i].getX() - GameLoop.mousePX < 8 &&
     inventoryCopy[i].getY() - GameLoop.mousePY <  8 &&
     inventoryCopy[i].getX() - GameLoop.mousePX > -8 &&
     inventoryCopy[i].getY() - GameLoop.mousePY > -8 &&
     inventoryCopy[i] != inventoryCopy[one])
   {
        selected2 = false;
        System.out.println("works");
        two = i;
        if(one >- 1 && two >- 1)
        {
           inventoryCopy = swap(inventoryCopy,one, two);
           one=-1;
           two=-1;
        }
  }
}


As you can see there are actually 2 loops after each other not nested.
Is this correct or did your horrible formatting lead to this misunderstanding of your own code / brackets ? =P

Code has to be easily readable for humans

Offline xsi3rr4x

Junior Devvie





« Reply #6 - Posted 2014-05-11 22:46:22 »

Yes cero, thats how it is written, yeah I do need to get a lot better with my formatting, this is my first big project and am quickly learning how important it is to keep it all straight.
Anyways from what I can see 'continue' wont help me here though =/
Online Longarmx
« Reply #7 - Posted 2014-05-11 22:53:39 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
for(int i = 0; i < inventoryCopy.length;i++)
{
       if(inventoryCopy[i].getX() - GameLoop.mousePX < 8 &&
        inventoryCopy[i].getY() - GameLoop.mousePY < 8 &&
        inventoryCopy[i].getX() - GameLoop.mousePX >-8 &&
        inventoryCopy[i].getY() - GameLoop.mousePY>-8)
       {
              one = i;
              inventoryCopy[i].setX(GameLoop.mouseX);
              inventoryCopy[i].setY(GameLoop.mouseY);
              g.drawImage(inventoryCopy[i].getSprite(), inventoryCopy[i].getX(), inventoryCopy[i].getY(), this);
              selected = true;
       }
break;
}


Whenever break gets called, it 'breaks out' of the loop.  In your code, break is being called, no matter what, at the end of the loop. This means that the loop will always only run once. My guess is that you wanted it inside the if statement maybe?

Offline xsi3rr4x

Junior Devvie





« Reply #8 - Posted 2014-05-12 00:03:52 »

Let me see if I can breakdown and explain the code in a much better way.

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  
30  
31  
32  
33  
34  
35  
for(int i = 0; i < inventoryCopy.length;i++)
{
       if(inventoryCopy[i].getX() - GameLoop.mousePX < 8 &&
        inventoryCopy[i].getY() - GameLoop.mousePY < 8 &&
        inventoryCopy[i].getX() - GameLoop.mousePX >-8 &&
        inventoryCopy[i].getY() - GameLoop.mousePY>-8)
       {
              one = i;
              inventoryCopy[i].setX(GameLoop.mouseX);
              inventoryCopy[i].setY(GameLoop.mouseY);
              g.drawImage(inventoryCopy[i].getSprite(), inventoryCopy[i].getX(), inventoryCopy[i].getY(), this);
              selected = true;
       }
break;
}
       
       
for(int i = 0; i < inventoryCopy.length;i++)
{
   if(selected == true && inventoryCopy[i].getX() - GameLoop.mousePX < 8 &&
     inventoryCopy[i].getY() - GameLoop.mousePY <  8 &&
     inventoryCopy[i].getX() - GameLoop.mousePX > -8 &&
     inventoryCopy[i].getY() - GameLoop.mousePY > -8 &&
     inventoryCopy[i] != inventoryCopy[one])
   {
        System.out.println("works");
        two = i;
        if(one >- 1 && two >- 1)
        {
           inventoryCopy = swap(inventoryCopy,one, two);
           one=-1;
           two=-1;
        }
  }
}


So as I said before the purpose for these loops is to swap items in my inventory.
In the first 'for loop' I have an 'if statement' that determines if you were to click on an item in the inventory than it marks the location in the array in my "one" variable which is an int,
afterwards the image of the item follows the mouse curser.
and makes the boolean selected "true"

What I'm wanting to do is after I have an item "selected" as I do in the above for loop than I want the flow of control to move to the second for loop.

which the 'if statement' in the second 'for statement' executes if selected is "true" and if you click on a second item in the inventory than it marks the location in the array for that item to the "two" variable which is an int.
and than it calls my swap method.

1  
2  
3  
4  
5  
6  
7  
public ItemEntity[] swap(ItemEntity[] c, int first, int second){
   ItemEntity temp = c[first];
   c[first] = c[second];
   c[second] = temp;
   selected=false;
   return c;
}
Offline UprightPath
« Reply #9 - Posted 2014-05-12 00:12:02 »

Try moving the break into the if statement.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline xsi3rr4x

Junior Devvie





« Reply #10 - Posted 2014-05-12 00:19:09 »

When I move the 'break' into the 'if statment' when I click on one object it selects it (as it should), but when I click on the second item than it just selects that second item and does not perform the swap.
Online BurntPizza

« JGO Bitwise Duke »


Medals: 296
Exp: 5 years



« Reply #11 - Posted 2014-05-12 00:39:35 »

Try this:

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  
30  
31  
32  
33  
34  
35  
36  
37  
if(!selected)
for(int i = 0; i < inventoryCopy.length;i++)
{
       if(inventoryCopy[i].getX() - GameLoop.mousePX < 8 &&
        inventoryCopy[i].getY() - GameLoop.mousePY < 8 &&
        inventoryCopy[i].getX() - GameLoop.mousePX >-8 &&
        inventoryCopy[i].getY() - GameLoop.mousePY>-8)
       {
              one = i;
              inventoryCopy[i].setX(GameLoop.mouseX);
              inventoryCopy[i].setY(GameLoop.mouseY);
              g.drawImage(inventoryCopy[i].getSprite(), inventoryCopy[i].getX(), inventoryCopy[i].getY(), this);
              selected = true;
             break;
       }
//break;
}
       
       
for(int i = 0; i < inventoryCopy.length;i++)
{
   if(selected == true && inventoryCopy[i].getX() - GameLoop.mousePX < 8 &&
     inventoryCopy[i].getY() - GameLoop.mousePY <  8 &&
     inventoryCopy[i].getX() - GameLoop.mousePX > -8 &&
     inventoryCopy[i].getY() - GameLoop.mousePY > -8 &&
     inventoryCopy[i] != inventoryCopy[one])
   {
        System.out.println("works");
        two = i;
        if(one >- 1 && two >- 1)
        {
           inventoryCopy = swap(inventoryCopy,one, two);
           one=-1;
           two=-1;
        }
  }
}


Other than that, listener-based event handling makes life much easier.


Also, I wouldn't put the selected = false; logic in the swap method and you don't need to return the array from it, but whatever.
Offline xsi3rr4x

Junior Devvie





« Reply #12 - Posted 2014-05-12 00:50:29 »

That works in a much more satisfactory way, although it does not 'de-select' the item after the swap is made

so whats happening is I select the item, than swamp it, but the item stays selected so when I click on another spot it instantly swaps to the 'other' spot instead of selecting the item at the 'other' spot
Online BurntPizza

« JGO Bitwise Duke »


Medals: 296
Exp: 5 years



« Reply #13 - Posted 2014-05-12 01:06:18 »

It sounds to me that this algorithm has two states:

selected == false: the tile being clicked is to be selected and dragged by the mouse
selected == true: the tile being clicked is to be swapped with the currently selected tile, which is in turn to be de-selected.

In other words only one loop should run at a time, which also means it can be made into one loop:

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  
for(int i = 0; i < inventoryCopy.length;i++) {
    if(inventoryCopy[i].getX() - GameLoop.mousePX < 8 &&
    inventoryCopy[i].getY() - GameLoop.mousePY < 8 &&
    inventoryCopy[i].getX() - GameLoop.mousePX >-8 &&
    inventoryCopy[i].getY() - GameLoop.mousePY>-8)
    {
        if(!selected) {
            one = i;
            inventoryCopy[i].setX(GameLoop.mouseX);
            inventoryCopy[i].setY(GameLoop.mouseY);
            // I would put this somewhere else, right now it only draws once, when you initially click
            g.drawImage(inventoryCopy[i].getSprite(), inventoryCopy[i].getX(), inventoryCopy[i].getY(), this);
            selected = true;
        } else {
            System.out.println("works");
            two = i;
            if(one >- 1 && two >- 1) {
                inventoryCopy = swap(inventoryCopy,one, two);
                selected = false;
                // need to reset inventoryCopy[one] and inventoryCopy[two] positions
                one=-1;
                two=-1;
            }
        }
        break; // EDIT: forgot to add this back in
    }  
}
Offline xsi3rr4x

Junior Devvie





« Reply #14 - Posted 2014-05-12 01:35:39 »

Thank you so much for the help, that kind of works, although I do have to click the two  items I want to swap multiple times, its kind of weird, but at least it works enough to where I can somewhat reliably swap objects around in my inventory....I'll work on it some more after I get some sleep lol
Offline UprightPath
« Reply #15 - Posted 2014-05-12 01:42:28 »

Edit: Eep, misread the displayed code!

Offline xsi3rr4x

Junior Devvie





« Reply #16 - Posted 2014-05-12 19:46:04 »

hey burnt pizza, in your code snippet that you sent what do you mean when you say that I need to reset inventoryCopy[one] and inventoryCopy[two] positions?
Online BurntPizza

« JGO Bitwise Duke »


Medals: 296
Exp: 5 years



« Reply #17 - Posted 2014-05-12 20:15:26 »

hey burnt pizza, in your code snippet that you sent what do you mean when you say that I need to reset inventoryCopy[one] and inventoryCopy[two] positions?

Well, if the inventory rendering depends on inventoryBlah[...].getX() etc, then currently these two lines
inventoryCopy[i].setX(GameLoop.mouseX);
inventoryCopy[i].setY(GameLoop.mouseY);
would ruin the rendering.

You should either reset the positions after the item is "dropped" back into the inventory to (deselected), or simply not change it:

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  
30  
31  
32  
33  
34  
35  
36  
37  
// somewhere
int selectedIndex = -1;

for(int i = 0; i < inventoryCopy.length;i++) {
    if(inventoryCopy[i].getX() - GameLoop.mousePX < 8 &&
    inventoryCopy[i].getY() - GameLoop.mousePY < 8 &&
    inventoryCopy[i].getX() - GameLoop.mousePX >-8 &&
    inventoryCopy[i].getY() - GameLoop.mousePY>-8)
    {
        if(!selected) {
            one = i;
            //inventoryCopy[i].setX(GameLoop.mouseX);
            //inventoryCopy[i].setY(GameLoop.mouseY);
            selectedIndex = i;
            //g.drawImage(inventoryCopy[i].getSprite(), inventoryCopy[i].getX(), inventoryCopy[i].getY(), this);
            selected = true;
        } else {
            System.out.println("works");
            two = i;
            if(one >- 1 && two >- 1) {
                inventoryCopy = swap(inventoryCopy,one, two);
                selectedIndex = -1;
                selected = false;
                one=-1;
                two=-1;
            }
        }
        break;
    }  
}


// meanwhile, in the render method:

if(selected) { // alternatively, ditch the selected variable altogether and test for selectedIndex != -1
    g.drawImage(inventoryCopy[selectedIndex].getSprite(), GameLoop.mousePX, GameLoop.mousePY, null); // we wanted to draw it at the mouse coordinates, why use a middleman?
}
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.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (54 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (85 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (79 views)
2014-12-05 12:39:02

SHC (90 views)
2014-12-03 16:27:13

CopyableCougar4 (97 views)
2014-11-29 21:32:03

toopeicgaming1999 (156 views)
2014-11-26 15:22:04

toopeicgaming1999 (153 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!