Wednesday 29 April 2009

Player code

The actionscript2 code that is placed on the player character is

onClipEvent (load) {
var slide:Number = 0;
//moving plaform slide
var grav:Number = 0;
// gravity
var speed:Number = 10;
// how fast you walk
var jumpHeight:Number = 15;
// how high you jump
var springHeight:Number = 30;
// how high you jump on spring
var movspringHeight:Number = 11;
// how high you jump on movingspring
var platformHeight:Number=0
//platform resistance
var dangerHeight:Number = 7;
// how high you jump when hit by danger
var slow:Number = .7;
// sets water falling speed
var fast:Number = 7;
var slowspd:Number = speed/2;
// sets water walking speed
var fastspd:Number = speed*1.5;
var setspeed:Number = speed;
var scale:Number = _xscale;
var ex:Number = 10;
// makes hitTests better, change for a closer hitTest (warning, more buggy if smalle, less real if further)
this.gotoAndStop(2);

var movingItems = new Array();
}
onClipEvent (enterFrame) {
grav++;
_y += grav;


// If not touching the ground then fall down (gravity)
while (_root.ground.hitTest(_x, _y, true)) {

_y--;
grav = 0;
}

// Test water and slow down
if (_root.water.hitTest(_x, _y, true)) {
if (grav>0) {
grav *= slow;
}
speed = slowspd;
} else {
speed = setspeed;
}

// Test fastbox and speed up
if (_root.fastbox.hitTest(_x, _y, true)) {


speed = fastspd;
}// else {
//speed = setspeed;
//}







if (Key.isDown(key.RIGHT) && !Key.isDown(88)) {
// Moving Right
_x += speed;
_xscale = scale;






if (_root.ground.hitTest(_x, _y+3, true)) {
this.gotoAndStop(1);
} else {
this.gotoAndStop(4);
}

} else if (Key.isDown(key.LEFT) && !Key.isDown(88)) {
//Moving Left
_x -= speed;
_xscale = -scale;
if (_root.ground.hitTest(_x, _y+3, true)) {
this.gotoAndStop(1);
} else {
this.gotoAndStop(4);
}
} else {
// not moving or falling
if (_root.ground.hitTest(_x, _y+3, true) && !Key.isDown(79) && !Key.isDown(73)) {
this.gotoAndStop(3);
}


//fast movement

if (Key.isDown(key.RIGHT) && Key.isDown(88)) {
// Moving Right
_x += fastspd;
_xscale = scale;

if (_root.ground.hitTest(_x, _y+3, true)) {
this.gotoAndStop(1);
} else {
this.gotoAndStop(4);
}

} else if (Key.isDown(key.LEFT)&& Key.isDown(88)) {
//Moving Left
_x -= fastspd;
_xscale = -scale;
if (_root.ground.hitTest(_x, _y+3, true)) {
this.gotoAndStop(1);
} else {
this.gotoAndStop(4);
}
} else {
// not moving or falling
if (_root.ground.hitTest(_x, _y+3, true) && !Key.isDown(79) && !Key.isDown(73)) {
this.gotoAndStop(3);
}

}


// Check moving blocks
// loop through them all
for(var i =0; i< movingItems.length; i++){

if (movingItems[i].hitTest(_x, _y+3, true)){
var offset = Math.abs(_x - movingItems[i].block._x);
_x = movingItems[i].block._x;

}
}
}

// Animations
if (Key.isDown(79) && !Key.isDown(65) && !Key.isDown(key.LEFT) && !Key.isDown(key.RIGHT) && !Key.isDown(73)) {
this.gotoAndStop(5);
}
if (Key.isDown(73) && !Key.isDown(65) && !Key.isDown(key.LEFT) && !Key.isDown(key.RIGHT) && !Key.isDown(79)) {
this.gotoAndStop(4);
}






//
if (Key.isDown(65) && _root.ground.hitTest(_x, _y+3, true)) {
// Jump
grav = -jumpHeight;
_y -= 4;
this.gotoAndStop(2);
}




if (_root.platform.hitTest(_x, _y, true)) {
// Jump
grav = -platformHeight;
_y -= 1;
this.gotoAndStop();
}

if (Key.isDown(65) && _root.platform.hitTest(_x, _y, true)) {
// Jump
grav = -jumpHeight;
_y -= 4;
this.gotoAndStop(2);
}



if (_root.spring.hitTest(_x, _y+3, true)) {
// Jump
grav = -springHeight;
_y -= 4;
this.gotoAndStop(3);
_root.spring.gotoAndPlay(2);
}


if (_root.movingspring.hitTest(_x, _y+3, true)) {
// Jump
grav = -movspringHeight;
_y -= 4;
this.gotoAndStop(4);
}



if (_root.danger.hitTest(_x, _y+3, true)) {
// Jump
grav = -dangerHeight;
_y -= 4;
_x -= 0;
this.gotoAndStop(5);
}




//switches
if (Key.isDown(88) && _root.soundbox01.hitTest(_x, _y,true)) {

loadMovie("plechide.swf",_root);
}

if (Key.isDown(88) && _root.soundbox02.hitTest(_x, _y,true)) {

loadMovie("plecnin.swf",_root);
}



// Hiting from Right
if (_root.ground.hitTest(_x+(_width/2)+ex, _y-(_height/2), true) || _root.ground.hitTest(_x+(_width/2)+ex, _y-(_height/6), true) || _root.ground.hitTest(_x+(_width/2)+ex, _y-_height, true)) {
_x -= speed;
}

// Hiting from Left
if (_root.ground.hitTest(_x-(_width/2)-ex, _y-(_height/2), true) || _root.ground.hitTest(_x-(_width/2)-ex, _y-(_height/6), true) || _root.ground.hitTest(_x-(_width/2)-ex, _y-_height, true)) {
_x += speed;
}

if (_root.ground.hitTest(_x, _y-_height-15, true)) {

grav = 1;
}

_root.gravTxt.text = grav;

}

No comments: