PDA

View Full Version : smooth movement and jumping help needed. v_v



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;
}


}

kodefoo
July 24th, 2007, 11:00 AM
well, i was able to get this to smooth out by getting rid of the key listener and setting up a setInterval to run every 20ms.

i don't know if anyone else ever had this problem but i will post my fix snipplet for those who can benefit from it.



var keyboardInterval:Number;

keyboardInterval = setInterval(gameControls,20);

public function gameControls(hero){
var key:Number = 0;

if(Key.isDown(Key.SPACE)){

heroJump();
key++;

}
if(Key.isDown(Key.RIGHT)){

heroRight();
key++

}
if(Key.isDown(Key.LEFT)){

heroLeft();
key++

}

}

Charleh
July 24th, 2007, 11:02 AM
well, i was able to get this to smooth out by getting rid of the key listener and setting up a setInterval to run every 20ms.

i don't know if anyone else ever had this problem but i will post my fix snipplet for those who can benefit from it.



var keyboardInterval:Number;
keyboardInterval = setInterval(gameControls,20,hc);

public function gameControls(hero){
var key:Number = 0;

if(Key.isDown(Key.SPACE)){

hero.heroJump();
key++;

}
if(Key.isDown(Key.RIGHT)){

hero.heroRight();
key++

}
if(Key.isDown(Key.LEFT)){

hero.heroLeft();
key++

}
if(key==0){
hero.speed = 0;
hero.updateText();
key = 0;

}
}





My approach just involves monitoring Key.isDown every frame and acting accordingly - I pass the input along to the required hanlder object (which is my player class)

kodefoo
July 24th, 2007, 12:48 PM
My approach just involves monitoring Key.isDown every frame and acting accordingly - I pass the input along to the required hanlder object (which is my player class)


yeah, i was trying to stay away from the "onClipEvent(enterFrame)" and keep everything in my class. That way i just had one place to edit the code instead of looking through the many movie clips in my library or try and find it on the stage. But thankyou for the input.

^_^