0
function ProcessCollisions():void{

//respawn if player died
if(percentHP == 0){
    Player.x = _startMarker.x;
    Player.y = _startMarker.y;
    Gun.x = Player.x;
    Gun.y = Player.y - 45;
    _boundaries.x = 0;
    _boundaries.y = 0;
    Player.vy = 0;
    currentHP = maxHP;
}
//when Player is Falling
if(Player.vy > 0){
    //Otherwise process collisions of boundaires
        var collision:Boolean = false;
        if(_boundaries.hitTestPoint(Player.x, Player.y, true)){collision = true;}
        if(collision == true){
            while(collision == true){
                Player.y -= 0.1;
                Gun.y -= 0.1;
                collision = false;
                if(_boundaries.hitTestPoint(Player.x, Player.y, true)){collision = true;}
            }
            trace("Collision is " + collision);
            currentHP -= Player.vy * .1
            Player.vy = 0;
            Gun.vy = 0;
            JumpCount = 0;
        }
}

}

My Collision processing is above...I have this issue where Collision boolean is constantly returned as false even when I am colliding with an object within _boundaries. It creates a shaking effect onto my player as the while loop constantly pulls the player out of the boundary and the player seemingly falls into it infinitely...Can anyone help with this? Also in a Frame handler called by an Enter Frame Timer I make some more changes to Player.vy, that is included below

function FrameHandler(e:Event):void{
    //If Statements to slow character
    if(aKeyPressed == false && Player.vx > 0){
        Player.vx -= 1
    }
    else if(dKeyPressed == false && Player.vx < 0){
        Player.vx += 1
    }
    //Gravitates Player
     Player.vy += 1.5;
    //Controls arrays of bullets for Weapons
    //Process Collisions
    ProcessCollisions();
    //Scroll Stage
    scrollStage();
    //Attunes for multiple keypresses
    MultipleKeypresses();
    //Keeps Gun attached to Players Arm
    Gun.x = Player.x
    Gun.y = Player.y - 45;
    if(sKeyPressed){
        Gun.y = Player.y - 13;
        Gun.x = Player.x + 8;
    }
    //Checks Health
    updateHealthBar()
    //move Player and Gun
    Gun.y += Player.vy;
    Gun.x += Player.vx;
    Player.x += Player.vx;
    Player.y += Player.vy;

}
Cory
  • 71
  • 7
  • what is _boundaries, where is it's size setup? also if it's shaking the character it must be hitting the collision=true condition at some point otherwise it would never get into the loop that "corrects" the players position – shaunhusain May 10 '12 at 22:45
  • Okay, makes sense so collision is indeed returning true...Any idea how to stop the "shaking"? _boundaries is a movieclip that is about twice the size of the stage containing various objects, that comprise of my level design – Cory May 10 '12 at 23:02

1 Answers1

1

(1) trace("Collision is " + collision); is only executed once you've left your while(collision == true) loop, so you will ONLY ever see "Collision is false" on your trace.

(2) When you detect a collision, Player.y -= 0.1; moves the player upward in steps of 0.1, so you'll never get further than 0.1 away from the thing you collided with.

Then, having set Player.vy=0, it gets reset in the next frame to 1.5 (by Player.vy += 1.5;), which increments Player.y by 1.5... which will always result in another collision!

EDIT: The way to fix this depends on what effect you want: if you want the player to bounce off the object like a trampoline, you can replace Player.vy = 0; with Player.vy = -Player.vy;.

Alternatively, to make the player land on the object and stay there, you can test for a potential collision before updating Player.y. Something like this:

//move Player and Gun, if we're not about to collide
if(_boundaries.hitTestPoint((Player.x+Player.vx), (Player.y + Player.vy), true)==false){
    Player.y += Player.vy;
    Gun.y += Player.vy;
    Player.x += Player.vx;
    Gun.x += Player.vx;
} else {
    // we're about to collide: stop moving
    Player.vy = 0;
    Player.vx = 0;
}

This isn't a perfect solution: it may make the player land some distance from the object, depending on vy and vx, so a refinement would be to work out the exact point of impact, and move there before stopping.

Richard Inglis
  • 5,888
  • 2
  • 33
  • 37
  • Thanks Richard. That makes sense. Though is there a better way for me to sort of, turn off gravity while Colliding? Is it as simple as setting vy = 0 in the while loop? Sorry if this seems rudimentary, Im a first year programmer. – Cory May 10 '12 at 23:01
  • Okay, I want him to land and stay on top of the objects, like someone landing on the ground...I moved the if statement in front of the Player.y -= .1, but now the "shaking effect" is slowed. I think it's because of gravity constantly pulling the character into the object and then that while loop constantly fixes, is there a way to make that less noticeable or to eliminate Gravity all together while im on an object? Shouldn't the if(collision == true) statement and Player.vy = 0; make my gravity zero when in contact with objects? Again, thanks for the help – Cory May 10 '12 at 23:08
  • Cory, I've added a suggestion above. – Richard Inglis May 10 '12 at 23:23
  • Okay, that solution seems to work, but after a few seconds my player falls straight through the _boundaries? Any ideas? – Cory May 10 '12 at 23:28
  • Thanks. Though when I implement that the player movement is choppy because for some reason, my player is still actually falling into objects, which triggers that while loop to pull it out, and since for a time it is inside an object it is obviously going to collide. Also to clarify, I want my player to land on the object and cease to fall into the object, still have movement in x direction but no movement in y, unless jumping then to have gravity pulling down on it. – Cory May 10 '12 at 23:37
  • It would help to see what your level looks like... have you got a screenshot? – Richard Inglis May 10 '12 at 23:41
  • Would it be easiest just to send you the source code? That way you can see all the variables and whatnot that could be affecting it? – Cory May 10 '12 at 23:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11132/discussion-between-cory-and-richard-inglis) – Cory May 10 '12 at 23:46