A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Randomized Buttons

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    7

    Randomized Buttons

    Is there a way to have buttons pop up in random locations on the stage and vanish after a set amount of time? I'm a total noob at Actionscript. Thanks in advance.

  2. #2
    Member
    Join Date
    Apr 2009
    Posts
    58
    var myTimer = new Timer(1000, 1); // Make a timer called myTimer that makes one cycle of 1000 milliseconds.

    myObject.x = Math.random() * 550; // place myObject in a random x coordinate between 0 and 550.
    myObject.y = Math.random() * 400; // place myObject in a random y coordinate between 0 and 400.
    myTimer.start(); // start the myTimer

    myTimer.addEventListener(TimerEvent.TIMER_COMPLETE , myTimerComplete); // When myTimer stops going (after its one cycle of 1000 milliseconds)...

    function myTimerComplete(e:TimerEvent):void { // ...do this:
    myObject.x = -300; // move myObject off the screen
    }


    If you want me to clarify anything I said, or you want me to explain the theory of this code, or you want help applying it to your project, let me know.
    Last edited by medfoe; 04-12-2010 at 05:36 AM.

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    7
    Thank you for the code. Is there a way to make the button keep appearing in different locations until it is clicked?

  4. #4
    Member
    Join Date
    Apr 2009
    Posts
    58
    Actionscript Code:
    var myTimer = new Timer(1000); // Creates a timer that goes in cycles of 1000 milliseconds until you tell it to stop.

    myTimer.start(); // Starts the timer.

    myTimer.addEventListener(TimerEvent.TIMER, tickTock);
    myObject.addEventListener(MouseEvent.CLICK, myClick);

    function tickTock(e:TimerEvent):void {
        myObject.x = Math.random() * (550 - myObject.width) + 1 / 2 * myObject.width;  //This is just getting creative with the code so that the whole button will always be on the screen, instead of parts of it getting cut off sometimes. You can change "myObject.width" to "(myObject.width + 10)" if you want some space between the object and the edge of the screen, changing "10" to whatever number gives it the distance from the edge that you want.
        myObject.y = Math.random() * (400 - myObject.height) + 1 / 2 * myObject.height;
    }
    function myClick(e:MouseEvent):void {
        myTimer.stop(); // It can't put the button in a random location if the timer doesn't tick!
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center