I am trying to create a few simple games for a school project and I decided to try a timeline-controlled programming style. Basically I have a movie clip for each character, each of which has a set of behaviors and animations. The hierarchy is something like Object>Behavior Controller>Sprite Index with each of these being a movie clip with different frames or sections of frames that are switched between by it’s parent (I think I’m using that term correctly). For instance, my sprite indexes are a series of frames, each with a single picture, with a stop() command for each frame. The behavior controllers have nothing but an instance of the sprite index and code to switch it between different sprites; the frames in the behavior controller correspond to different behaviors or “modes” that are switched between based on input and interaction with other Objects. When an Object does something that will interact with another object, it will check what frame the other object is on to determine the outcome of that action.
There are only two separate interactive objects per game, so in theory, this should work.
First question: Is this completely ludicrous?
More specific question: I’m having a little bit of difficulty with the scope of variables and where I can initialize variables and functions. My Behavior Controller is setting and resetting the onEnterFrame function for its nested instance of the Animation movie clip which looks something like the following:
stop();
joe.onLoad = function(){
var stance:Number = 1;
var frame:Number = 1;
var counter:Number = 0;
var lastSChange:Number = 0;
var lastFChange:Number = 0;
var temp:Number = 0;
var frameID:String = "1_1";
function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}
}
joe.onEnterFrame = function(){
counter++;
if (lastSChange >= (counter + 20)) {
//Generate random stance
stance = randRange(1,3);
lastSChange = counter;
}
if (lastFChange >= (counter + 5)) {
//Generate random frame, don't repeat frames
temp = randRange(1,2);
if (frame == 1){ frame += temp; }
else if (frame == 2 && temp == 1){ frame = 1; }
else if (frame == 2 && temp == 2){ frame = 3; }
else if (frame == 3){ frame -= temp; }
lastFChange = counter;
}
//Generate frame name
frameID = (stance + "_" + frame);
//Change clip's stance and frame
joe.gotoAndStop(frameID);
}
The first problem I had was that “joe” was being loaded before this code ran, so none of the variables got initialized. That can be fixed be actually putting that function in the instance of that movie clip, but then I have to put “joe.” in front of every variable in the rest of the code. This works, but is annoying. Is there a way to set scope for a section of code so every variable called is called from that scope? Or is there a recommended scope level for me to be using these variables? (Keeping in mind that the onEnterFrame function is going to be rewritten every time the behavior changes)
I’m sure I’ll have other problems later, but this is the one that I’m stuck on now.