2 Replies Latest reply on Oct 7, 2010 7:02 PM by TheEricBraun

    Getting a random number without clicking

    TheEricBraun Level 1

      Hello There. I've been making a game sort of like Four Second Fury (Frenzy and Firestorm). Im right now just at the begging process and i've come across a problem that i cant quite figure out whats going on =/. If anybody could help it'd be greatly appreciated =). Heres what i have.

       

      onClipEvent (enterFrame) {
      myDisplay = random(3)+1; // myDisplay is the random number variable.
      _root.timer++; // once the animaton goes to frame 40 i want it to go to one of the 3 games.
      if (myDisplay == 1 && _root.timer>=40) {
        gotoAndStop("game1");
      }
      if (myDisplay == 2 && _root.timer>=40) {
        gotoAndStop("game2");
      }
      if (myDisplay == 3 && _root.timer>=40) {
        gotoAndStop("game3");
      }
      }

       

       

      When i run it, myDisplay never changes it always stays at 0. I made a new Flash Document and I tried doing this but instead it was onRelease() and it worked just fine. So i was wondering if there was anyway i could do this without using the onRelease() method.

       

      Kind Regards, TheEricBraun

        • 1. Re: Getting a random number without clicking
          Rothrock Level 5

          My guess is that the _root.timer variable isn't initialized so that undefined++ is still undefined. But I don't know. Seems that should work. However there are a bunch of things that, while not exactly wrong, certainly aren't right!

           

          Unless you are publishing to Flash 5, there is no reason to use onClipEvents and many reasons to stop doing so. Here is a great article about the issue:

           

          http://www.quip.net/blog/2006/flash/museum-pieces-on-and-onclipevent

           

          The next thing is that if you are wanting to wait until frame 40, it would be better to see if the frame is actually 40. A secondary counter is a clever strategy, but I don't think it is the best approach. Here is the code I might use for this situation.

           

          myClip.onEnterFrame=waitForIt;

           

          function waitForIt(){

               if(this._currentFrame>=40){

                    delete this.onEnterFrame;

                    this.gotoAndStop("game"+Math.round(Math.random()*2)+1);

               }

          }

           

          Or something like that. I imagine you have more going on that I don't know about. But I highly recommend taking the code off of objects and putting it on the timeline. That will make it much easier to find and maintain.

          • 2. Re: Getting a random number without clicking
            TheEricBraun Level 1

            Well i know that _root.timer is actually working because i created an instance of it on the stage. =/