2 Replies - 629 Views - Last Post: 09 August 2019 - 04:52 PM Rate Topic: -----

#1 tyses96   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 22-March 14

While statements nested in if statements?

Posted 09 August 2019 - 01:02 PM

So I've created a method that waits for a counter to get to a certain point, then execute the code. However I want to execute the code while another counter counts down for the same or similar amount of time, however when I've added a while statement underneath the if statement for the second counter, the intended result is not accomplished.

Its used to make characters wander around my game. Using only one counter, they do as intended and randomly walk at certain periods its just they kind of teleport and they don't go very far, however when implementing the second counter, the characters disappear completely.

The intended result is the
if(state)
happens while a second counter called
counterDown
counts down.


Here is the method:


public void wander(Animation f, Animation b, Animation l, Animation r) {
			Random rand = new Random();
			int state = rand.nextInt(1000);
			counterUp++;
			if(counterUp > enemyDelay) {
					if(state <= 250) {
							enemyAnimation = l;
							enemyX += enemySpeed;
							counterUp = 0;
					}
					else if (state > 250 && state <= 500) {
							enemyAnimation = r;
							enemyX -= enemySpeed;
							counterUp = 0;

					}
					else if (state > 500 && state <= 750) {
							enemyAnimation = b;
							enemyY += enemySpeed;
							counterUp = 0;
					}
					else if (state > 750 && state <= 1000) {
							enemyAnimation = f;
							enemyY -= enemySpeed;
							counterUp = 0;
					}
						
				}
				
			}


Any help would be appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: While statements nested in if statements?

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,899
  • Joined: 25-December 13

Re: While statements nested in if statements?

Posted 09 August 2019 - 01:56 PM

Quote

execute the code while another counter counts down

Sounds like you need a Thread if you have two processes that you want to run concurrently.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7469
  • View blog
  • Posts: 15,489
  • Joined: 16-October 07

Re: While statements nested in if statements?

Posted 09 August 2019 - 04:52 PM

As an aside, this could be simplified a little:
public void wander(Animation f, Animation b, Animation l, Animation r) {
    // not, there should be one instance of this for the entire object
    // Random rand = new Random();
    // you don't actually need this yet
    // int state = rand.nextInt(1000);
    // hmmm...
    // counterUp++;
    if(++counterUp > enemyDelay) {
        // this is true in all cases
        counterUp = 0;
        // now we want that rand
        // though 1000 seems odd
        int state = rand.nextInt(4);
        if(state == 0) {
            enemyAnimation = l;
            enemyX += enemySpeed;
        } else if (state == 1) {
            enemyAnimation = r;
            enemyX -= enemySpeed;
        } else if (state == 2) {
            enemyAnimation = b;
            enemyY += enemySpeed;
        } else { // this must be true
            enemyAnimation = f;
            enemyY -= enemySpeed;
        }
    }
}



Rather than a second counter or zeroing out a counter, you could reasonably use one counter and adjust accordingly. e.g.
if(this.counter % enemyDelay == 0) {


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1