I'm working on a flash game with a main character who helps people around town. It's moveable with the arrow keys because of a function called keyPressed();
When the main character hitTests with a person and you press spacebar the person should gotoAndPlay a label within itself. This works great, but because I've put these if statements in the keyPressed function, everytime the main character moves (keyPressed) the label plays from the beginning. I understand why this is happening but my question to you is:
It went great so far but now I realize I have a lot of if statements and they all are in the keyPressed function. This causes my problem (the looping of the gotoAndPlay) and I'm pretty sure this will cause more trouble in the future. I should use more functions, but where and when do I use these to accomplish my goal?
More info: I'm coding on the timeline (F9), maincharacter = char, client = the person wich label should go to 'leaving'
var clients:int = 0;
var orders:int = 0;
var delivery:int = 0;
var hodling:int = 0;
var isRight:Boolean=false;
var isLeft:Boolean=false;
var isUp:Boolean=false;
var isSpace:Boolean=false;
var isDown:Boolean=false;
var speed:Number = 3;
var mazeRect:Rectangle = bounds.getBounds(this);
var charRect:Rectangle = char.getBounds(this);
var boundsBmpData = new BitmapData(mazeRect.width, mazeRect.height, true, 0);
var charBmpData = new BitmapData(charRect.width, charRect.height, true, 0);
boundsBmpData.draw(bounds);
charBmpData.draw(char);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, moving);
function keyPressed(event:KeyboardEvent):void
{
if(event.keyCode==39){
isRight=true
char.gotoAndStop('right')}
if(event.keyCode==37){
isLeft=true
char.gotoAndStop('left')}
if(event.keyCode==38){
isUp=true
char.gotoAndStop('up')}
if(event.keyCode==40){
isDown=true
char.gotoAndStop('down')}
if(event.keyCode==32){
isSpace=true
}
if(char.hitTestObject(order) && isSpace == true && orders <3)
{
orders++ ;
}
if(char.hitTestObject(orderz) && isSpace == true && orders > 0 && holding <3 )
{ orders--;
holding++;}
if(char.hitTestObject(order2) && isSpace == true && orders > 0)
{ orders--;
holding++;}
if(char.hitTestObject(order3) && isSpace == true && orders > 0)
{ orders--;
holding++;}
if (char.hitTestObject(client) && client.currentLabel == 'seated' && isSpace == true && holding>0){
delivery++;
}
if( char.hitTestObject(order) && isSpace == true && orders == 1){
orderz.gotoAndPlay('one')
}
if( char.hitTestObject(order) && isSpace == true && orders == 2){
order2.gotoAndPlay('two')
}
if( char.hitTestObject(order) && isSpace == true && orders == 3){
order3.gotoAndPlay('three')
}
if( char.hitTestObject(orderz) && isSpace == true){
orderz.gotoAndPlay('zero')
}
if( char.hitTestObject(order2) && isSpace == true ){
order2.gotoAndPlay('zero')
}
if( char.hitTestObject(order3) && isSpace == true ){
order3.gotoAndPlay('zero')
}
if (counterDisplay.text == ('3') && delivery == 3)
{
served ++;
//trace("ready");
client.gotoAndPlay('leaving');
}
if (counterDisplay.text == ('2') && delivery == 2 )
{
served ++;
//trace("ready");
client.gotoAndPlay('leaving');
}
if (counterDisplay.text == ('1') && delivery == 1 )
{
served ++;
client.gotoAndPlay('leaving');
}
if ( served == 1)
{
trace("ready");
}
}
function keyReleased(event:KeyboardEvent)
{
if(event.keyCode==39){
isRight=false}
if(event.keyCode==37){
isLeft=false}
if(event.keyCode==38){
isUp=false}
if(event.keyCode==40){
isDown=false}
if(event.keyCode==32){
isSpace=false}
}
function moving(e: Event): void
{
var newx: Number = char.x - (isLeft ? speed : 0) + (isRight ? speed : 0);
var newy: Number = char.y - (isUp ? speed : 0) + (isDown ? speed : 0);
if(!boundsBmpData.hitTest(new Point(bounds.x, bounds.y),
255,
charBmpData,
new Point(newx, newy),
255))
{
char.x = newx;
char.y = newy;
}
}
Some additional info: the 'leaving'label is 200 frames in length and then it should stop at the next label. (the next label is not in the code yet, but the problem is that every time a key is pressed, the label 'leaving' repeats from the first frame. And the maincharacter should be movable during this animation so keys will be pressed, wich causes the label to loop/start over from the first frame.