PDA

View Full Version : limited character flight with actionscript



felixirDesign
February 4th, 2009, 05:51 PM
Hi all, i am creating a flash project that uses a character with limited flight. the idea is that the character has a set amount of time that they can fly for, say 3 seconds and then they run out of power and the power will recharge once they let go of the button but will re-charge at a slower rate than used but i have run into trouble. if anyone can point me in the right direction that would be great. here is my code

var yspeed = 0;
var xspeed = 0;
var gravity = 0.6;
var rotationspeed = 0;
var power = 1.6;
var flightPower = 1.6;
var friction = 0.9;
var flightTime = 3;
var curFlightTime= 3;
onEnterFrame = function() {
yspeed += gravity;
xspeed *= friction;
this._x += xspeed;
rotationspeed *= friction;
this._rotation = 0+rotationspeed;
if (this._y<=400-this._height/2) {
this._y += yspeed;
if (Key.isDown(Key.LEFT)) {
xspeed -= power;
rotationspeed += power*xspeed/2;
} else if (Key.isDown(Key.RIGHT)) {
xspeed += power;
rotationspeed += power*xspeed/2;
}
} else {
xspeed *= friction;
yspeed = 0;
this._y = 400-this._height/2;
}

//trouble starts here
if (Key.isDown(Key.UP)) {

yspeed -= flightPower;
curFlightTime=flightTime--;
countDown = function () {
flightTime--;
if (flightTime == 0) {
flightPower = 0.2;
}

}
timer = setInterval(countDown, 500);

}else{
powerBack = function (){
flightTime = flightTime +0.1;
flightPower= 1.6;
if (flightTime >=3){
flightTime=3;
}
}
timer = setInterval(powerBack, 500)
}
//and ends here
if (yspeed<0) {
yspeed *= friction;
}
}

crucifix
February 5th, 2009, 02:17 AM
Try this instead of the plain else statement
}else if(!Key.isDown(Key.UP)){
powerBack = function (){
flightTime = flightTime +0.1;
flightPower= 1.6;
if (flightTime >=3){
flightTime=3;
}
}

felixirDesign
February 5th, 2009, 08:45 AM
still no use, no the guy either flys continously and nothing stops him or he flys till the timer runs out then cannot ever get off the ground again, would anyone advise using 2 variables? such as current flight time and max flight time? i have been using bars to show the flight time so that they become smaller using their _width property to visually see how the flightTime is affected and the minute i release the up button all hell breaks loose

crucifix
February 6th, 2009, 10:21 PM
You need to watch the timers. you are resetting wrong.