dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Need help with random plz

  1. #1
    Member
    Join Date
    Feb 2002
    Posts
    45
    I have a MC with 2 frames that are playing randomly.
    Code:
    frame = Number(random(2))+1;
    tellTarget ("_root.main.randomMC") {
        gotoAndStop(_root.main.randomMC.:frame);
    }
    then I have script on the "main" MC timeline that needs to identify which of the 2 frames was chosen so it can play according sound. What I tried:
    Code:
    if (_root.main.randomMC._currentframe == 1) {
        mysound = new Sound();
        mysound.attachSound("C");
        mysound.start();
    }
    if (_root.main.randomMC._currentframe == 2) {
        mysound = new Sound();
        mysound.attachSound("B");
        mysound.start();
    }
    OR:

    Code:
    if (_root.main.randomMC._currentframe == 1) {
        mysound = new Sound();
        mysound.attachSound("C");
        mysound.start();
    } else {
        mysound = new Sound();
        mysound.attachSound("B");
        mysound.start();
    }
    Neither works. Except these 2 I tried ALOT of things but it still seems like randomizer chooses 1st or 2nd frame BUT it doesn't actually play it but rather always stays on the 1st frame. I've attached the FLA http://myx.mine.nu/randomtest01.fla so have a look if you still don't understand what I'm talking about.
    in FLA, main = staff; randomMC = sharp

    Please help me out

  2. #2
    Member
    Join Date
    Feb 2002
    Posts
    45
    anybody?

  3. #3
    Senior Member Pporksoda's Avatar
    Join Date
    Jul 2000
    Location
    baltimore
    Posts
    174
    you have a couple of problems.

    change your syntax here from this:
    framesharp = Number(random(2))+1;
    to this:
    framesharp = random(2)+1;

    that will fix the random problem.

    the reason it doesn't play the sound is because the frame action that plays the sound is read by the computer before the random action takes place. that's why it always plays the lower note.

    simplify your code by using onClipEvent(load). select the instance called 'sharp' and paste this code in the actions window:

    onClipEvent (load) {
    framesharp = random(2)+1;
    gotoAndStop(framesharp);
    if (_currentframe == 1) {
    mysound = new Sound();
    mysound.attachSound("C");
    mysound.start();
    }
    if (_currentframe == 2) {
    mysound = new Sound();
    mysound.attachSound("B");
    mysound.start();
    }
    }

    as soon as 'sharp' loads, it calculates the random number, goes to the random frame, and then plays the sound based on that frame.

    you also no longer need to use tellTarget. instead of this--
    tellTarget ("_root.staff.sharp") {
    gotoAndStop(_root.staff.sharp.:framesharp);
    }
    try this--
    _root.staff.sharp.gotoAndStop(framesharp);

  4. #4
    Member
    Join Date
    Feb 2002
    Posts
    45
    Yep that worked but that's not exactly what I needed. There gonna be more than 1 "sharp" and that's why I need "if _root...._currentframe == 1or2". And no matter what it loads, like you said, first the action in the frame and then the random function. But that's ok because I finally found another way (maybe a bit long but it works). Well thanks anyways and you got a good site there

  5. #5
    Senior Member Pporksoda's Avatar
    Join Date
    Jul 2000
    Location
    baltimore
    Posts
    174
    you can paste that code on any movieclip reguardless of it's name or position within the hierarchy. no need to target anything.

    glad you worked it out though.

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