A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Masking with actionscript

  1. #1
    Junior Member
    Join Date
    Jul 2006
    Posts
    5

    Masking with actionscript

    Hello.

    This is my first post on flashKit.

    I am familiar with the Flash interface and timeline animation techiques, but am new to actionscript. I am trying to understand masking using script.
    I can mask a circle movie clip by a triangle shape movie clip on the main timeline like so:

    this.attachMovie( "circle1", "circle1", this.getNextHighestDepth());
    this.attachMovie( "triangle", "triangle", this.getNextHighestDepth());
    this.circle1.setMask(triangle);

    However, when they are place in another movie clip using createEmptyMovieClip() method like so:

    this.createEmptyMovieClip( "testClip", this.getNextHighestDepth());

    this.testClip.attachMovie( "circle1", "circle1", testClip.getNextHighestDepth());

    this.testClip.attachMovie( "triangle", "triangle", testClip.getNextHighestDepth());

    circle1.setMask(triangle);

    the mask does not work. Why is this? The second mask attempt is almost exactly the same as the first, except that it is inside a nested movie clip (testClip) on the main timeline.

    This is a very simple example. I am actually wanting to mask thumbnail images that are in an array and that are dynamically placed on the stage. But first, I need to understand the simplest masking technique possible.
    Is there anywhere online that has tutorials on actionscripting masks - for further study?

    Can anyone help me with this?

    Michon

  2. #2
    Can't Re-Member madzigian's Avatar
    Join Date
    Apr 2004
    Location
    Boston MA
    Posts
    2,662
    Welcome To FK!

    As far as your problem goes, you were almost there... So very close!! Try this instead:
    Code:
    this.createEmptyMovieClip("testClip", this.getNextHighestDepth());
    
    // since you are already within the scope of 'this', you don't really need to use it
    // i.e. this.testClip.attachMovie() is the same as testClip.attachMovie() 
    testClip.attachMovie("circle1", "circle1", this.getNextHighestDepth());
    
    // by targeting testClip to attach a movieclip inside it, you should be 
    // looking for the first available depth inside testClip --> i.e. 'this'
    // so this.getNextHighestDepth() would get you the first avail depth in testClip
    
    testClip.attachMovie("triangle", "triangle", this.getNextHighestDepth());
    
    // your target path was off here... since you attached those clips INSIDE testClip,
    // you have to reference them through testClip.
    testClip.circle1.setMask(triangle);
    hope that helps you a bit...
    Please DO NOT PM me simply for individual help. Post in the public forums...that's what they are for, and they allow others to benefit as well. - Thx
    OS X 10.3.9 || Flash MX04 Pro
    | del.icio.us || furl || Helpful Source Files || Free Pixel Fonts |

  3. #3
    Junior Member
    Join Date
    Jul 2006
    Posts
    5

    Masking with actionscript

    Hello Madzigian.

    Thank you for your reply. Yes that did help. Right now, at my level, scope seems to be kind of my nemesis, so to speak.

    The next problem (which is where I started - believe it or not), is how to mask movie clips in a dynamically loaded array. Here is an example of what I mean. There are two arrays as follows:

    var circleArray:Array = new Array();
    circleArray.push( "circle1" );
    circleArray.push( "circle2" );
    circleArray.push( "circle3" );

    var triangleArray:Array = new Array();
    triangleArray.push( "triangle1" );
    triangleArray.push( "triangle2" );
    triangleArray.push( "triangle3" );

    //load the circle movie clips
    for ( var i:Number = 0; i < circleArray.length; i++)
    {
    this.attachMovie( circleArray[i], circleArray[i], this.getNextHighestDepth());
    circleArrayHolder = this[circleArray[i]];
    circleArrayHolder._x = 0;
    circleArrayHolder._y = (100 * i);
    }
    //load the triangle movie clips
    for ( var i:Number = 0; i < triangleArray.length; i++)
    {
    this.attachMovie( triangleArray[i], triangleArray[i], this.getNextHighestDepth());
    triangleArrayHolder = this[triangleArray[i]];
    triangleArrayHolder._x = 0;
    triangleArrayHolder._y = (100 * i);
    }
    //mask the circle movie clips with the triangle movie clips??
    circleArrayHolder.setMask(triangleArrayHolder);

    It's kind of like my first post. Close, but.....

    The third item in the circleArray (circle3) is masked by the third item in the trangleArray (triangle3), but the first 2 items in the circleArray are not maske. Why is this? There is something fundamentally wrong. Since everything is on the main timeline, scope should not be an issue.

    I have been playing around with arrays lately and sometimes find that only the last item in the array displays or is not displayed or is the item that is affected in some way. Puzzling.

    What am I doing wrong?

    Please help.

    Michon

  4. #4
    Can't Re-Member madzigian's Avatar
    Join Date
    Apr 2004
    Location
    Boston MA
    Posts
    2,662
    try this:
    Code:
    var circleArray:Array = new Array();
    circleArray.push("circle1");
    circleArray.push("circle2");
    circleArray.push("circle3");
    
    var triangleArray:Array = new Array();
    triangleArray.push("triangle1");
    triangleArray.push("triangle2");
    triangleArray.push("triangle3");
    
    // attach and position the clips. because the array length is equal in both
    // we only need 1 'for' loop.  if using 2, make the next one different 
    // i.e. for(var j = 0; j<etc.. j++){  to avoid conflicts with the var value..
    for (var i = 0; i<circleArray.length; i++) {
    	// create a reference to the circle mc's and attach them
    	var circ = attachMovie(circleArray[i], circleArray[i], i);
    	// create a ref for the triangles, to avoid attaching on same depth, add 10 to current i
    	var tri = attachMovie(triangleArray[i], triangleArray[i], i+10);
    	// position circ
    	circ._x = 0;
    	circ._y = (100*i);
    	// position tri
    	tri._x = 0
    	tri._y = (100*i);
    	// set the mask.
    	circ.setMask(tri);
    	// because circ and tri are the refernces for the clips, each
    	// time we cycle through the loop, we're masking circleArray[i] with trangleArray[i], 
    }
    Please DO NOT PM me simply for individual help. Post in the public forums...that's what they are for, and they allow others to benefit as well. - Thx
    OS X 10.3.9 || Flash MX04 Pro
    | del.icio.us || furl || Helpful Source Files || Free Pixel Fonts |

  5. #5
    Junior Member
    Join Date
    Jul 2006
    Posts
    5

    Masking with actionscript

    Thanks Madzigian. It worked perfectly.

    One quick question, if I may.

    How is a person supposed to learn this stuff? I have books - Actionscript for Flash MX (Colin Moock), Actionscript Cookbook (Joey Lott), Flash Hacks (Sham Bangal), but none of them discussed this. What book would you recommend? At my level, I would never have figured this out on my own.

    Thanks again. Your input was very, very helpful.

  6. #6
    Can't Re-Member madzigian's Avatar
    Join Date
    Apr 2004
    Location
    Boston MA
    Posts
    2,662
    How is a person supposed to learn this stuff?
    Which stuff? I figure a lot out from trial & error... And just sort of pick up little tid bits here and there over time.

    I'm really not the best person to consult about how best to learn flash... I've been teaching myself for almost 2 years. i'm afraid i can't recommend any Flash books... I've never opened one. I just used to browse through the forums and read interesting sounding threads, pick apart source files to figure out how they worked, and eventually some of the logic started to make sense. I also am somewhat of an insomniac, so i spent A LOT of nights just playing around and trying things out. If i got stuck, I'd post here. The people around here are very helpful, especially when they can see you are putting in the effort.

    I was surprised, (but more impressed) to see that you were actually trying to work out your problems yourself, and had the code to show you had been. And you got damn close... both times!!! So many people sign up here and start demanding that someone "do this" and "do that" FOR them... It's nice to see someone interested in actually learning!!

    As far as how to go about learning... this thread will give you some more info :

    http://board.flashkit.com/board/showthread.php?t=699670

    Good Luck!!! And your welcome!!
    Please DO NOT PM me simply for individual help. Post in the public forums...that's what they are for, and they allow others to benefit as well. - Thx
    OS X 10.3.9 || Flash MX04 Pro
    | del.icio.us || furl || Helpful Source Files || Free Pixel Fonts |

  7. #7

  8. #8
    Can't Re-Member madzigian's Avatar
    Join Date
    Apr 2004
    Location
    Boston MA
    Posts
    2,662
    oh yea... just another quick comment:
    I'm not sure how exactly how 'new' you are to ActionScript... But in my opinion, from what i can tell from the code samples you posted, you doing pretty damn well so far. You don't seem like you're afraid to just jump in and go for it and you seem to have a knack for the logic of it. With those things weighing greatly in your favor, you'll be writing complex code and custom classes in your sleep before you know it!
    Please DO NOT PM me simply for individual help. Post in the public forums...that's what they are for, and they allow others to benefit as well. - Thx
    OS X 10.3.9 || Flash MX04 Pro
    | del.icio.us || furl || Helpful Source Files || Free Pixel Fonts |

  9. #9
    Junior Member
    Join Date
    Jul 2006
    Posts
    5
    Madzigian.

    bwah, hah, hah...... where's a good laughing smiley. - this will have to do.

    Read the thread link you posted here. I just posted there. Turns out, I do have some background. I am actually quite ordinary, with a greater than average drive, I think..... or I just think too much......whatever.

    The code I wrote in my questions was such that I wanted it to be as simple as possible so that I could understand what my problem was/is - if that makes any sense. It's masking that I was/am not understanding, as well as scoping. Scoping is a major issue it seems. Anyways, I don't want you to think I am some kind of gifted programmer.

    You know, I have more questions regarding the coding issues in this thread. One thing leads to another. For example, I want to mask MOVING clips dymanically loaded from an array. eg. images that enter from offstage. I can move the clips ok., but the mask does not work. Then I also want to try making these moving clips a button and using a rollover action to apply a mask (eg. a ripple effect) to the button - have not succeded with this yet - the masking part. Although I can mask it with the timeline.

    I want to write more code. The problem is, I have an agenda with the book I am studying AND I want to build a site. I find that if I go off on my own ( site planning/writing code), I SEEM to waste a lot of time, that could be better spent reading/studying more code. Eventually, I am hoping writing code falls in place - so to speak. In the meantime, I figure that looking at and analizing all that code has got to rub off - at least in my dreams anyway.

    I also don't know how much to ask of people here. It seems like questons could go on forever, if you know what I mean. When should I stop asking......that's also an the issue ....

    So much to learn, so little time.....

    later...

    Michon

  10. #10
    Can't Re-Member madzigian's Avatar
    Join Date
    Apr 2004
    Location
    Boston MA
    Posts
    2,662
    well, as far as when to stop asking questions in general.. never stop. In relation to the forums, it's a different story. First, don't ask questions until you've thoroughly searched the forums for an answer, done some google searches, and really made an effort to find some info. (You'll notice the same questions get asked around here OVER AND OVER AND OVER!!!) Answering those same questions wastes everyone's time. Usually you can get an answer faster by doing a few searches, than having to wait for someone to respond.

    If your searches are unsuccessful... Then post. Basically, when you start a new thread, you are usually asking for an answer to one question, or a few that all relate. Once the initial questions have been answered... Mark the thread resolved. If you have more questions that develop further down the line, then start a new thread. It's easy to get off the original topic. But keeping things on topic makes it easier to find solutions when searching, and keeps things from going on and on forever.

    On the other hand... if your question was answered, but you still don't understand.. then ask away.

    But basically, if you're asking too many questions.. you'll know because eventually people will stop responding to them. That means, move on. I am guilty of doing that a lot. People will start asking things like "would this work?" and "if i do this, can i make this happen"? And my response to that is..."There's an easy and faster way to find out if it works.... TRY IT! If it doesn't work, try something else!!" You get more out of trying things unsuccessfully than you do from waiting for someone to provide a working solution.


    as for the rest of your comments, you're really into this adding clips from an array thing huh?.. mix it up a little and try dynamically creating them. Try to draw circles, squares, and triangles using the drawing API.

    oh.. i little tip: You can only use one mc to mask another mc. So to use multiple mc's as a mask, (or to mask multiple mc's) you just stick them inside a single clip and use that clip as the mask (or mask that clip)
    Please DO NOT PM me simply for individual help. Post in the public forums...that's what they are for, and they allow others to benefit as well. - Thx
    OS X 10.3.9 || Flash MX04 Pro
    | del.icio.us || furl || Helpful Source Files || Free Pixel Fonts |

  11. #11
    Junior Member
    Join Date
    Jul 2006
    Posts
    5
    Hey Madzigian.

    Thread Resolved.

    Thanks for the excellent tips. The "unspoken" rules sometimes have to be articulated regardless.

    Guess what? I got the other issues I mentioned with the arrays working. It feels great. . Thanks for all your support.

    A person could spend all day on this forum there is so much going on. Anyways, see you lator...

    Michon

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