Author Topic: Flash & ActionScript Help  (Read 571 times)

0 Members and 1 Guest are viewing this topic.

Offline Sandwich

  • Got Screen?
  • 213
    • Skype
    • Steam
    • Twitter
    • Brainzipper
Flash & ActionScript Help
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.
SERIOUSLY...! | {The Sandvich Bar} - Rhino-FS2 Tutorial | CapShip Turret Upgrade | The Complete FS2 Ship List | System Background Package

"...The quintessential quality of our age is that of dreams coming true. Just think of it. For centuries we have dreamt of flying; recently we made that come true: we have always hankered for speed; now we have speeds greater than we can stand: we wanted to speak to far parts of the Earth; we can: we wanted to explore the sea bottom; we have: and so  on, and so on: and, too, we wanted the power to smash our enemies utterly; we have it. If we had truly wanted peace, we should have had that as well. But true peace has never been one of the genuine dreams - we have got little further than preaching against war in order to appease our consciences. The truly wishful dreams, the many-minded dreams are now irresistible - they become facts." - 'The Outward Urge' by John Wyndham

"The very essence of tolerance rests on the fact that we have to be intolerant of intolerance. Stretching right back to Kant, through the Frankfurt School and up to today, liberalism means that we can do anything we like as long as we don't hurt others. This means that if we are tolerant of others' intolerance - especially when that intolerance is a call for genocide - then all we are doing is allowing that intolerance to flourish, and allowing the violence that will spring from that intolerance to continue unabated." - Bren Carlill

  

Offline IceFire

  • GTVI Section 3
  • 212
    • http://www.3dap.com/hlp/hosted/ce
Flash & ActionScript Help
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
- IceFire
BlackWater Ops, Cold Element
"Burn the land, boil the sea, you can't take the sky from me..."

 

Offline Xelion

  • 28
  • In the Ether
Flash & ActionScript Help
There are a few ways to do this but AFAIK so far you can use the MovieClip.setMask() 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 :).

 

Offline Sandwich

  • Got Screen?
  • 213
    • Skype
    • Steam
    • Twitter
    • Brainzipper
Flash & ActionScript Help
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
SERIOUSLY...! | {The Sandvich Bar} - Rhino-FS2 Tutorial | CapShip Turret Upgrade | The Complete FS2 Ship List | System Background Package

"...The quintessential quality of our age is that of dreams coming true. Just think of it. For centuries we have dreamt of flying; recently we made that come true: we have always hankered for speed; now we have speeds greater than we can stand: we wanted to speak to far parts of the Earth; we can: we wanted to explore the sea bottom; we have: and so  on, and so on: and, too, we wanted the power to smash our enemies utterly; we have it. If we had truly wanted peace, we should have had that as well. But true peace has never been one of the genuine dreams - we have got little further than preaching against war in order to appease our consciences. The truly wishful dreams, the many-minded dreams are now irresistible - they become facts." - 'The Outward Urge' by John Wyndham

"The very essence of tolerance rests on the fact that we have to be intolerant of intolerance. Stretching right back to Kant, through the Frankfurt School and up to today, liberalism means that we can do anything we like as long as we don't hurt others. This means that if we are tolerant of others' intolerance - especially when that intolerance is a call for genocide - then all we are doing is allowing that intolerance to flourish, and allowing the violence that will spring from that intolerance to continue unabated." - Bren Carlill

 

Offline Xelion

  • 28
  • In the Ether
Flash & ActionScript Help
Heres the mouse-based one (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 if your thinking of getting more familar with flash.