Another boolean issue


(Nate ) #1

So I set up some nifty cool particles today with the help of Jacob! Anyway I have some when my player jumps and I am trying to implement a nice big splash when my player hits water!

I have it all working, the issue is, once the player has entered the water, the particles continue to splash from his feet. I know it is because he is colliding with the water, I was wondering what the best way to go about making the splash stop once he is in the water would be?

Here is all of the related code from my player.as class!

if (collide("water", x, y))
		{
			onWater == true;
			
			if (onWater == true)
			{
			var b:int = 100;
			while (b --)
			emit.emit("dust", x + 22, y + 80);//emits the stars from the base of the player!
			}
		}
		
		if (!collide("water", x, y))
		{
			onWater == false;
		}

(Zachary Lewis) #2

Let’s “dive in,” so to speak.

onWater == true;

This doesn’t actually do anything. I mean, it evaluates the value of onWater with true, but that’s it. You want to use the assignment operator, =.

onWater = true;

Additionally, you’ll want to know when he is on water and when he is in water.

// Was the player in water last tick?
if (wasOnWater)
{
  // He was. Let's see if he left, or if he's still swimming around.
  if (onWater)
  {
    // The player has been in water for a while.
  }
  else
  {
    // The player just left water.
  }
}
else
{
  // The player was high and dry last tick. Did he take a dip?
  if (onWater)
  {
    // The player just entered water.
    // Splash!
    emit.emit("dust", x + 22, y + 80);
  }
  else
  {
    // The player has remained dry for sometime.
  }
}

// Save the previous state of onWater so we can use it next tick.
wasOnWater = onWater;

With a setup like this, you’ll be able to know when he entered water, when he left water, and when he’s stayed in or out.


(Nate ) #3

Okay thank you Zach! Is all of that to go inside of my collide code? Should I have onWater and wasOnWater start at false?


(Zachary Lewis) #4

It’s up to you. I’d probably have the collision check set onWater, then after the collision check run the other check. That way, you could know when to spawn bubbles (if underwater) or kick up dust (if out of water).

You should if the player doesn’t start in water. If not, you’d probably want them to be true. :tongue:


(Nate ) #5

Okay I thought so about starting with both booleans false. Would you mind writing this out in detail I can’t seem to get my head around this for now. I am sort of fried at the moment lol. In a nutshell, if my character is hitting the water, I want it to splash

var b:int = 100;
while (b --)
emit.emit("dust", x + 22, y + 80);

These three lines make a nice pretty splash. I just don’t want the splash to continue once I am under the water.


(David Williams) #6

Where he has this, put: