Skip to main content
Known Participant
May 11, 2016
Answered

Action Script 3 need some randomness

  • May 11, 2016
  • 1 reply
  • 2335 views

Hi all, I have a question.

I'm creating interactions in Adobe Animate using AS3.

I have 3 moviclips each is a self contained game/interaction.

How can I script a button that will play one of the moviclips at random, then when the player comes back to main timeline and the button is pressed again the user is sent randomly to one of the other movieclips? I dont want the user to see the same moviclip twice in a row.

Hope you guys can help, thanks

John

Peace

    This topic has been closed for replies.
    Correct answer sinious

    Hi Sinious, I was having trouble making a button inside the moviClip's timeline. I had used some text over a  shape and this seemed to be the cause of my problems. When I removed the text the code for Tap Event worked fine.

    I am now having trouble scripting another button on the same movieClip's timeline to remove the Clip from the stage.

    I hope that you have a relaxing vacation.


    Hello again,

    Sorry for the late reply. Text will block clicks. If you're generating the text yourself then it's on a layer that is "higher" or above the shape. If you attach the click event to the shape then the text may intercept it. This happens to a lot of people that draw a button shape and then add text on the timeline in the layer above where the text interferes with the click.

    When I'm generating dynamic text, I set the TextField property to .mouseEnabled = false; Try doing this to see if it solves your problem.

    e.g.

    yourNewTextField.mouseEnabled = false;

    Then see if you can click on your button.

    InteractiveObject - Adobe ActionScript® 3 (AS3 ) API Reference

    1 reply

    sinious
    Legend
    May 12, 2016

    This is just a quick frame script assuming there's timeline clips named 'clip_1', 'clip_2' and 'clip_99', just for examples. It just has an array you can add as many clips to that you like (named anything) and it'll stop all other clips in the array while starting a different random clip that won't be the same clip. This is the easy way so it should be easy to follow (there's a million ways to do it).

    Again assuming those clips and 'changeClipBtn' was the name of your button, just change to suit your needs. This is a script on frame 1 of the main timeline:

    // keep track of the current clip

    var lastClip:int = 0;

    // filling an array with instance names on the timeline to choose between

    // add as many as you like

    var clips:Array = [ 'clip_1', 'clip_2', 'clip_99' ];// assuming on the main stage

    // might want to iterate though clips turning their visible off, etc

    // assign button handler, season name to taste

    changeClipbtn.addEventListener( MouseEvent.CLICK, changeClipHandler );

    function changeClipHandler(e:MouseEvent):void {

      // assure clips contains something to assure no infinite loops

      if ( clips.length == 0 ) { return; }// no clips, do nothing

      // we at least have one clip, stop all clips

      for ( var i:int = 0; i < clips.length; i++ ) {

      root[ clips ].gotoAndStop(1);

      }

      // only a single clip? play it again

      if ( clips.length == 1 ) {

      this[ clips[0] ].gotoAndPlay(1);

      return;

      }

      // if we get here we at least have 2 clips

      // rand generate a number pointing to a spot in the array

      var nextClip:int = Math.floor( Math.random() * clips.length );

      // if the generated number is the same as the last number, generate again

      while ( nextClip == lastClip ) {

      nextClip = Math.floor( Math.random() * clips.length );

      }

      // just showing you the values

      trace('nextClip:' + nextClip + ', lastClip:' + lastClip);

      // track the new loading clip

      lastClip = nextClip;

      // here you can just play the clip or return the value, I'm asking it to play

      root[ clips[nextClip] ].gotoAndPlay(1);

    }

    Known Participant
    May 18, 2016

    Thanks sinious, that will surely be a help to me, thanks again.

    sinious
    Legend
    May 24, 2016

    You're welcome and good luck!