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;
}
(1)
trace("Collision is " + collision);is only executed once you’ve left yourwhile(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 (byPlayer.vy += 1.5;), which incrementsPlayer.yby 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;withPlayer.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:This isn’t a perfect solution: it may make the player land some distance from the object, depending on
vyandvx, so a refinement would be to work out the exact point of impact, and move there before stopping.