Keep player in level


(Nate ) #1

Hey guys!

I have a level, player and enemy. I currently use this to keep the player inside of the level.

moveBy(xSpeed, ySpeed, ["level"]);

This keeps the player inside of the level (which is generated with OGMO)

When the player hits an enemy, I have the player ‘bumping’ away from the enemy to simulate getting struck. The problem I have, and you may have guessed, is when the player is bumped, if the player is jumping or near the edge of the level, the enemy is able to bump the player outside of the level.

I temporarily fixed this by adding all sorts of if(collide(“level” blah blah)); and it just got really confusing… So I was wondering if there is a cure all I could do in a few lines of code, to basically say, if the player is not within the confines of the level, move them to within the level.

Something like that. Thanks guys!


(Bora Kasap) #2

it’s better you wait for some real answers instead of my gameplay idea, you can use a “boolean” value about player is “bumping at the moment or not”

then you can try bumping player from “walls” & “other solids” too “if player in bumping state” at the moment.

i don’t know is that code works, but that’s the idea, i don’t know is that good for your gameplay?

if(bumping == false)
{
   moveBy(xSpeed, ySpeed, ["level"]);
}
else
{
   moveBy(xSpeed, ySpeed, ["level"]);
   if (moveCollideX != null) xSpeed *= -1;
   if (moveCollideY != null) ySpeed *= -1;
}

(Nate ) #3

In this code snippet, where would I set bumping to true?

Thanks for the reply!


(Bora Kasap) #4

i’ve no idea, it’s your code, it’s your bumper :slight_smile:


(Nate ) #5

Well I figured out with trace statements that this check is true when the player is off the level:

    if(collide("level", x, y))
    {
trace("stuck in level");
}

I just don’t know what to execute in this conditional… I tried your moveCollideX & Y in that conditional but that did not do anything.


(Justin Wolf) #6

FP.clamp() will keep an object within points. Call it after your moveBy() function.

x = FP.clamp(x, 0, levelWidth - width);
y = FP.clamp(y, 0, levelHeight - height);

Obviously, you’ll need to adjust these, as your hitbox of your player may not be set to its (0, 0) coord. If you’re using a grid system, you can easily get the level width and height by doing grid.width and grid.height.


(Nate ) #7

Okay I am using what you wrote here but not sure how to plug in my characters hit box.


(Jacob Albano) #8

Better yet:

FP.clampInRect(player, levelX, levelY, levelWidth, levelHeight, Math.max(player.width, player.height));

This clamps the player inside a given rectangle, and keeps far enough away from the edges that he won’t overlap.


(Nate ) #9

What would levelX and levelY be? I have the levelWidth and levelHeight I grab those from the xml… but not sure about player, levelX, levelY


(Nate ) #10

This is my player hitbox:

setHitbox(16, 48, -10, -16);

How would I use this with the code you posted?


(Jacob Albano) #11

levelX and levelY are probably just 0, 0. I didn’t know if you had some kind of offset to worry about. Sorry for the confusion!


(Nate ) #12

Okay, would player just be this?

EDIT: Jacob using your code, my player still gets clipped into the level D:


(Nate ) #13

Hey guys, sorry for the double post, I am still having issues with this. It seems like not matter what I do, if the player is near a wall of the level and an enemy hits the player they get launched outside of the level. I put collide checks around the enemy bumping code and that sort of worked in SOME situations, but I am looking for a solution that will keep the player from bumping outside of the level that is solid and covers all possible scenarios.

Thanks guys!


(Jacob Albano) #14

Last idea: use clampHorizontal() and clampVertical() with a large buffer, just to see if it’s working at all:

// do all your moving first

clampHorizontal(0, levelWidth, 100);
clampVertical(0, levelHeight, 100);

With this code, your player should stay a minimum of 100 pixels from the edge of the level at all times.


(Nate ) #15

Well this is the first thing I have done that seems to give me a visual reaction, however now the player can’t really walk around at all! So I am making SOME progress! Any suggestions for how I should adjust the 100’s?


(Jacob Albano) #16

Try passing your player’s height to clampVertical(), and his width to clampHorizontal().


(Nate ) #17

My character is 32x64 so how would I use clampHorizontal/Vertical correctly?


(azrafe7) #18

Sorry to chime in only now, after a long discussion… You’ve already got precious help so far but, I think that posting the relevant code in this cases should really speed up the search for a solution.

**yeah… show your code or it didn’t happen! :smile: **

From what I gather so far, your Hero is getting stuck inside a Grid after bumping against an Enemy. A couple of considerations:

  • moveBy() works wonderfully to separate an Entity based on the colliding one (assuming it isn’t moving too fast), so your Hero probably behaves correctly when interacting with the level
  • you could probably use the same technique when he collides with an Enemy

This leads me to the question:

How are you handling the bumping with the enemy? Maybe it just gets teleported, and so gets stuck within the Grid without a chance to get out of it.

If you manage to use moveBy() in that place too you shouldn’t have problems, as separation from the respective Hitboxes should happen automatically.

Not sure I’ve explained this as I wished to, but if you have any questions I’d be glad to answer them.

Again: code or just an swf to visualize the issue can help us help you! :smirk:


(Nate ) #19

Thanks for chiming in azrafe!

At the end of my Player.as Update method I use this to keep the player from leaving the level:

moveBy(xSpeed, ySpeed, ["level"]);

When the player collides with an enemy this is what I do to ‘bump’ the player:

    if (theEnemy.hittingLeft && collide("enemy", x + 1 ,y))
            {
                    health = health - 7;
                    this.x = this.x - 10;
            }
            
            if (theEnemy.hittingRight && collide("enemy", x - 1, y))
            {
                health = health - 7;
                this.x = this.x + 10;
            }

I’m sure my issue has something to do with how I am ‘bumping’ the player. Any input is greatly appreciated! Thanks guys!


(azrafe7) #20

What if you use something like:

if (theEnemy.hittingLeft && collide("enemy", x + 1 ,y))
{
     health = health - 7;
     this.moveBy(-10, 0, "level", true);
}
if (theEnemy.hittingRight && collide("enemy", x - 1, y))
{
     health = health - 7;
     this.moveBy(10, 0, "level", true);
}

?

Still… posting more of your code (/swf) could be helpful!

EDIT: Also, if the above at least partially works, you probably also want to take into consideration some edge cases, like when you’re in a corner, pressed by an Enemy, and continually hit by him. In that case one solution could be to give your Hero a temp invulnerability (f.e. think Ghosts’n’Goblins). By the way, no need to worry until you actually face the issue.

Cheers!