Wellp, I've looked everywhere, and I'm as green as they come at this..
I've recently started playing with AS3 because I want to learn it, so I began building a simple platformer deal.
I think I know what my problem is, but finding the solution has been hades and a half.
From what I can research, this is a matter of player position calculation/framerates. The character has gone too far before the program calculates his position as in relation to the floor, and when it finally recognizes where he is, it begins recalibrating, bringing him back to Zero.
My particular problem is that everyone who makes tutorials and stuff uses different labeling systems and I can't find anything well enough adapted to this particular problem in plainspeak.
So please forgive me for asking, but is there any chance someone could check out my code and help me spot where things are going wrong. I feel like it has something to do with the
if(downBumping) bit, but I'm struggling to figure out what to change and how. :\
Here's the code:
import flash.geom.Point;
import flash.events.KeyboardEvent;
import flash.events.Event;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);
var scrollX:Number = 0;
var scrollY:Number = 0;
var xSpeed:Number = 0;
var ySpeed:Number = 0;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;
var leftBumpPoint:Point = new Point(-62, 40);
var rightBumpPoint:Point = new Point(62, 40);
var upBumpPoint:Point = new Point(0, -83);
var downBumpPoint:Point = new Point(0, 80);
var animationState:String = "idle";
class Player extends player {
var speedConstant:int = 4;
var friction:Number = .75;
var maxSpeedConstant:Number = 18;
var gravityConstant:Number = 16;
var jumpConstant:Number = -180;
}
function loop(e:Event):void{
if(back.collisions.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){
trace("my butt!!");
leftBumping = true;
} else {
leftBumping = false;
}
if(back.collisions.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){
trace("my face!!");
rightBumping = true;
} else {
rightBumping = false;
}
if(back.collisions.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){
trace("my head!!");
upBumping = true;
} else {
upBumping = false;
}
if(back.collisions.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){
trace("my feet!");
downBumping = true;
} else {
downBumping = false;
}
//speed mechanics
if(leftPressed){
xSpeed -= speedConstant;
player.scaleX = -1;
} else if(rightPressed){
xSpeed += speedConstant;
player.scaleX = 1;
}
if(leftBumping){
if(xSpeed < 0){
xSpeed *= -0.5;
}
}
if(rightBumping){
if(xSpeed > 0){
xSpeed *= -0.5;
}
}
if(upBumping){
if(ySpeed < 0){
ySpeed *= -0.5;
}
}
if(downBumping){
if(ySpeed > 0){
ySpeed *= -.19;
}
if(upPressed){ //and if we're pressing UP
ySpeed = jumpConstant; //set y speed to jump constant
}
} else {//if we are not touching the floor
ySpeed += gravityConstant; //accelerate downwards
}
if(xSpeed > maxSpeedConstant){
xSpeed = maxSpeedConstant;
} else if(xSpeed < (maxSpeedConstant * -1)){
xSpeed = (maxSpeedConstant * -1);
}
xSpeed *= friction;
ySpeed *= friction;
if(Math.abs(xSpeed) < 0.5){
xSpeed = 0;
}
scrollX -= xSpeed;
scrollY -= ySpeed;
back.x = scrollX;
back.y = scrollY;
if( ( leftPressed || rightPressed || xSpeed > speedConstant || xSpeed < speedConstant *-1) && downBumping){
animationState = "walk";
}else if(downBumping){
animationState = "idle";
}
if(player.currentLabel !=animationState){
player.gotoAndStop(animationState)
}
}
function keyDownHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = true;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = true;
} else if(e.keyCode == Keyboard.UP){
upPressed = true;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = true;
}
}
function keyUpHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = false;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = false;
} else if(e.keyCode == Keyboard.UP){
upPressed = false;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = false;
}
}
Any help would be so very appreciated. I want to learn, but it's hard when I don't even know what exactly to look for.. Thanks so much!