kodefoo
July 24th, 2007, 09:07 AM
hello!
i am having some problems with movement. I am trying to use classes and stuff but i am not able to get the smooth motion that i want. Plus i cannot get my character to run, then jump in the direction he is moving. Pretty much my key listener is only listening to 1 key at a time, and stops listening to my other keys that are still down if a new key is pressed.
i just have 2 movie clips on my root timeline. the "ground" and the "hero". If i need to i can attach the fla.
here is my class:
class heroClass {
var heroClip:MovieClip;
var originalY:Number;
var watchKeyboard:Object;
var jumpingInterval:Number;
var fallingInterval:Number;
var jump:Number;
var speed:Number;
var maxSpeed:Number;
var ground:MovieClip;
public function heroClass(baseClip:MovieClip){
heroClip = baseClip;
ground = _root.ground;
// jumping vars
heroClip.jumping = false;
heroClip.falling = false;
jump = 0;
// movement vars
speed = 0;
maxSpeed =15;
watchKeyboard = new Object();
watchKeyboard["actionObject"] = this;
watchKeyboard.onKeyDown = function(){
if(Key.isDown(Key.SPACE)){
this["actionObject"].heroJump();
}
if(Key.isDown(Key.RIGHT)){
this["actionObject"].heroRight();
}
if(Key.isDown(Key.LEFT)){
this["actionObject"].heroLeft();
}
}
watchKeyboard.onKeyUp = function(){
if(Key.getCode() == 37 || Key.getCode() == 39){
// 37 = left
// 39 = right
this["actionObject"].speed = 0;
this["actionObject"].updateText();
}
}
Key.addListener(watchKeyboard);
}
public function heroJump(){
if(jump == 0){
if(!heroClip.jumping && !heroClip.falling){
heroClip.jumping = true;
}
if(heroClip.jumping){
jump++;
originalY = heroClip._y;
jumpingInterval = setInterval(jumpingAnimator,5,heroClip,this);
}
}
}
public function heroRight(){
if(speed<maxSpeed){
speed+=.5;
updateText();
}
heroClip._x +=speed
ground._x -=speed;
}
public function heroLeft(){
if(speed<maxSpeed){
speed+=.5;
updateText();
}
heroClip._x -=speed
ground._x +=speed;
}
//
// supporting functions
//
private function jumpingAnimator(mc:MovieClip,hero:heroClass){
var peak:Number = hero.originalY - 150;
if(mc._y <(peak+25)){
mc._y -= 1;
} else {
mc._y -= 3;
}
if(mc._y <=peak){
mc.jumping = false;
mc.falling = true;
clearInterval(hero.jumpingInterval);
hero.fallingInterval = setInterval(hero.fallingAnimator,5,hero.heroClip,h ero);
}
}
private function fallingAnimator(mc:MovieClip,hero:heroClass){
var peak:Number = hero.originalY - 150;
if(mc._y <(peak+25)){
mc._y += 1;
} else {
mc._y += 3;
}
if(mc.hitTest(_root.ground)){
mc._y = _root.ground._y;
mc.falling = false;
hero.jump = 0;
clearInterval(hero.fallingInterval);
}
if(mc._y > Stage.height){
trace("you are dead");
}
}
private function updateText(){
_root.speed_txt.text = speed;
}
}
i am having some problems with movement. I am trying to use classes and stuff but i am not able to get the smooth motion that i want. Plus i cannot get my character to run, then jump in the direction he is moving. Pretty much my key listener is only listening to 1 key at a time, and stops listening to my other keys that are still down if a new key is pressed.
i just have 2 movie clips on my root timeline. the "ground" and the "hero". If i need to i can attach the fla.
here is my class:
class heroClass {
var heroClip:MovieClip;
var originalY:Number;
var watchKeyboard:Object;
var jumpingInterval:Number;
var fallingInterval:Number;
var jump:Number;
var speed:Number;
var maxSpeed:Number;
var ground:MovieClip;
public function heroClass(baseClip:MovieClip){
heroClip = baseClip;
ground = _root.ground;
// jumping vars
heroClip.jumping = false;
heroClip.falling = false;
jump = 0;
// movement vars
speed = 0;
maxSpeed =15;
watchKeyboard = new Object();
watchKeyboard["actionObject"] = this;
watchKeyboard.onKeyDown = function(){
if(Key.isDown(Key.SPACE)){
this["actionObject"].heroJump();
}
if(Key.isDown(Key.RIGHT)){
this["actionObject"].heroRight();
}
if(Key.isDown(Key.LEFT)){
this["actionObject"].heroLeft();
}
}
watchKeyboard.onKeyUp = function(){
if(Key.getCode() == 37 || Key.getCode() == 39){
// 37 = left
// 39 = right
this["actionObject"].speed = 0;
this["actionObject"].updateText();
}
}
Key.addListener(watchKeyboard);
}
public function heroJump(){
if(jump == 0){
if(!heroClip.jumping && !heroClip.falling){
heroClip.jumping = true;
}
if(heroClip.jumping){
jump++;
originalY = heroClip._y;
jumpingInterval = setInterval(jumpingAnimator,5,heroClip,this);
}
}
}
public function heroRight(){
if(speed<maxSpeed){
speed+=.5;
updateText();
}
heroClip._x +=speed
ground._x -=speed;
}
public function heroLeft(){
if(speed<maxSpeed){
speed+=.5;
updateText();
}
heroClip._x -=speed
ground._x +=speed;
}
//
// supporting functions
//
private function jumpingAnimator(mc:MovieClip,hero:heroClass){
var peak:Number = hero.originalY - 150;
if(mc._y <(peak+25)){
mc._y -= 1;
} else {
mc._y -= 3;
}
if(mc._y <=peak){
mc.jumping = false;
mc.falling = true;
clearInterval(hero.jumpingInterval);
hero.fallingInterval = setInterval(hero.fallingAnimator,5,hero.heroClip,h ero);
}
}
private function fallingAnimator(mc:MovieClip,hero:heroClass){
var peak:Number = hero.originalY - 150;
if(mc._y <(peak+25)){
mc._y += 1;
} else {
mc._y += 3;
}
if(mc.hitTest(_root.ground)){
mc._y = _root.ground._y;
mc.falling = false;
hero.jump = 0;
clearInterval(hero.fallingInterval);
}
if(mc._y > Stage.height){
trace("you are dead");
}
}
private function updateText(){
_root.speed_txt.text = speed;
}
}