PLATFORMER:Change behavour when overlap to Swim

Get help using Construct 2

Post » Fri May 17, 2013 10:42 am

This is a pretty old topic, but it's on the FAQ page and States are important, so...

I make a Multi-State object by giving the object an Instance Variable which I call "State", which I set to a String. Make the default something like 'Walk' or 'Move'
For your purposes, add both Platform and 8-Direction, but disable the 8-Direction to start.

On the event sheet where you handle controlling your player, begin with the event "Compare Instance Variable" and see if it reads "Walk" Put all walking, running, standing on ground behaviors there. For your purposes, there you would use 'Set Enabled' to Enable Platform and Disable 8-Direction

Next, when your player makes contact with the water, set 'State' to "Swim" and set it back to "Walk" when the player is not in contact with water (I think the 'Overlap' function would work there)

Then, same thing, do a 'Compare Instance Variable' to see if State is "Swim", disable Platform and enable 8-Direction, then put in all your Swimming Behavior (such as changing animations to swimming, any swim-specific controls, even adding an Air Meter to the HUD if you'd like)

It may seem a bit complicated, but that system (based off a Finite State Machine, if you want to look that up) is extremely powerful and makes for fairly clean code once you get used to it. You can use that same 'State' variable for climbing ladders, flinching on taking damage, and anything else that temporarily changes the behavior of your character.

Hope that helps somebody!
Posts: 4
Reputation: 997

Post » Sun May 19, 2013 12:15 am

psst! Hey, it would be even better if you add a actual example for the folks you know ;)
Posts: 229
Reputation: 7,287

Post » Sun May 19, 2013 1:11 am

I just use the platform behavior to swim.
I have a boolean called Swimming that sets to True when I'm overlapping water, and False when I'm not. When Swimming is True, I change gravity to 0 and mess with the Platform Behavior's vector-y. Left and right movement works the same as not in water, so I don't have to change that.

One key thing I do is that I NEVER use the "jump" action at all in my game, I always just "set vector-y" to something (same effect as pressing jump). So when I'm in water and gravity is 0, pressing up sets vector-y to -250 and pressing down sets it to 250.

Then I have one more event that triggers every 0.01 seconds that NEITHER up nor down are pressed in water: If vector-y is greater than zero, subtract 50, and if it's less than zero, add 50. That way, my character will slow down and stop when I let go of up or down.

This method prevents me from having to add two movement behaviors to my character; I just don't like that.

As an added bonus, I have a sprite indicating the top of the water. If my character is overlapping that and up is pressed, I set vector-y to -600 so that she pops out of the water. It's not necessary but I think it adds a really nice little touch.

Here's a screenshot of the event sheet:
Posts: 95
Reputation: 2,661

Post » Tue Nov 19, 2013 7:00 pm

i just had to say thank you!
Posts: 277
Reputation: 3,948


Return to How do I....?

Who is online

Users browsing this forum: Taweel, Tigeri102 and 1 guest