A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: if (statement) question

  1. #1
    Member
    Join Date
    Feb 2003
    Posts
    55

    if (statement) question

    Hi there.


    I am trying to change the alpha of a movie clip using if statement.
    I have done this but i want to change the alpha using a fade in way.
    e.g. From 100% fade to 0% using a motion tween.

    I am wondering if you can do such an effect using only actionsript!

    In the example i have created some movie clips turning off & on their alpha, but i didn't manage to do it using any fade effect.

    Please see the fla. attachment

    thanks.
    Attached Files Attached Files

  2. #2
    Flash Enthusiast? thecombat's Avatar
    Join Date
    Aug 2003
    Location
    New Jersey
    Posts
    305
    Heres some psuedo code:
    code:

    if (statement) {
    movieclip._alpha = 90;
    break;
    movieclip._alpha = 80;
    break;
    movieclip._alpha = 0;
    }
    }



    I'm not expecting that to work, its just a reference, look in the action script dictionary for break; and _alpha...

  3. #3
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Hi,

    i've attached an edited version of the file that fades the clips in and out when the buttons are pressed. all the code is found in frame 1.

    hope it helps
    Attached Files Attached Files

  4. #4
    Member
    Join Date
    Feb 2003
    Posts
    55

    if (statement) question

    thanks
    ill see the fla.
    hope its what i want.

  5. #5
    Member
    Join Date
    Feb 2003
    Posts
    55

    if (statement)

    I have just seen the fla.
    Its exactly what i wanted. I am glad.
    Although, i want to ask you for a few more things about it!

    what changes i have to do if i want not to use 5 frames for the fade out?
    e.g. when you press the button fade in using 5 frames, when press again fade out instantly.

    And something more....When you press and see the fade in, play a sound as well, when press again not hear the sound.

    I dont know how complicate things go, if its a problem, forget it.

    Thanks anyway.

  6. #6
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Hi,

    you could have the clip fade out instantly and then fade back more gradually using this,

    code:

    function fade() {
    this._alpha += (this.dest - this._alpha) / this.ease;
    if (Math.round(this._alpha) == this.dest) {
    this._alpha = this.dest;
    delete this.onEnterFrame;
    }
    }

    for (var i = 0; i < 5; ++i) {
    this["red" + i].dest = 100;
    this["red" + i].ease = 1;
    this["btn" + i].id = i;
    this["btn" + i].onPress = function() {
    // store a reference to the clip we'll be targeting in a variable to save us having to type out the whole target path several times
    var mc = this._parent["red" + this.id];
    if (mc.dest == 100) {
    mc.dest = 0;
    mc.ease = 1;
    } else {
    mc.dest = 100;
    mc.ease = 5;
    }
    mc.onEnterFrame = fade;
    };
    }



    if you wanted a sound to play as the clip faded in (this assumes each clip plays the same sound) you could import your sound into the movie, using

    file > import

    then find the sound file in the library, right click on its name and choose linkage from the options. in the linkage dialog box, select export for actionscript and fill in the identifier field (in this example I used the identifier bgm) now in your movie your frame actions could become,

    code:

    var mySound = new Sound(this); // create a new sound object
    mySound.attachSound("bgm"); // associate it with the your sound file

    function fade() {
    this._alpha += (this.dest - this._alpha) / this.ease;
    if (Math.round(this._alpha) == this.dest) {
    this._alpha = this.dest;
    delete this.onEnterFrame;
    }
    }

    for (var i = 0; i < 5; ++i) {
    this["red" + i].dest = 100;
    this["red" + i].ease = 1;
    this["btn" + i].id = i;
    this["btn" + i].onPress = function() {
    var mc = this._parent["red" + this.id];
    if (mc.dest == 100) {
    mc.dest = 0;
    mc.ease = 1;
    } else {
    mc.dest = 100;
    mc.ease = 5;
    this._parent.mySound.start(0, 1); // play the sound
    }
    mc.onEnterFrame = fade;
    };
    }


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