All pastes #514650 Raw Edit

Working Platformer Code

public text v1 · immutable
#514650 ·published 2007-05-27 20:59 UTC
rendered paste body
stop();

this.test = "";

this.speed = 15;

this.jumpVelocity = 80;
this.jumpTime = 0;
this.startAltitude = 0;
this.jumping = false;
this.a = -10;

this.bottomY = this._height;
this.bottomXMin = 0;
this.bottomXMax = this._width;

this.altitude;

this.onEnterFrame = function() {
	this._y = Math.floor(this._y);
	this._x = Math.floor(this._x);

	UpdateStats();
	UpdatePosition();
}

function UpdateStats() {
	this.altitude = _root.stageHeight - Math.floor(this._y) - Math.floor(this._height);
}

function UpdatePosition() {
	if (Key.isDown(Key.LEFT)) {
		var check = CheckLeftWall(this._x, this._x-speed);
		
		if (check.bool) {
			this._x = Math.floor(check.wallX);
		} else {
			this._x -= speed;
		}
	} if (Key.isDown(Key.RIGHT)) {
		var width = Math.floor(this._width);
		var check = CheckRightWall(this._x+width, this._x+width+speed);
		
		if (check.bool) {
			this._x = Math.floor(check.wallX) - width;
		} else {
			this._x += speed;
		}
	} if (Key.isDown(Key.SPACE) && !jumping) {
		StartJump();
	}
	
	var goingUp = (CalculateJump(jumpTime) < CalculateJump(jumpTime+1));
	if (goingUp) {
		var sh = _root.stageHeight;
		var h = Math.floor(this._height);
		
		var check = CheckCieling(sh-h-CalculateJump(jumpTime), sh-h-CalculateJump(jumpTime+1));
		if (check.bool) {
			SetAltitude(sh-h-check.cielingY);
			jumping = false;
		}
	}
	
	if (jumping) {
		jumpTime++;
		
		SetAltitude(CalculateJump(jumpTime));
		
		var check = CheckGround(CalculateJump(jumpTime), CalculateJump(jumpTime+1));
		if (check.bool) {
			jumping = false;
			SetAltitude(check.floorAlt);
		}
	} else {
		var check = CheckGround(altitude, altitude-1);
		if (!check.bool) {
			jumping = true;
			jumpTime = (-jumpVelocity)/(2*a);
			startAltitude = altitude-150;
		}
	}
}

function CheckGround(current, next) {
	var ret = {bool: false, floorAlt: 0};
	var x = Math.floor(this._x);
	
	for (var a = x; a < (this.bottomXMax+x); a++) {
		for (var b = _root.currentArena.Floors[a].Points.length-1; b >= 0; b--) {
			var floorUnderneath = _root.currentArena.Floors[a].Points[b];
			if (floorUnderneath <= current && floorUnderneath >= next) {
				ret.bool = true;
				ret.floorAlt = floorUnderneath;
				break;
			}
		}
	}
	
	return ret;
}

function CheckCieling(current, next) {
	var ret = {bool: false, cielingY: 0}
	var x = Math.floor(this._x);
	var y = Math.floor(this._y);
	var height = Math.floor(this._height);
	
	for (var a = x; a < this.bottomXMax+x; a++) {
		for (var b = 0; b < _root.currentArena.Cielings[a].Points.length; b++) {
			var cieling = _root.currentArena.Cielings[a].Points[b];
			if (cieling >= next && cieling <= current) {
				ret.bool = true;
				ret.cielingY = cieling;
			}
		}
	}
	
	return ret;
}

function CheckLeftWall(current, next) {
	var ret = {bool: false, wallX: 0};
	var y = Math.floor(this._y);
	var height = Math.floor(this._height);
	
	for (var a = y; a < (height+y); a++) {
		for (var b = 0; b < _root.currentArena.Walls[a].Points.length; b++) {
			var wall = _root.currentArena.Walls[a].Points[b];
			if (wall <= current && wall >= next) {
				ret.bool = true;
				ret.wallX = wall;
			}
		}
	}
	
	return ret;
}

function CheckRightWall(current, next) {
	var ret = {bool: false, wallX: 0};
	var y = Math.floor(this._y);
	var height = Math.floor(this._height);
	
	for (var a = y; a < (height+y); a++) {
		for (var b = 0; b < _root.currentArena.Walls[a].Points.length; b++) {
			var wall = _root.currentArena.Walls[a].Points[b];
			if (wall >= current && wall <= next) {
				ret.bool = true;
				ret.wallX = wall;
			}
		}
	}
	
	return ret;
}

this.SetAltitude = function(num) {
	this._y = _root.stageHeight - Math.floor(this._height) - num;
	this.altitude = _root.stageHeight - Math.floor(this._y) - Math.floor(this._height);
}

function StartJump() {
	startAltitude = altitude;
	jumping = true;
	jumpTime = 0;
}

function CalculateJump(time) {
	return a*Math.pow(time, 2) + jumpVelocity*time + startAltitude
}