Page 1 of 2 12 LastLast
Results 1 to 20 of 32
  1. #1
    Aboogesnickle is offline Member
    Join Date
    Jun 2014
    Posts
    37
    Rep Power
    0

    Default How do you skip to a certain line of code?

    Basically what I want to do is have an if statement that if true then skip down so many lines

    Java Code:
    if(true){
                   //skip
              }
             //more lines of code that i do not want ran
             
    
     if(){
          //skip to here and run code
     }
    I have done this in other programming, but not in Java. I do not know if it is even possible.

    Java Code:
    		// move B2D body, set curState
    		if (Gdx.input.isKeyPressed(Keys.S) || Gdx.input.isKeyPressed(Keys.W) || Gdx.input.isKeyPressed(Keys.A)
    				|| Gdx.input.isKeyPressed(Keys.D)) {
    			if (Gdx.input.isKeyPressed(Keys.S) && body.getLinearVelocity().y >= -speed) {
    				body.applyLinearImpulse(new Vector2(0, -0.3f), body.getWorldCenter(), true);
    				curState = State.WALKDOWN;
    			}
    			if (Gdx.input.isKeyPressed(Keys.W) && body.getLinearVelocity().y <= speed) {
    				body.applyLinearImpulse(new Vector2(0, 0.3f), body.getWorldCenter(), true);
    				curState = State.WALKUP;
    			}
    			if (Gdx.input.isKeyPressed(Keys.A) && body.getLinearVelocity().x >= -speed) {
    				body.applyLinearImpulse(new Vector2(-0.3f, 0), body.getWorldCenter(), true);
    				curState = State.WALKLEFT;
    			}
    			if (Gdx.input.isKeyPressed(Keys.D) && body.getLinearVelocity().x <= speed) {
    				body.applyLinearImpulse(new Vector2(0.3f, 0), body.getWorldCenter(), true);
    				curState = State.WALKRIGHT;
    			}
    			// set state for if angled direction
    			if (Gdx.input.isKeyPressed(Keys.S) && Gdx.input.isKeyPressed(Keys.A)) {
    				curState = State.WALKDOWN;
    			} else if (Gdx.input.isKeyPressed(Keys.S) && Gdx.input.isKeyPressed(Keys.D)) {
    				curState = State.WALKDOWN;
    			} else if (Gdx.input.isKeyPressed(Keys.W) && Gdx.input.isKeyPressed(Keys.A)) {
    				curState = State.WALKUP;
    			} else if (Gdx.input.isKeyPressed(Keys.W) && Gdx.input.isKeyPressed(Keys.D)) {
    				curState = State.WALKUP;
    			}
    
    		} else {
    			body.setAwake(false);
    		}
    Im using Box2D from libgdx. I want to be able to hold down a direction key(ex. up) and while holding it(up) press the opposite direction(ex. down) and it go (down) that way until I either release it(down) or reclick original direction(up) and when I release the opposite direction(down) while still holding original direction(up) I want it to go original direction(up).

    I do not know if that is understandable, but the problem I am running into is that it just goes the first direction pressed until I release. I did get it to work in one direction by adding a blank if statement and putting the impulsive/ curstate lines in an else statement

    Java Code:
    if (Gdx.input.isKeyPressed(Keys.S) && body.getLinearVelocity().x >= -speed) {
                         if(Gdx.input.isKeyPressed(Keys.W){
                          }else{
    				body.applyLinearImpulse(new Vector2(-0.3f, 0), body.getWorldCenter(), true);
    				curState = State.WALKLEFT;
                                 }
    			}
    This gives the desired outcome in the down direction only
    Last edited by Aboogesnickle; 12-23-2015 at 04:14 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How do you skip to a certain line of code?

    The if statement can control the execution of the statements within the {}s that follow it.
    When true, the statements within the {}s are executed.
    If the if statement's condition is false, the statements within the {}s are not executed.

    What you are asking for sounds like a goto statement which does not exist in java (or most modern languages).

    A combination of if and else statements should give you control of the order of execution of statements.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How do you skip to a certain line of code?

    Quote Originally Posted by Aboogesnickle View Post
    I have done this in other programming, but not in Java. I do not know if it is even possible.
    Java my not have the specific keyword constructs which you describe. I recommend you provide the name of the language and an example in said language. Make certain that conditionals are properly aligned and the format is readable. I am certain that someone on this forum can figure out what you are trying to do and provide some equivalent code in Java.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Aboogesnickle is offline Member
    Join Date
    Jun 2014
    Posts
    37
    Rep Power
    0

    Default Re: How do you skip to a certain line of code?

    I added the code im trying to get to work. Also the specific command I am referring to is the jmp command in assembly language. You could "compare" one registry with the one you specify and it would jump to where you tell it to. You named the jump like a variable.

    like

    cmp 0, eax;
    jmp NAME;

    //some other code that is skipped if registry == 0


    NAME:
    //where code picks back up

    It would change the pointer that was pointing to the next line of code to the line called NAME:
    Last edited by Aboogesnickle; 12-23-2015 at 04:22 AM.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How do you skip to a certain line of code?

    You can do what you want using the Java event dispatch system. In your case it would be using the key listener interface and setting appropriate flags based on the state of the keys.

    Edit: This answer was based on the Java code you provided using the up/down requirement.

    Regards,
    Jim
    Last edited by jim829; 12-23-2015 at 04:30 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How do you skip to a certain line of code?

    Here is a small piece of code (that could certainly stand some improvement) to demonstrate what I think you want. The letters 'a' and 'b' print on the console as you press them. The letter 'b' has priority.

    Java Code:
    import java.awt.Dimension;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class KeyListenerDemo extends JPanel {
       JFrame frame;
       boolean aKey = false;
       boolean bKey = false; // takes priority over 'a' key.
       
       
       public KeyListenerDemo() {
          frame = new JFrame("Demo");
          setFocusable(true);
          setPreferredSize(new Dimension(600, 600));
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(this);
          addKeyListener(new MyKeyListener());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          new KeyListenerDemo().start();
       }
    
       public void start() {
    
          Timer timer = new Timer(0, (event) -> moveKey()); // requires Java 8
          timer.setDelay(200);
          timer.start();
    
       }
    
       public void moveKey() {
          if (bKey) {
             System.out.println("b");
          } else if (aKey) {
             System.out.println("a");
          }
       }
    
       class MyKeyListener extends KeyAdapter {
          public void keyPressed(KeyEvent ke) {
             char key = ke.getKeyChar();
             if (key == 'a') {
                aKey = true;
             } else if (key == 'b') {
                bKey=true;
             }
          }
    
          public void keyReleased(KeyEvent ke) {
             char key = ke.getKeyChar();
             if (key == 'a') {
                aKey = false;
             } else if (key == 'b') {
                bKey=false;
             }
          }
       }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How do you skip to a certain line of code?

    Here's my penny's worth...:)

    You need to define your rules for keys, but I'm going to try and work with what you've given.
    You want to "move" using whatever the last direction held down is, so if I'm holding down "up" and then click "down" then down takes precedence until it is released. Presumably this applies vice-versa. I'm also going to assume this applies if I hold "up" then "right", then right takes precedence as it's the last key. This also continues if you manage to hold three or four direction keys.

    Java Code:
    public enum Direction {
        UP(0,1), DOWN(0,-1),LEFT(-1,0),RIGHT(1,0);
    
        private int horizontal;
        private int vertical;
        
        Direction(int horizontal, int vertical) {
            // Set vals.
        }
        // Add getters or probably a move method with a Point.
    }
    When someone presses a key then the relevant enum should be inserted into the front of a List or Set or something, but it needs to handle pushing and popping from the head.
    When a key is released then remove it from that collection/queue/thingamajig.
    When checking for movement then just use the first one in the collection/queue/thingamajig.

    ETA: actually, it just needs handling inserting into the head. Popping is irrelevant. Sets won't work, as you want it to be based on clicks, not any sort of natural ordering. So a simple List.
    Last edited by Tolls; 12-23-2015 at 09:37 AM.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Aboogesnickle is offline Member
    Join Date
    Jun 2014
    Posts
    37
    Rep Power
    0

    Default Re: How do you skip to a certain line of code?

    That is close to what I am talking about but if up and right are pressed at the same time I want it to go up and right. I only want the opposite directions to override one another.

    I think I can use your enum idea tho to get it working the way I need it to. Ill post back if I do.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How do you skip to a certain line of code?

    want the opposite directions to override one another.
    What does that mean?
    If Down is pressed (and held) and later Up is pressed -> What should happen?
    Then if Up is released (and Down is still pressed) -> what happens?
    Then if Down is release (now nothing is pressed) -> What happens?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How do you skip to a certain line of code?

    Yeah. I had thought about that too (and using a Stack<Enum> or even a Set<Enum>). I didn't even consider showing a way of keeping precedence of the keys based on first pressed because I didn't want to over complicate things (or totally solve the OPs problem). So I settled on introducing key listeners and constraining the key priority a bit.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How do you skip to a certain line of code?

    Quote Originally Posted by Norm View Post
    What does that mean?
    I think I can answer these.
    (If nothing else it will help me confirm what I think is going on).

    Quote Originally Posted by Norm View Post
    If Down is pressed (and held) and later Up is pressed -> What should happen?
    It should go up.
    Quote Originally Posted by Norm View Post
    Then if Up is released (and Down is still pressed) -> what happens?
    It should go down.
    Quote Originally Posted by Norm View Post
    Then if Down is release (now nothing is pressed) -> What happens?
    It should stop.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How do you skip to a certain line of code?

    Yep! That was my take too. Of course, it all depends on which one is pressed first. And gets more complicated when you add lateral motion keys.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How do you skip to a certain line of code?

    The only complexity with lateral motion is they become cumulative.

    Using the enums above with two lists (or somesuch), get current horizontal from one, get vertical from the other, pass in current position into the move() on each.
    So:
    Java Code:
    pseudo:
    horizontalList = [DOWN, UP];
    verticalList = [RIGHT, LEFT];
    would result in DOWN and RIGHT being used, based on my description above.
    Java Code:
    pseudo:
    horizontalList = [empty];
    verticalList = [LEFT, RIGHT];
    would result in LEFT only.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How do you skip to a certain line of code?

    I was looking for the OPs thoughts on how his app should work.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How do you skip to a certain line of code?

    But I believe you have to adjust the lists (perhaps by rotation) to alter priority.

    Here, DOWN has priority.

    Press and hold DOWN- move DOWN.
    Now press and hold left - move LEFT.
    Release LEFT - DOWN continues.

    Or

    Press and hold DOWN- move DOWN.
    Now press and hold left - move LEFT.
    Release DOWN - continue moving LEFT - LEFT now has priority.
    Press and hold DOWN- move DOWN.
    Release DOWN - continue moving LEFT.

    Or

    Press and hold DOWN, then hold LEFT, then hold RIGHT. Priorities change based on which the order of pressing and releasing. The OP didn't
    give this as an example, I simply extrapolated as a possible requirement. And the degree of complexity is inversely proportional to the experience
    of the programmer. :)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How do you skip to a certain line of code?

    If a horizontal and a vertical are held at the same time then both apply, so DOWN and LEFT would result in moving DOWN and LEFT.

    See post #8 ("I only want the opposite directions to override one another.")

    Anyway, hopefully they'll come back with their solution...and I can see if my guesswork is correct.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    Aboogesnickle is offline Member
    Join Date
    Jun 2014
    Posts
    37
    Rep Power
    0

    Default Re: How do you skip to a certain line of code?

    Quote Originally Posted by Norm View Post
    What does that mean?
    If Down is pressed (and held) and later Up is pressed -> What should happen?
    Then if Up is released (and Down is still pressed) -> what happens?
    Then if Down is release (now nothing is pressed) -> What happens?
    Question
    #1 Should start moving up
    #2 Should resume going down
    #3 Should stop moving

    and the same should be true if you swap up for down and vice versa
    should do same for left and right

    the vertical should be separate from horizontal so if left and down were pressed should move down and left diagonally

    The problem come when trying to get it to work in both directions
    Last edited by Aboogesnickle; 12-23-2015 at 04:52 PM.

  18. #18
    Aboogesnickle is offline Member
    Join Date
    Jun 2014
    Posts
    37
    Rep Power
    0

    Default Re: How do you skip to a certain line of code?

    Btw I am using Libgdx with Box2D physics engine and there is a isJustPressed() method that returns true one time when the key is pressed then false until you release it and repress key. I am trying to see if using that with a boolean would help but I have not been able to come up with anything.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How do you skip to a certain line of code?

    The problem come when trying to get it to work in both directions
    What does "both directions" mean? There are 4 directions: up/down/left/right
    Can you give an example of "the problem"?
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How do you skip to a certain line of code?

    Quote Originally Posted by Tolls View Post
    If a horizontal and a vertical are held at the same time then both apply, so DOWN and LEFT would result in moving DOWN and LEFT.
    You were correct!! Diagonal motion would result in that case (according to a post by the OP).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Page 1 of 2 12 LastLast

Similar Threads

  1. Line by line code performance analysis
    By gdrsantosh in forum Advanced Java
    Replies: 5
    Last Post: 10-22-2014, 11:35 AM
  2. skip first line when reading a csv
    By j0rdan in forum New To Java
    Replies: 3
    Last Post: 08-29-2012, 09:46 AM
  3. skip a line between 2 system.out.println messages
    By aconti in forum New To Java
    Replies: 5
    Last Post: 05-30-2011, 11:27 AM
  4. Constructors pls explan the code line by line in comments
    By vibaviattigala in forum New To Java
    Replies: 1
    Last Post: 02-19-2011, 04:03 AM

Tags for this Thread

Posting Permissions

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