Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var player = 1;
- var gravity = 2;
- var speed = 10;
- var plrY = new Array(0, 0, 0);
- var plrDir = new Array(0, 0, 0);
- var plrIsFalling = new Array(0, 1, 1);
- var KeyListener = new Object();
- Key.addListener(KeyListener);
- //Key controls
- KeyListener.onKeyDown = function()
- {
- //Jump
- if (Key.isDown(87))
- {
- //if the player isnt already jumping
- if (plrIsFalling[player] == false)
- {
- //player is now jumping
- plrIsFalling[player] = true;
- //sets base speed to launch into air
- plrY[player] = -20;
- //moves off of the surface to avoid collision issues
- _root["plr" + player]._y--;
- }
- }
- //Left
- if (Key.isDown(65))
- {
- plrDir[player] = -1;
- }
- //Right
- if (Key.isDown(68))
- {
- plrDir[player] = 1;
- }
- //Player swap button
- if(Key.isDown(81)){
- if(player == 1){
- player = 2
- }else{
- player = 1
- }
- }
- };
- KeyListener.onKeyUp = function()
- {
- //These just reset the players direction when
- //the button is lifted up. Reversed to remove
- //priority glitch
- if (Key.isDown(68))
- {
- plrDir[player] = 1;
- }
- else if (Key.isDown(65))
- {
- plrDir[player] = -1;
- }
- else
- {
- plrDir[player] = 0;
- }
- };
- //Moves the currently controlled player and runs basic collision control
- movePlr = function ()
- {
- //Moves currently enabled player
- _root["plr" + player]._x += plrDir[player] * speed;
- //Checks edge and wall collisions for each player
- checkEdge(plr1, 1)
- checkWall(plr1, 1)
- checkEdge(plr2, 2)
- checkWall(plr2, 2)
- };
- //Checks if the item has fallen off the edge of a platform
- checkEdge = function(item, ID){
- if(item.hitF(bottom) == false){
- plrIsFalling[ID] = true
- }
- }
- //Checks if item has ran into a wall
- checkWall = function(item, ID){
- //Its dependent on the direction the item is currently moving
- switch(plrDir[ID]){
- case -1:
- if(item.hitF(left)){
- //Use another function to allow recursion
- shiftPlr(item, -1);
- }
- break;
- case 1:
- if(item.hitF(right)){
- shiftPlr(item, 1);
- }
- }
- }
- //Shifts player if they're colliding with the wall
- shiftPlr = function(item, dir){
- //Moves them in the opposite direction to the way that they're walking
- item._x += dir * -1
- switch(dir){
- case -1:
- //If its still colliding
- if(item.hitF(left)){
- //RECUUURRRRSSSIIIOOOOON
- shiftPlr(item, -1);
- //if not
- }else{
- //shift it back by one so they dont get stuck
- item._x += dir * -1
- }
- break;
- case 1:
- if(item.hitF(right)){
- shiftPlr(item, 1);
- }else{
- item._x += dir
- }
- break;
- }
- }
- //Deals with vertical movement and collisions
- falling = function ()
- {
- if (plrIsFalling[1] == true)
- {
- //Basic collision check
- if (plr1.hitF(bottom) == false)
- {
- //If it hasnt hit the ground, increase the velocity and move
- plrY[1] += gravity;
- plr1._y += plrY[1];
- }
- //Not using else statement as i want to check if its hit the bottom
- //before performing the next incriment as if i dont, the graphics
- //bug the fuck out and it looks ugly
- if (plr1.hitF(bottom) == true)
- {
- //resets the variables
- plrY[1] = 0;
- plrIsFalling[1] = false;
- //Starts recursion to remove embedded player
- loopTillFloating(plr1);
- }
- }
- if (plrIsFalling[2] == true)
- {
- if (plr2.hitF(bottom) == false)
- {
- plrY[2] += gravity;
- plr2._y += plrY[2];
- }
- if (plr2.hitF(bottom) == true)
- {
- plrY[2] = 0;
- plrIsFalling[2] = false;
- loopTillFloating(plr2);
- }
- }
- };
- //Removes the item from any horizontal platforms
- loopTillFloating = function (itemID)
- {
- var item:MovieClip = itemID;
- //moves it up one
- item._y -= 1;
- //if its still imbedded
- if (item.hitF(bottom))
- {
- //do it all over again!
- loopTillFloating(itemID);
- }
- };
- //Its funny how these few lines basically make everything work
- onEnterFrame = function ()
- {
- //Run teh algorithms
- movePlr();
- falling();
- };
Add Comment
Please, Sign In to add comment