I’m trying to create platformer whose physics are a bit more complicated, and I’m having a bit of trouble. Here are the key sections of the code:
function checkGroundHit(e:Event):void{
//The grounded variable simply shows whether or not the player is in contact with the ground.
if (player.hitTestObject(ground) == true){
grounded = true;
} else if (player.hitTestObject(ground) == false){
grounded = false;
}
}
function setResistances(e:Event):void{
//Vertical normal reaction force
if (grounded == true){
ynResistance = gravity;
player.y = ground.y - (ground.height/2) - (player.height/2) - 10;
}else {
ynResistance = 0;
}
//Total resistance (Air resistance & friction)
xResistance = xVel * driftFactor;
//driftFactor is a const which reflects on how much I want the player to drift before reaching a stop.
}
[SUP]
Those are two functions that are called upon in an enterFrame event listener. Afterwards, the following functions update the player’s position:
[/SUP]
function setMovementValues(e:Event):void{
yRes = gravity - upForce - ynResistance;
yVel += yRes;
xRes = rightForce - leftForce - xResistance;
xVel += xRes;
}
function movePlayer(e:Event):void{
player.y += yVel;
player.x += xVel;
}
The problem I’m having is this: when the player hits the ground, they either twitch up and down before settling in the correct position or, if they are going fast enough, they’ll fall halfway through the ground and stay there, motionless.
After placing two identical trace functions for player.y before and after like so:
trace(player.y);
player.y = ground.y - (ground.height/2) - (player.height/2) - 10;
trace(player.y);
It appears that the player’s y value continuously increases (moving it downwards), before finally resetting to (player.y = ground.y - (ground.height/2) - (player.height/2) - 10;) which is a lower value.
Here is my entire code if it helps:
//Constants
const gravity:Number = 5;
const driftFactor:Number = 0.3;
const manSpeed:Number = 3;
const jumpCap:Number = 20;
//Verticals
var yRes:Number = 0;
var yVel:Number = 0;
var yResistance:Number = 0;
var ynResistance:Number = 0;
var upForce:Number = 0;
//Horizontals
var xRes:Number = 0;
var xVel:Number = 0;
var xResistance:Number = 0;
var xnResistance:Number = 0;
var rightForce:Number = 0;
var leftForce:Number = 0;
//Booleans
var grounded:Boolean;
var pressRight:Boolean;
var pressLeft:Boolean;
var pressUp:Boolean;
var pressDown:Boolean;
/////////////////////////////////////////////////////////
player.addEventListener(Event.ENTER_FRAME, main);
function main(e:Event):void{
checkGroundHit(e);
setResistances(e);
checkKeys(e);
keysHappen(e);
setMovementValues(e);
movePlayer(e);
/*if (grounded == true){
player.y = ground.y - (ground.height/2) - (player.height/2) - 10;
}*/
}
function checkGroundHit(e:Event):void{
if (player.hitTestObject(ground) == true){
grounded = true;
} else if (player.hitTestObject(ground) == false){
grounded = false;
}
}
function setResistances(e:Event):void{
//Vertical normal reaction force
if (grounded == true){
ynResistance = gravity;
player.y = ground.y - (ground.height/2) - (player.height/2) - 10;
trace(player.y);
}else {
ynResistance = 0;
}
//Total resistance (Air resistance & friction)
xResistance = xVel * driftFactor;
}
function checkKeys(e:Event):void{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keys_down);
function keys_down(e:KeyboardEvent):void{
if(e.keyCode==Keyboard.UP){
pressUp = true;
}
if(e.keyCode==Keyboard.LEFT){
pressLeft = true;
}
if(e.keyCode==Keyboard.RIGHT){
pressRight = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, keys_up);
function keys_up(e:KeyboardEvent):void{
if(e.keyCode==Keyboard.UP){
pressUp = false;
}
if(e.keyCode==Keyboard.LEFT){
pressLeft = false;
}
if(e.keyCode==Keyboard.RIGHT){
pressRight = false;
}
}
}
function keysHappen(e:Event):void{
//Press functions (true)
if(pressUp==true){
trace(grounded);
if (grounded == true){
upForce = 20;
}
}
if(pressLeft==true){
leftForce = manSpeed;
if (xVel > 0){
rightForce = 0;
}
}
if(pressRight==true){
rightForce = manSpeed;
if (xVel < 0){
leftForce = 0;
}
}
//Lift functions (false)
if(pressUp==false){
upForce = 0;
}
if(pressLeft==false){
leftForce = 0;
}
if(pressRight==false){
rightForce = 0;
}
}
function setMovementValues(e:Event):void{
yRes = gravity - upForce - ynResistance;
yVel += yRes;
xRes = rightForce - leftForce - xResistance;
xVel += xRes;
}
function movePlayer(e:Event):void{
player.y += yVel;
player.x += xVel;
}
Thank you for your help!