Vagabond
03-03-2009, 12:50 AM
I'm running into a weird problem with the multiple movie clips running in my game.
It's the exact same clip placed on the stage multiple times, with unique instance names. The root time line passes variables to each one to control them differently, one such variable is called myDelayRate and it controls a frame-based time delay so each movie clip has it's own execution timers.
To summarize, the first frame of the movie clip (named generate_deer_mvc) will generate a random number between 2 and the given delay rate (myDelayRate), then it will play, and check every frame number for a match to myDelayRate. When one is found, the playback skips ahead to a frame labeled "genesis" which will attach a temporary movie clip (named and linked as temp_deer_mvc), then move that movie clip across the stage.
For right now, all I need it to do is check to see if that movie clip has passed beyond the stage boundaries, and if so, remove the movie clip and return the playback to frame 1 to start it over again.
The problem is that for some reason, the deer movie clips will stop moving and the playback will jump to 1, run through the myDelayRate check, then return to the genesis frame to continue to deer's movement across the stage.
(I know it starts the delay over again because I traced the onEnterFrame function that checked myDelayRate against the current frame number, and when the deer suddenly stopped, my output was spammed with incrementing numbers as it ran through the frames again. I have, since, removed that trace so as to limit the output spam)
I can't figure out why it's acting like this. It even did this before I added any gotoAndPlay() command, so I don't even know where it's coming from, or why the time line would jump like this for no apparent reason.
I've been working on this off and on for the last week, along with 2 other projects, and I know it's probably something simple that I'm missing, but I'm just too close and my eyes are too blurry to see it; so I hope a few extra pair of clear eyes will help.
I've attached a zip file with the FLA in question. Please let me know if you notice anything that might explain this. I really appreciate any advice ^^
Vagabond
03-04-2009, 05:21 AM
Turns out I had left the faulty pauseMovie prototype in the script. So it was creating a delay timer and pausing the movie at the genesis frame. Except, this prototype doesn't effect onEnterFrame functions, so the movie appeared to be working fine, but once that timer ran out, the prototype hit the play button and sent the MC back to frame 1, thus executing another delay run, and subsequently another genesis.
Additionally, there was a problem getting the deer MC to flip properly. Turned out I was setting _xscale twice. Once to flip it, and then later to scale the deer down as to simulate distance.
For anyone interested, here's a side-by-side (well... sort of) comparison of the before and after scripts.
With Bugs:
01 stop(); // stop local time line
02
03 // create local xPos and yPos
04 var xPos:Number = 0;
05 var yPos:Number = 0;
06
07 // myMOBnum is the serialized number for the next MOB this MC will generate
08 var myMOBnum:Number = 0;
09
10 // declared here for use outside the GenerateDeer function
11 var myMOBname:String;
12 var myCoinFlip:String;
13
14 // this function paused the local time line for a number of seconds within a range defined by myRate
15 function GenerateDeer():Void {
16
17 trace("Generating new Deer...");
18
19 // set duration (for pauseMovie)
20 // a number between myRate and 2*myRate
21 var duration:Number = (Math.round(Math.random()*myRate)+myRate)*1000;
22 trace("Duration set: " + duration);
23
24 // pause movie for 3 seconds
25 this.pauseMovie(duration);
26
27 // create new MOB name
28 myMOBname = "deer"+myMCnum+myMOBnum;
29 trace("New deer name: " + myMOBname);
30
31 // attach new MOB
32 this.attachMovie("temp_deer_mvc", myMOBname, this.getNextHighestDepth());
33 trace("New deer attached...");
34
35 // flip a coin to decide which direction the deer will be walking
36 myCoinFlip = _root.flipCoin();
37 trace("My Coin Flip = " + myCoinFlip);
38
39 // set MOB's x position (and flip transform) depending on myCoinFlip
40 switch(myCoinFlip){
41
42 // for heads, plant deer at 0 (right side of the stage)
43 case "heads": xPos = 0; break;
44
45 // for tails, plant deer at (negative) stage width and flip horizontally
46 case "tails": xPos = _root.stageWidth*(-1); this[myMOBname]._xscale *= -1; break;
47
48 } // end switch(myCoinFlip)
49
50 // set MOB's y position randomly between 0 and 50;
51 yPos = Math.round(Math.random()*50);
52
53 // place the new MOB on the stage
54 this[myMOBname]._x = xPos;
55 this[myMOBname]._y = yPos;
56
57 // set new MOB scale
58 this[myMOBname]._xscale = myScale;
59 this[myMOBname]._yscale = myScale;
60
61 trace("Deer x axis = " + this[myMOBname]._x + ", y axis = " + this[myMOBname]._y);
62
63 } // end function generate_deer()
64
65 // generate deer
66 GenerateDeer();
67
68 this.onEnterFrame = function(){
69
70 // if there is a deer on the stage
71 if(this[myMOBname].deerStatus != "alive"){
72
73 // remove the deer MC
74 this[myMOBname].removeMovieClip();
75
76 // return to frame 1 and start over
77 gotoAndPlay(1);
78
79 } // end if(this[myMOBname].deerStatus != "alive")
80
81 // check the results of myCoinFlip
82 switch(myCoinFlip){
83
84 // if heads, move the deer across the stage from right to left (negative trend)
85 case "heads": this[myMOBname]._x -= myMOBspeed; break;
86
87 // if tails, move the deer across the stage from left to right (positive trend)
88 case "tails": this[myMOBname]._x += myMOBspeed; break;
89
90 } // end switch(myCoinFlip)
91
92 // if the MOB's x axis falls outside the stage, kill deer
93 // note: these MC's are placed on the right edge of the stage,
94 // so 0 here is +550 on the root stage, and -550 here is 0 on the root stage
95 // thus, the limits are reversed, 0 is the right edge, -550 is the left edge
96 if(this[myMOBname]._x > 0 || this[myMOBname]._x < -550){
97
98 this[myMOBname].deerStatus = "dead";
99 trace("Deer escaped! Removing MOB...");
100
101 } // end if(this[myMOBname]._x > 0 || this[myMOBname]._x < -550)
102
103 } // end function onEnterFrame
I took out the xPos and yPos variables, the call to the pauseMovie prototype, as well as the duration variable, and I moved the code setting the _x and _y values, and the _xscale and _yscale values inside the switch for myCoinFlip. Then, of course, I went through and removed the traces, as now that it works, I don't really need them anymore.
So now it looks like this:
01 stop(); // stop local time line
02
03 // myMOBnum is the serialized number for the next MOB this MC will generate
04 var myMOBnum:Number = 0;
05
06 // declared here for use outside the GenerateDeer function
07 var myMOBname:String;
08 var myCoinFlip:String;
09
10 // this function paused the local time line for a number of seconds within a range defined by myRate
11 function GenerateDeer():Void {
12
13 // create new MOB name
14 myMOBname = "deer"+myMCnum+myMOBnum;
15
16 // attach new MOB
17 this.attachMovie("temp_deer_mvc", myMOBname, this.getNextHighestDepth());
18
19 // flip a coin to decide which direction the deer will be walking
20 myCoinFlip = _root.flipCoin();
21
22 // set MOB's x position (and flip transform) depending on myCoinFlip
23 switch(myCoinFlip){
24
25 // for heads, plant deer at 0 (right side of the stage),
26 case "heads":
27
28 // set new MOB x at 0
29 this[myMOBname]._x = 0;
30 // set new MOB y randomly between 0 and 50
31 this[myMOBname]._y = Math.round(Math.random()*50);
32
33 // set new MOB scale as given by _root
34 this[myMOBname]._xscale = myScale;
35 this[myMOBname]._yscale = myScale;
36
37 break; // end case "heads"
38
39 // for tails, plant deer at (negative) stage width and flip horizontally
40 case "tails":
41
42 // set new MOB x to the opposite end of the stage
43 this[myMOBname]._x = Stage.width*(-1);
44 // set y randomly between 0 and 50
45 this[myMOBname]._y = Math.round(Math.random()*50);
46
47 // set new MOB with inverted xscale and normal yscale
48 this[myMOBname]._xscale = myScale*(-1);
49 this[myMOBname]._yscale = myScale;
50
51 break; // end case "tails"
52
53 } // end switch(myCoinFlip)
54
55 } // end function generate_deer()
56
57 // generate deer
58 GenerateDeer();
59
60 this.onEnterFrame = function(){
61
62 // if there is a deer on the stage
63 if(this[myMOBname].deerStatus != "alive"){
64
65 // remove the deer MC
66 this[myMOBname].removeMovieClip();
67
68 // return to frame 1 and start over
69 gotoAndPlay(1);
70
71 } // end if(this[myMOBname].deerStatus != "alive")
72
73 // check the results of myCoinFlip
74 switch(myCoinFlip){
75
76 // if heads, move the deer across the stage from right to left (negative trend)
77 case "heads": this[myMOBname]._x -= myMOBspeed; break; // end case "heads"
78
79 // if tails, move the deer across the stage from left to right (positive trend)
80 case "tails": this[myMOBname]._x += myMOBspeed; break; // end case "tails"
81
82 } // end switch(myCoinFlip)
83
84 // if the MOB's x axis falls outside the stage, kill deer
85 // note: these MC's are placed on the right edge of the stage,
86 // so 0 here is +550 on the root stage, and -550 here is 0 on the root stage
87 // thus, the limits are reversed, 0 is the right edge, -550 is the left edge
88 if(this[myMOBname]._x > 75 || this[myMOBname]._x < -625){
89
90 this[myMOBname].deerStatus = "dead";
91
92 } // end if(this[myMOBname]._x > 0 || this[myMOBname]._x < -550)
93
94 } // end function onEnterFrame
cjx3711
03-05-2009, 12:25 PM
Thanks for the answer. I was tearing my hair out trying to figure out the problem. Sorry I couldn't find it.
Vagabond
03-09-2009, 11:03 PM
No worries ^^ I didn't fine it until I went through and added a trace() to almost every line of script so I could "read" it at runtime. It's effective, but time consuming *nods*
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.