Hard Light Productions Forums

Off-Topic Discussion => General Discussion => Topic started by: Sandwich on April 18, 2005, 02:30:25 am

Title: Flash & ActionScript Help
Post by: Sandwich on April 18, 2005, 02:30:25 am
I'm starting to mess around with Flash at work (2004 MX), and I've run across a question easier to get answered here by people who know Flash then by searching the web. :p

I have an FLA where I have an image in the background, and a rectangle stretching from top to bottom in a layer above the image, set as the mask for the image. Thus, only the part of the image "covered" by the mask rectangle shows through. Along the timeline, I've set keyframes for the rectangle, positioned it differently in those keyframes, and motion tweened it so that it moves nicely from one keyframe to the next.

However, I'd like to be able to have an actionscript that will automatically make the rectangle move in an erratic fashion along the horizontal. Something along the lines of the following pseudo-code:



// set 3 random keyframes
$targetframe[1] = rand(0, last_frame);
$targetframe[2] = rand(0, last_frame);
$targetframe[3] = rand(0, last_frame);

function moveRectangle($frame) {
$speed = rand(1, 5); // number of pixels to shift each repositioning
$dir = rand(+, -); // set direction of movement along x axis, plus or minus
while (frame.current < $frame) {
if (rectangle.position.x < 1) $dir = "+"; // if the rectangle is beyond the left border, change directions
if (rectangle.position.x > movie.width) $dir = "-"; // if the rectangle is beyond the right border, change directions
if ($dir == "+") {
rectangle.position.x = rectangle.position.x + $speed;
} else {
rectangle.position.x = rectangle.position.x - $speed;
}

}
}

moveRectangle($targetframe[1]);
moveRectangle($targetframe[2]);
moveRectangle($targetframe[3]);



Wow, that turned out to be more complex than I thought. I hope it's understandable.
Title: Flash & ActionScript Help
Post by: IceFire on April 18, 2005, 08:05:29 am
I'd have to sit down and think about it but you look like you're on the right track.  I'm not sure if I wrote it it'd do it that way and my ActionScript abilities are far less than yours so take me for a grain of salt :D
Title: Flash & ActionScript Help
Post by: Xelion on April 18, 2005, 10:31:18 am
There are a few ways to do this but AFAIK so far you can use the MovieClip.setMask() (http://www.macromedia.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary566.html) method and you could do it with minimal actionscript but I have no idea how as yet (working on it :D). I'll upload a mouse-base one later :).
Title: Flash & ActionScript Help
Post by: Sandwich on April 19, 2005, 05:14:47 pm
Quote
Originally posted by IceFire
..and my ActionScript abilities are far less than yours so take me for a grain of salt :D


They've got to be bad to mistake my pseudo-PHP code for ActionScript. :p I don't know the first thing about AS. :D
Title: Flash & ActionScript Help
Post by: Xelion on April 21, 2005, 04:08:54 am
Heres the mouse-based one (http://home.iprimus.com.au/nexus387/forums/flashmask_files.rar) (2MB) + .mov on how to create it :). Instead of using the mouse's X position you can change the code on frame 7 of the Actions layer from:

Code: [Select]
rectangle._x = _xmouse;

to

Code: [Select]
rectangle._x = Math.random()*470;

The Math.random() method generates a number between 0 and 1. In this example I've multiplied that number by 470 since you didn't want the rectangle mask going over the edge of the movie.

This might not be the path you need to take to create what your looking for with flash, but it can help. I've listed some Flash Urls below that have some masking tutorials.


http://www.kirupa.com/developer/mx2004/masks2.htm
http://www.actionscript.org/tutorials/advanced/Dynamic_masking_using_the_drawing_API/index.shtml
http://www.actionscript.org/tutorials/advanced/Dynamic_masking_using_the_drawing_API_II/index.shtml
http://www.entheosweb.com/Flash/masking.asp

There are also some great Flash books at friends of ED (www.friendsofed.com/) if your thinking of getting more familar with flash.