I have created a bar where it is full on frame 1, then empty on frame 100, and I’ve made it into a shape tween. I want to create a code where when I press a certain button, it depletes the bar, so the frame goes from 1 until it reaches 100 as long as a certain button is held.
I was thinking of a way to do this, but it seems impossible.
var maxHP:int = 100;
var currentHP:int = maxHP;
var percentHP:Number = currentHP / maxHP;
I would like to have the percentHP var equal the bar, so as it gets to 50, it’ll reach frame 50, and so on.
Is this the correct way of going about this?
Thanks.
Here are the variables being used in the code I’m trying to fix below. Also, the game I’m creating has multiple characters, so the bar I’m referring to is within a couple of movieclips.
Basically, when the character double jumps, it is flying, and the bar depletes. Once the bars depletes to 0(Or when PercentHP is 0) then it should end the flying. Afterwards, I would like the bar to fill back up towards 100 overtime.
var percentHP = Number(Bar.CBar.CMana_bar.currentFrame) / Number(100);
var gravityConstant:Number = 1;
var doubleJumpReady:Boolean = false;
var flyingJump:Boolean = false;
var upReleasedInAir:Boolean = false;
function loop(e:Event):void{
//Flying
if(upReleasedInAir == true){ // if the player releases the up arrow key
upReleasedInAir = false; // set the variable to true
}
if(doubleJumpReady == false){
doubleJumpReady = true;
}
} else { //if we are not touching the floor
ySpeed += gravityConstant; //accelerate downwards
//Flying
if(upPressed == false && upReleasedInAir == false){
upReleasedInAir = true;
//trace("upReleasedInAir");
}
if(doubleJumpReady && upReleasedInAir){
if(upPressed){ //and if the up arrow is pressed
flyingJump = true;
//trace("doubleJump!");
doubleJumpReady = false;
ySpeed = jumpConstant + 8; //set the y speed to the jump constant
gravityConstant = 0;
Bar.CBar.CMana_bar.play();
//trace(percentMP);
}
}
if(Bar.CBar.CMana_bar.currentFrame == 100 && percentHP == 0){
gravityConstant = 1;
Bar.CBar.CMana_bar.stop();
flyingJump = false;
doubleJumpReady = false;
}
What you should do is attach a MOUSE_DOWN and MOUSE_UP event to your button. Type in
stop();in frame 1 of your bar and then use MOUSE_DOWN and MOUSE_UP to play or stop the animation in your bar. Something along the lines of: