AS2 : onEnterFrame causing problems in game

I took an existing Tic Tac Toe game in Actionscript 2, and I am trying to modify it. The modifications are mostly animation-related.

The original version of the game was only a single frame. In order to add the animations I wanted, I had to space things out, which predictably has caused problems. Let’s focus on one of those problems.

Let us try to let the computer win the game. Computer wins game > game resets > computer has first turn > everything is fine. Every time the computer makes a move, an animation plays.

On the third game, the animation plays twice. Not on the first game, not on the second game, but on the third game the animation of placing the playing piece plays twice, with seemingly 1 frame delay.

The following body of code is a section of the whole code. I believe this is what is causing the problem. I don’t like having it onEnterFrame, but I tried everything else I could think of and these ‘if statements’ wouldn’t work at all unless they were in an onEnterFrame. This function repeats continuously until the game is reset, but doesn’t cause an animation problem until the third game. tmp is the position that the computer has moved to on the board.

onEnterFrame = function () {
    if (tmp == 11) {
        _root.armcontainer1.gotoAndStop(2);
        _root.rc11.enabled = false;
        trace("tmp");
    }
 };

If I can make this code work without having it be onEnterFrame, I think that could solve the problem, but this is the only way I’ve gotten this section of code to run at all.

Here is the Flash file. https://www.mediafire.com/?7kdhj1u61h2nv8z

The code in question begins on frame 12, line 432. Any help is greatly appreciated.

Let me explain it this way:

If tmp == 11, the following code runs continuously. I want it to run only once per turn. So I’d like to use something other than onEnterFrame. However, I can’t get any other method to work.

onEnterFrame = function () {
if (tmp == 11) {
_root.armcontainer1.gotoAndStop(2);
_root.rc11.enabled = false;
trace ("tmp");
}
};

The code above works, and traces “tmp”, but runs continuously, which I don’t want.
I’ve tried other things below, which do not work:

if (tmp == 11) {
_root.armcontainer1.gotoAndStop(2);
_root.rc11.enabled = false;
trace ("tmp");
}

Does not run; “tmp” is not traced in output.

function pinkPieces() {
if (tmp == 11) {
_root.armcontainer1.gotoAndStop(2);
_root.rc11.enabled = false;
trace ("tmp");
}
};

Also Does not run; “tmp” is not traced in output.

How can I get the code to work without using onEnterFrame? That would be a good place to start.

Thanks.

Where is tmp set? Without looking at any of your other code, you may be able to call pinkPieces or just run the if statement right after tmp is set.

If your tmp is set to 11, the code will run continuously. You need to do an action to change tmp value.

Thank you for the reply.

tmp changes every time the computer moves. It can be 9 different values: 11, 12, 13, 21, 22, 23, 31, 32, 33. It corresponds to spaces on the tic tac toe board.

tmp is set in quite a few places in the code. There are 4 difficulty levels in the game, and each has different code that sets tmp. There is one function which sets tmp which runs for all 4 difficulty levels. That function is:

function pcstrategy(istowin) {
    if (level>0) {
        var str = (istowin) ? 2 : 1;
        for (n=1; n<=8; n++) {
            if ((moves[ways[n][1]] == str) && (moves[ways[n][2]] == str) && (moves[ways[n][3]] == 0)) {
                tmp = ways[n][3];
            }
            if ((moves[ways[n][1]] == str) && (moves[ways[n][3]] == str) && (moves[ways[n][2]] == 0)) {
                tmp = ways[n][2];
            }
            if ((moves[ways[n][2]] == str) && (moves[ways[n][3]] == str) && (moves[ways[n][1]] == 0)) {
                tmp = ways[n][1];
            }
        }
    if (tmp == 11) {
       trace ("works");  }
    }
}

I have tried putting an if statement in there, if (tmp == 11){ trace (“works”); } , but it does not return anything.

I have also tried calling pinkPieces, and can’t get it to work.

function pinkPieces() {
if (tmp == 11) {
//do things
trace ("tmp");
  }
};
pinkPieces;

If tmp is set to 11 in the pcstrategy function, the trace should work. This suggests that tmp is not being set to 11. Maybe trace tmp at the end of the function and see what you get.

I tried that as well and it was not working… However, I found a work-around!

onEnterFrame = function(){
    if (tmp == 11) {
        caller();
        //Play the playing-piece animation
    trace("tmp = 11");
    }
};

The variable caller is called above, then calls function pinkPieces,

var caller:Function = pinkPieces;

function pinkPieces():Void {
    tmp = '00';
    trace("tmp set to 0");
}

pinkPieces then sets tmp to 00, preventing onEnterFrame from running again.

I have a feeling I will have more problems coming up in the near future, so stay tuned :disappointed_relieved:
Thanks for the help.