0
\$\begingroup\$

I have seen several references of A.I. and FSM, but sadly I still can't understand the point of an FSM in AS2.0. Is it a must to create a class for each state?

I have a game-project which also it has an A.I., the A.I. has 3 states: distanceCheck, ChaseTarget, and Hit the target.

It's an FPS game and played via mouse.

I have created the A.I. successfully, but I want to convert it to FSM method...

My first state is CheckDistanceState() and in that function I look for the nearest target and trigger the function ChaseState(), there I insert the Hit() function to destroy the enemy,

The 3 functions that I created are being called in AI_cursor.onEnterframe.

Is there any chance to implement an FSM without the need to create a class? From what I've read before, you have to create a class. I prefer to write the code on frames in flash and I still don't understand how to have external classes.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

First of all: You don't need classes to implement a FSM. Sure, it's a nice and extendable design, but something like this works just as well (using a switch statement):

onUpdate(){
    switch(state){
        case STATE_1:
            do state 1 stuff
            if(conditionThatLeadsToState2){
                state = STATE_2;
            }
            break;
        case STATE_2:
            do state 2 stuff
            if(conditionThatLeadsToState3){
                state = STATE_3;
            }
            break;
        ... etc ...
    }
}

Using a switch statement works for very simple FSMs, but gets really cumbersome with more complex architectures. Since Functions are also objects in flash, you could have functions as states (instead of classes). Here's an example written in ActionScript 2:

var count = 0;

function state1(){
    count++;
    if(count == 10){
        state = state2;
    }
    trace("state 1, increasing: " + count);
}

function state2(){
    count--;
    if(count == 0){
        state = state1;
    }
    trace("state 2, decreasing: " + count);
}

var state = state1;

function onEnterFrame(){
    state();
}

As you can see, we're always executing state in the update (onEnterFrame) method. But we can dynamically change state during runtime. So in the state1 function I'm assigning state2 to state, when a certain condition is met. This pattern is pretty close to an implementation with classes and should be fairly flexible.

But if your implementation already works, why not keep it the way it is? I'm pretty sure you have already implemented your own FSM pattern and it seems to work.

Also using classes is really easy. Just write your class, save it to an .as file in the same folder as the FLA and then instantiate it with myInstance = new MyClass(); in your code. A quick google-search turned up this tutorial.

\$\endgroup\$
2
  • \$\begingroup\$ thx for the fast response , uhmm i need to implement the FSM for my undergraduate thesis , so i choose the FSM method , haha ... i dunno that my AI is the method of the FSM or not , because im doing the code from trial and error etc. i dont know about FSM pattern too , almost of the article that i read is the FSM pattern is switch case ( like the 1st of your code) but they design it with class. oh ya, my code is similar from the second Example code, is that FSM pattern too ?? \$\endgroup\$
    – Up2u
    Jul 6, 2012 at 11:55
  • \$\begingroup\$ @Up2u A state machine just consists of a state that is updated regularly and that switches to another state whenever a given condition is met. There are tons of different ways to implement this, whether you use classes, state-tables, switch or if statements or function pointers is entirely up to you. \$\endgroup\$
    – bummzack
    Jul 18, 2012 at 6:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .