Skip to main content
screeen
Inspiring
January 9, 2012
Answered

Math.random x and y positions - issue in Flash Pro and AS files

  • January 9, 2012
  • 1 reply
  • 5608 views

hi.

i'm having an issue using randomizing the appearance of movie clips using Math.random.

I have a movie clip in the library, which is 85 frames long but it begins and ends with 15 blank key frames. In other words, it has 2 layers, one for action script and one for graphics which are movie clips too, the AS layer is 85 frames long while the art layer is shorter and begins on frame 16.

I began by putting the movie clip on the stage, without any code on the main timeline, with some code on the movie clip's timeline -

this.x = Math.random()*stage.stageWidth;

this.y = Math.random()*stage.stageHeight;

In this case, tested the movie, the swf played just what i wanted, the movie clip "appears" randomely on the stage, plays, disappears, and reappears, and so on and so forth.

I then gave the movie clip on the stage an instance name of  b_mc, removed the code in its timeline, and put it on the main timeline, as such -

b_mc.x = Math.random()*stage.stageWidth;

b_mc.y = Math.random()*stage.stageHeight;

In this case, the movie clip does NOT appear randomely on the stage when the swf is tested, rather, it appears at the same position, appearing and disappearing over and over as planned, but in the same place - NOT as planned.

whey?

This topic has been closed for replies.
Correct answer _spoboyle

In my example above, i dont have the code set up in an AS file as your propose. It is explained as being on the main timeline of a Flash Pro file, so no "private" functions. And when set up as a regular function, on the main timeline of the fla as noted, your code outputs this error:

Scene 1, Layer 'Layer 3', Frame 1, Line 10Warning: 1090: Migration issue: The onEnterFrame is not triggered automatically by Flash Player at run time in ActionScript 3.0.  You must first register this handler for the event using addEventListener ( 'enterFrame', callback_handler).

I did use an AS file but failed horribly so if u want, please write the code u provided (above) as an AS file (constructor function etc...) because when i did so (using BubbleMaker3.as) as the document class in the fla, without anything on the stage, only a library item called bubble linked for AS, with BubbleMaker3.as looking like this:

package

{

    import flash.display.*;

    import flash.events.*;

    import bubble;//this is irrelevant

    public class BubbleMaker3 extends MovieClip

    {

        import flash.events.TimerEvent;

        import flash.utils.Timer;

        var bubblesInGame:uint;

        public var bubbleTimer:Timer;

        var container_mc:MovieClip;

        var cursor3:MovieClip;

        var score:int;

        public function BubbleMaker3()

        {

            var bubbleA:MovieClip = new bubble;

            bubblesInGame = 5;

            bubbleTimer = new Timer(1100, bubblesInGame);

            bubbleTimer.addEventListener(TimerEvent.TIMER, createBubbles);

            bubbleTimer.start();

            /*cursor3 = new Cursor3();

            addChild(cursor3);

            cursor3.enabled = false;

            Mouse.hide();

            stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);*/

            score = 0;

            function createBubbles(event:TimerEvent):void

            {

                addChild(bubbleA);

                container_mc = new MovieClip();

                addChild(container_mc);

                container_mc.addChild(bubbleA);

                bubbleA.x = Math.random() * stage.stageWidth;

                bubbleA.y = (Math.random() * stage.stageHeight);

            }

        }

        /*function dragCursor(event:MouseEvent):void

        {

            cursor3.x = this.mouseX;

            cursor3.y = this.mouseY;

        }*/

    }

}

i got the same result re the position issue: when the movie clip appears on the stage in the swf, it is always in the same spot. Its position is indeed random but only at each additional test of the movie, not what i want.

Further, i got errors to do with the cursor being masked, hence it is commented in my code (above)

D:\flash cs5.5\images\bubble_4_document_class\BubbleMaker2.as, Line 441120: Access of undefined property Mouse.

Plus, the timer didnt work properly at all and addChild only add one instance of the bubble, the timer makes the movie clip appear to "jump" around the stage 5 times, and on the 5th, it stays and plays out, then reappears in the same position, as said.

Precisely why i didnt produce all this effort in my initial query....to try to focus on the question of: why when the Math.random code is in an mc on the stage it appears randomly while when it is on the main timeline or in a AS, does it not appear or trace randomly?


ok sorry i didn't run this example to test if it would work sorry.

you can still use the example I wrote for you but you'll have to change the function name to something else

so mc_b.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);

function onEnterFrameHandler(e:Event):void

{

     // same as before

}

oh and it should be mc.currentFrame == 1

1 reply

_spoboyle
Inspiring
January 9, 2012

have you traced out Math.random()*stage.stageWidth every time you want the MovieClip to appear in a new place. what values are you getting is it different everytime? Is stage.StageWidth = 0 ? Or perhaps Math.random()*stage.stageWidth is only getting called once?

screeen
screeenAuthor
Inspiring
January 9, 2012

in the first case, when the code is only inside the MC (and NOT on the main timeline,) as such

this.x = Math.random()*stage.stageWidth;

this.y = Math.random()*stage.stageHeight;

trace (Math.random()*stage.stageWidth);

at each "appearance" of the mc it is in a different position in the swf, and at each appearance there's a new trace with its stage x position

However, and this is the problem, when the code inside the mc is commented out and instead i have the code for positioning in the first (and only) frame of the main timelins as below, then there's only one single trace.

b_mc.gotoAndPlay(Math.floor(Math.random() * 5) + 1);

b_mc.x = Math.random()*stage.stageWidth;

b_mc.y = Math.random()*stage.stageHeight;

trace(Math.random()*stage.stageWidth);

Of course, in this case, the next time i test the movie, the mc will have a different position and trace in accordance.

thanks.

_spoboyle
Inspiring
January 9, 2012

Ok I think I understand the issue.

before you call b_mc.gotoAndPlay(Math.floor(Math.random() * 5) + 1);

add

b_mc.addEventListener(Event.ENTER_FRAME, onEnterFrame);

then add the following function

private function onEnterFrame(e:Event):void

{

     var mc:MovieClip = e.target as MovieClip;

     if (mc.currentFrame == 0)

     {

          mc.x = Math.random()*stage.stageWidth;

          mc.y = Math.random()*stage.stageHeight;

     }

}

this will setup a listener for the enterFrame event. Every frame we check if the MovieClip is at frame 0 and if so recalculate its position