PDA

View Full Version : [AS3] when you repeat the space bar the faster you press it the faster it goes


loky93
08-19-2008, 05:48 PM
Hi

i need to know the code for when you repeat the space bar the faster you press it the faster it goes and the slower you press it the slower it goes and when you don't press it it goes down .

Thanks :)

QuantumTiger
08-19-2008, 11:11 PM
You could try something line this
var speed:number = 0;
var acc:number = 0.1; // set to a suitable value for your acceleration
var decay:number = 5; // set to a suitable value for deceleration
var fc:number = 0;

stage.addEventListener(KeyboardEvent.KEY_DOWN, trapKeyDown);
addEventListener(Event.ENTER_FRAME, onEnterFrame);

function trapKeyDown(ke:KeyboardEvent):void
{
if (ke.keyCode == 32) speed += acc; // trap space bar
}

function onEnterFrame(e:Event):void
{
hero.x += speed;
fc++;
if (fc % decay == 0) speed -= acc;
if (speed <=0) speed = 0;
}