Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
You said you know why it is looping. But we don't know why. Can you please explain exactly what is the problem? If you don't want it to loop, then what do you want it to do, and under what conditions? –  Jake May 27 '12 at 15:46
    
@Jake Thank you for your reaction, the if statements are in the keyPressed function, so everytime a key is pressed it repeats gotoAndPlay. Let me know if you understand what I mean! –  Opoe May 27 '12 at 15:48
    
The code will not gotoAndPlay unless the if condition is met. You need to tell us (or flash) the conditions you want to gotoAndPlay and the conditions when you do not want to gotoAndPlay.... we have no idea what logic effects gotoAndPlay has on your game so it's hard to tell the conditions. –  Jake May 27 '12 at 15:56
add comment

1 Answer 1

up vote 2 down vote accepted

The code only executes gotoAndPlay if the specified condition is true. You need to modify your if statements so that the condition is more strict. For example:

if(char.hitTestObject(order) && isSpace == true && orders == 1 && PREVIOUS_PLAY_IS_FINISHED){ 
     // only when previous animation of "one" finished then we play again.
     orderz.gotoAndPlay('one') 
} 

Without knowing exactly what you want to do, it's hard to tell you how to implement the stricter condition.

EDIT: You can use the MovieClip.currentFrame property to check whether the animation is still playing so that you do not repeat the animation again from start.

if(char.currentFrame == 200) // change 200 to the actual frame number, 200 from the "leaving" label.
{
    client.gotoAndPlay('leaving');
}

ALTERNATIVELY, to deal with the organisation of your general structure, you can consider making the animations of char as individual movieclips nested inside the char movieclip. Then can do things like

char.leaving.play() where leaving is the movieclip animation for "leaving" etc. then you can do things like

if(!char.leaving.isPlaying) // if not playing the leaving animation
{
   char.leaving.play();
}
share|improve this answer
    
Thank you for your answer I added some additional info about the labels –  Opoe May 27 '12 at 18:04
    
Please see edit. Not sure if that is what you want. –  Jake May 28 '12 at 8:17
    
thank you for your answer it is answer to my question, my bad because I can't explain it right. –  Opoe May 28 '12 at 14:32
    
adding the currentFrame to my if statement works! –  Opoe May 28 '12 at 18:44
    
Glad it helped. I think you can also use the MovieClip.isPlaying() method call to check if you do not want to use frames, or if it is more approperiate. –  Jake May 29 '12 at 2:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.