A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Animation with color change with actionscript

  1. #1
    Member
    Join Date
    Sep 2005
    Posts
    62

    Animation with color change with actionscript

    Hi,

    I'm not very good at all in action script, that's why this may sound like a rather dumb question...

    I have to make a project where people can colorize different parts of a seat individually.

    So I have 30 buttons of different colors for the gallows and another 30 buttons for the armrest.
    The seat image is kind of a wireframed picture.
    When one clicks on red for the armrest for example the armrest shall be covered by the fabric in the red color by using a shape tween or maybe by working with a mask.

    Now I do not really want to make 900 (30x30) tweens and was wondering if this can be done with actionscript.

    I found out how to change a color with actionscript by using the "mycolor set RGB" script, but I could not find anywhere if this can work together with an animation.

    Anyone knows something?? Thanks.

  2. #2
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Hi,

    Are you refferring to an actionscripted color fade? Maybe something like........
    code:

    doTween = function () {
    // t will go from 0 to 1 over the time specified by this.dur (duration)
    var t = (getTimer()-this.startTime)/this.dur;
    if (t>1) {
    t = 1;
    delete this.onEnterFrame;
    }
    var r = t*this.r2+(1-t)*this.r1;
    var g = t*this.g2+(1-t)*this.g1;
    var b = t*this.b2+(1-t)*this.b1;
    // trace(r + ", "+ g + ", " + b);
    this.clr.setRGB((r << 16)+(g << 8)+b);
    }
    MovieClip.prototype.beginColorTween = function(r1, g1, b1, r2, g2, b2, duration) {
    this.r1 = r1;
    this.g1 = g1;
    this.b1 = b1;
    this.r2 = r2;
    this.g2 = g2;
    this.b2 = b2;
    this.dur = duration;
    this.startTime = getTimer();
    this.clr = new Color(this);
    this.onEnterFrame = doTween;
    }
    // transform movieclip 'mc' from red to green over 4 seconds
    _root.onMouseDown=function(){
    mc.beginColorTween(100, 100, 255, 255, 100, 0, 2*1000);
    }


  3. #3
    Junior Member
    Join Date
    Feb 2006
    Posts
    12
    Thats pretty cool. You got a dynamic one? I'd like to apply it to a few clips.

  4. #4
    Member
    Join Date
    Sep 2005
    Posts
    62
    Thank you NTD,

    This seems like a great script. I have tried to figure out how to use it, but as I am still really bad in AS I didn't get it.

    To test it I put on the stage one button (btn) and one MC (aaa). The MC (aaa) is supposed to change color when clicked on the button (btn)

    I have put your AS in the first frame of the timeline in a different layer.

    I then have replaced "mc" by "aaa".

    Then I changed this line
    _root.onMouseDown=function(){

    into
    _root.btn.onMouseDown=function(){

    in order to specifie that the action has to take place after clicking on "btn"
    but it doesn't work.

    Does someone know where I have to specifie the Instance name of the button?

  5. #5
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    onMouseDown events are run whenever the mouse button is pressed, regardless of the object the you attach it to.

    You need to use onPress or onRelease if you want the event to occur when a particular object is clicked on.

    i.e.

    _root.btn.onRelease = function(){
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  6. #6
    Member
    Join Date
    Sep 2005
    Posts
    62
    Great. Thank you! That works.

    I didn't realize anything happened as my MC was set to Alpha 0, because I actually wanted the MC be totally transparent at first and then be filled with color. Can I change lines in this script in order to do so? (Maybe the r1,g1,b1 make it alpha=0??)

    Also, is there a possibility in AS that the color will not only fade in but also wipe from the bottom to the top.
    Should look like the transition in: Timeline effects>Transform/Transition>Transition (clicking on Wipe and Fade)

    Actually if the Wipe function can be done with AS the MC does not even have to be transparent, I think.
    I only want it to be transparent so one can see what's beneath it. Beneath it is a wireframed chair which shall be covered by leather in the appropriate color when clicking on a button. Hope this makes any sense?
    Last edited by oswaldine; 02-16-2006 at 12:26 PM.

  7. #7
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Hi,

    This little script was my first venture into time based animation and was written by Jbum.

    Thats pretty cool. You got a dynamic one? I'd like to apply it to a few clips.
    The script uses prototype inheritance to make it very dynamic. It can be used just like the built in play() or stop() methods. To control more than one movieclip, just assign a new movieclip to the function......

    someMCInstanceName.beginColorTween(100, 100, 255, 255, 100, 0, 2*1000);


    The first six parameters of the function are the rgb color values to start and end with. Manipulate those values to achieve different color fades. You can open the Flash color mixer to see the RGB value of any color.

    As for the wipe effect,....... you could use the function in combination with a mask to simulate a wipe effect.

    Ok, with that said, there are other options available for color fades. With Flash8, the bitmap data class has some interesting color options. The built in easing classes also offer color fades. And last but not least, the laco tween class is another easing class that offers color manipulation. The first two can be found inside flash. Do a google search to find the laco tween class.

    Regards
    NTD

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