Get current url to Flash swf using an External Interface call

Update: please see the newer tut talking about getting the current url with as3

Overview:

Many have struggled with the task of getting you swf to read or get the current url showing in the browser, the html page the browser is at which has the swf embedded. Not to be confused with the _root._url which returns the path of the swf file. This would be helpful to know if someone is embedding your swfs on their site, or even customize your swf depending on which page it resides on. There is a pretty simple, yet virtually undocumented way to do this. We have to use javascript by calling External Interface and get the url from the DOM. Window.location.href. Then we must call the toString() function only because ExternalInterface.call requires a function rather than only reading a static property.

Steps:

  1. Import external interface into your file: import flash.external.ExternalInterface;

  2. Initialize a variable to store the url path: var urlPath;

  3. Create a function to call external interface and assign the html page path to your variable: urlPath = ExternalInterface.call(“window.location.href.toString”);

  4. Call the function when/if needed.

Example:

With javascript: window.location.href or window.location.href.toString();
With actionscript: ExternalInterface.call(“window.location.href.toString”);
External Interface html Example
[kml_flashembed publishmethod=”dynamic” fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2008/01/externalinterface.swf” width=”550″ height=”200″ targetclass=”flashmovie”]

Get Adobe Flash player

[/kml_flashembed]
Actionscript:
[cc lang=”actionscript” tab_size=”2″ lines=”40″]
import flash.external.*;
var urlPath;

function geturlhttp() {
urlPath = ExternalInterface.call(“window.location.href.toString”);
}
geturlhttp();
//Here I assign the url to a text box on the stage
_root.urlText.text = urlPath;

[/cc]

Download:

ExternalInterface.zip

PHP Menu Include function to reuse an html block on multiple pages

Overview:
When making websites frequently we want a navigation bar or menu to show on every page. Rather than repeating the code on every single page, which is virtually impossible to update and maintain, or worse, using frames, use PHP to automate this and build each page dynamically. PHP will just paste in the navigation or whatever you’d want represented on every page. To do this we use the php include function. You can use include() to show headers, footers, or any elements that you’d reuse on multiple pages. The include() function takes all the text in a specified file and copies it into the file that calls the include function. This is great for scalability and updating- instead of changing multiple files, only change one!
Here’s what a php include function looks like:
<?php include(“nav.php”); ?>

Steps:
1. Make the actual nav you want.

2. Put it into a php file (or any type of file, html, txt, as long as it’s formatted as html)

3. Place the php in your page to ‘print’ the desired file into your final html page before the browser renders the page using the include function. This spits out html. The viewer’s only see html, as that’s all it is, the php created the html file- That’s why it’s called PHP: Hypertext Preprocessor.

Example:
The nav.php file:
[cc lang=”php” tab_size=”2″ lines=”40″]

Home |
About Us |
Portfolio |
Contact Us

[/cc]

The homepage.php:
[cc lang=”php” tab_size=”2″ lines=”40″]
<?php include(“nav.php”); ?>

Welcome to my home page

home page text lorem ipsum
[/cc]

And here’s a sample site with a nav bar containing 4 pages included through php for Home, About, Portfolio and Contact.

Download

phpNav.zip

NavBar Menu List with CSS

The Overview:

An interactive navigation bar using CSS with rounded corners. Just put the html as an unordered list and your navigation links as list items and the CSS will do the rest! As shown here! The css will tell the browser to display this special unordered list as the menu. This utilizes a technique to have one background image with three states: normal, hover and active. The css controls the placement of the background image so when you hover, the images is displaced to show only the hover state, and likewise for the active and then returns to the normal state. This is compatible in all the browsers I have checked so far, let me know if you find any issues [thanks].
vertical nav menu screenshot

The Steps:

1. First make the html. An <ul> full of <li> elements.

2. Make the desired backgrounds. Three part background (or two) . One part ‘Normal’ state and another ‘hover’ state and optionally an ‘active’ state. Add additional backgrounds if you want anything like rounded corners, or just a header.
menuTop
Top Menu Background Image
menuMiddle
Middle Menu Background Image
menuBottom
Bottom Menu Background Image

3. The CSS to attach the two together

The Example:

verticalMenuCSS.html

As you can see this is a simple html unordered list with list items.

[cc lang=”html”]

  • My Site
  • Home
  • About
  • Biography
  • Portfolio
  • Contact

[/cc]

Here’s the magic.

[cc lang=”css”]
/*vertical Menu CSS style sheet*/

*{
list-style:none;
margin:0px;
padding:0px;
font-family: Arial, Helvetica, sans-serif;
font-size: 1em;
}

.sidemenu {
width: 150px;
border: none;
}
.sidemenu li a {
height: 18px;
text-decoration: none;
}
/*Top sidemenu Item (Header and rounded corners)*/
.sidemenu li.top a:link, .sidemenu li.top a:visited {
color: #828282;
display: block;
background: url(images/menuTop.png);
background-repeat:no-repeat;
padding: 1px 0 1px 0;
font-size: 1em;
font-weight: bold;
text-align: center;
}
/* Uncomment this if you want the header to be an interactive link
.sidemenu li.top a:hover {
color: #828282;
background: url(images/menuTop.png) 0 -22px;
}
.sidemenu li.top a:active {
color: #26370A;
background: url(images/menuTop.png) 0 -44px;
}*/

/*sidemenu Item Middle*/
.sidemenu li.middle a:link, .sidemenu li.middle a:visited {
color: #5E7830;
display: block;
background: url(images/menuMiddle.png);
background-repeat:no-repeat;
padding: 1px 0 1px 20px;
font-size: 0.8em;
}
.sidemenu li.middle a:hover {
color: #FFFFFF;
background: url(images/menuMiddle.png) 0 -22px;
}
.sidemenu li.middle a:active {
color: #FFFFFF;
background: url(images/menuMiddle.png) 0 -44px;
}

/*Bottom sidemenu Item (rounded corners)*/
.sidemenu li.bottom a:link, .sidemenu li.bottom a:visited {
color: #5E7830;
display: block;
background: url(images/menuBottom.png);
background-repeat:no-repeat;
padding: 1px 0 1px 20px;
font-size: 0.8em;
}
.sidemenu li.bottom a:hover {
color: #FFFFFF;
background: url(images/menuBottom.png) 0 -22px;
}
.sidemenu li.bottom a:active {
color: #FFFFFF;
background: url(images/menuBottom.png) 0 -44px;
}
[/cc]

verticalMenuCSS.html

The Download:

verticalMenuCSS.zip

Dynamic Flash Vertical Scrolling Link List with XML

As seen at activeden. I’ve fixed it up a lot and made it much easier to incorporate into your own files.

A link list. Vertically scrolling list of links or just words. Scrolls and wraps automatically and interactively. Reads an external XML file containing just titles and paths and creates an interactive click-able link list!

[kml_flashembed publishmethod=”dynamic” fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2007/08/linklist.swf” width=”550″ height=”500″ targetclass=”flashmovie”]

Get Adobe Flash player

[/kml_flashembed]

Download the source files (linkList.fla, cats.xml, linkList.swf): Link List at activeden.

Other Circlecube Items at activeden

21075 24687 45713 45893 22018

Rollover elastic bounce rezise

Flash experiment that could be used for navigation. Rollover each area and watch them all resize themself to accomodate the growing box. It’s pretty fun to play with.

Example

[kml_flashembed publishmethod=”dynamic” fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2007/07/rolloverresizeevan.swf” width=”550″ height=”400″ targetclass=”flashmovie”]

Get Adobe Flash player

[/kml_flashembed]
I’m sure there is a way to simplify the code, but this worked so there was no need.

Actionscript

[cc lang=”actionscript” tab_size=”2″ lines=”70″]
import mx.transitions.easing.*;
import mx.transitions.Tween;
var normWidth:Number = mc_1._width;
mc1x = mc_1._x;
mc2x = mc_2._x;
mc3x = mc_3._x;
mc4x = mc_4._x;
mc_1.onRollOver = function ():Void {
//Tween( obj:Object, prop:String, func:Function, begin:Number, finish:Number, duration:Number, useSeconds:Boolean )
//Double in size
var mc1Width:Tween = new Tween (mc_1, “_width”, Elastic.easeOut, normWidth, normWidth * 2, 1, true);
mc_1.gotoAndPlay(“over”);
//Shrink by 33% and move 66% along using width
var mc2Width:Tween = new Tween (mc_2, “_width”, Elastic.easeOut, normWidth, normWidth * 2 / 3, 1, true);
var mc2X:Tween = new Tween (mc_2, “_x”, Elastic.easeOut, mc2x, (mc2x) + (normWidth * 2 / 3), 1, true);
//Shrink by 33% and move 33% along using width
var mc3Width:Tween = new Tween (mc_3, “_width”, Elastic.easeOut, normWidth, normWidth * 2 / 3, 1, true);
var mc3X:Tween = new Tween (mc_3, “_x”, Elastic.easeOut, mc3x, (mc3x) + (normWidth * 1 / 3), 1, true);
//Shrink by 33%, no need to move as already in position
var mc4Width:Tween = new Tween (mc_4, “_width”, Elastic.easeOut, normWidth, normWidth * 2 / 3, 1, true);
//trace(“rollOver 1”);
};
mc_1.onRollOut = function ():Void {
//Return to normal width
var mc1Move:Tween = new Tween (this, “_width”, Elastic.easeOut, normWidth * 2, normWidth, 1, true);
mc_1.gotoAndStop(“up”);
//Return to normal width and use mc_1 as basis for x
var mc2Width:Tween = new Tween (mc_2, “_width”, Elastic.easeOut, normWidth * 2 / 3, normWidth, 1, true);
var mc2X:Tween = new Tween (mc_2, “_x”, Elastic.easeOut, (mc2x) + (normWidth * 2 / 3), mc2x, 1, true);
//var mc2X:Tween = new Tween (mc_2, “_x”, Elastic.easeOut, mc_2._x, mc_1._x + (normWidth*2), 1, true);
//Return to normal width and use mc_4 as basis for x
var mc3Width:Tween = new Tween (mc_3, “_width”, Elastic.easeOut, normWidth * 2 / 3, normWidth, 1, true);
var mc3X:Tween = new Tween (mc_3, “_x”, Elastic.easeOut, (mc3x) + (normWidth * 1 / 3), mc3x, 1, true);
//Return to normal width
var mc4Width:Tween = new Tween (mc_4, “_width”, Elastic.easeOut, normWidth * 2 / 3, normWidth, 1, true);
//trace(“rollOut 1”);
};
mc_2.onRollOver = function ():Void {
//Double in size and move left 33%
var mc2Width:Tween = new Tween (mc_2, “_width”, Elastic.easeOut, normWidth, normWidth * 2, 1, true);
var mc2X:Tween = new Tween (mc_2, “_x”, Elastic.easeOut, mc2x, (mc2x) + (normWidth * 2 / 3), 1, true);
mc_2.gotoAndPlay(“over”);
//Tween( obj:Object, prop:String, func:Function, begin:Number, finish:Number, duration:Number, useSeconds:Boolean )
//shrink by 33% mc1
var mc1Width:Tween = new Tween (mc_1, “_width”, Elastic.easeOut, normWidth, normWidth * 2 / 3, 1, true);
//var mc2X:Tween = new Tween (mc_2, “_x”, Elastic.easeOut, mc_2._x, (mc_2._x) + (mc_2._width*2/3), 1, true);
//Shrink by 33% and move 33% along using width
var mc3Width:Tween = new Tween (mc_3, “_width”, Elastic.easeOut, normWidth, normWidth * 2 / 3, 1, true);
var mc3X:Tween = new Tween (mc_3, “_x”, Elastic.easeOut, mc3x, (mc3x) + (normWidth * 1 / 3), 1, true);
//Shrink by 33%, no need to move as already in position
var mc4Width:Tween = new Tween (mc_4, “_width”, Elastic.easeOut, normWidth, normWidth * 2 / 3, 1, true);
//trace(“rollOver 2”);
};
mc_2.onRollOut = function ():Void {
//Return to normal width and position
var mc2Move:Tween = new Tween (this, “_width”, Elastic.easeOut, normWidth * 2, normWidth, 1, true);
var mc2X:Tween = new Tween (mc_2, “_x”, Elastic.easeOut, (mc2x) + (normWidth * 2 / 3), mc2x, 1, true);
mc_2.gotoAndStop(“up”);
//Return to normal width and use mc_1 as basis for x
var mc1Width:Tween = new Tween (mc_1, “_width”, Elastic.easeOut, normWidth * 2 / 3, normWidth, 1, true);
//var mc2X:Tween = new Tween (mc_2, “_x”, Elastic.easeOut, mc_2._x, mc_1._x + (normWidth*2), 1, true);
//Return to normal width and use mc_4 as basis for x
var mc3Width:Tween = new Tween (mc_3, “_width”, Elastic.easeOut, normWidth * 2 / 3, normWidth, 1, true);
var mc3X:Tween = new Tween (mc_3, “_x”, Elastic.easeOut, (mc3x) + (normWidth * 1 / 3), mc3x, 1, true);
//Return to normal width
var mc4Width:Tween = new Tween (mc_4, “_width”, Elastic.easeOut,normWidth * 2 / 3, normWidth, 1, true);
//trace(“rollOut 2”);
};
mc_3.onRollOver = function ():Void {
//Double in size and move left 33%
var mc3Width:Tween = new Tween (mc_3, “_width”, Elastic.easeOut, normWidth, normWidth * 2, 1, true);
var mc3X:Tween = new Tween (mc_3, “_x”, Elastic.easeOut, mc3x, (mc3x) + (normWidth * 1 / 3), 1, true);
mc_3.gotoAndPlay(“over”);
//Tween( obj:Object, prop:String, func:Function, begin:Number, finish:Number, duration:Number, useSeconds:Boolean )
//shrink by 33% mc1
var mc1Width:Tween = new Tween (mc_1, “_width”, Elastic.easeOut, normWidth, normWidth * 2 / 3, 1, true);
//var mc2X:Tween = new Tween (mc_2, “_x”, Elastic.easeOut, mc_2._x, (mc_2._x) + (mc_2._width*2/3), 1, true);
//Shrink by 33% and move 33% along using width
var mc2Width:Tween = new Tween (mc_2, “_width”, Elastic.easeOut, normWidth, normWidth * 2 / 3, 1, true);
var mc2X:Tween = new Tween (mc_2, “_x”, Elastic.easeOut, mc2x, (mc2x) + (normWidth * -2 / 3), 1, true);
//Shrink by 33%, no need to move as already in position
var mc4Width:Tween = new Tween (mc_4, “_width”, Elastic.easeOut, normWidth, normWidth * 2 / 3, 1, true);
//trace(“rollOver 3”);
};
mc_3.onRollOut = function ():Void {
//Return to normal width and position
var mc3Move:Tween = new Tween (this, “_width”, Elastic.easeOut, normWidth * 2, normWidth, 1, true);
var mc3X:Tween = new Tween (mc_3, “_x”, Elastic.easeOut, (mc3x) + (normWidth * 1 / 3), mc3x, 1, true);
mc_3.gotoAndStop(“up”);
//Return to normal width and use mc_1 as basis for x
var mc1Width:Tween = new Tween (mc_1, “_width”, Elastic.easeOut, normWidth * 2 / 3, normWidth, 1, true);
//var mc2X:Tween = new Tween (mc_2, “_x”, Elastic.easeOut, mc_2._x, mc_1._x + (normWidth*2), 1, true);
//Return to normal width and use mc_4 as basis for x
var mc2Width:Tween = new Tween (mc_2, “_width”, Elastic.easeOut, normWidth * 2 / 3, normWidth, 1, true);
var mc2X:Tween = new Tween (mc_2, “_x”, Elastic.easeOut, (mc2x) + (normWidth * -2 / 3), mc2x, 1, true);
//Return to normal width
var mc4Width:Tween = new Tween (mc_4, “_width”, Elastic.easeOut, normWidth * 2 / 3, normWidth, 1, true);
//trace(“rollOut 3”);
};
mc_4.onRollOver = function ():Void {
//shrink in size and move left 33%
var mc3Width:Tween = new Tween (mc_3, “_width”, Elastic.easeOut, normWidth, normWidth * 2/3, 1, true);
var mc3X:Tween = new Tween (mc_3, “_x”, Elastic.easeOut, mc3x, (mc3x) + (normWidth * – 3 / 3), 1, true);
//Tween( obj:Object, prop:String, func:Function, begin:Number, finish:Number, duration:Number, useSeconds:Boolean )
//shrink by 33% mc1
var mc1Width:Tween = new Tween (mc_1, “_width”, Elastic.easeOut, normWidth, normWidth * 2 / 3, 1, true);
//var mc2X:Tween = new Tween (mc_2, “_x”, Elastic.easeOut, mc_2._x, (mc_2._x) + (mc_2._width*2/3), 1, true);
//Shrink by 33% and move 33% along using width
var mc2Width:Tween = new Tween (mc_2, “_width”, Elastic.easeOut, normWidth, normWidth * 2 / 3, 1, true);
var mc2X:Tween = new Tween (mc_2, “_x”, Elastic.easeOut, mc2x, (mc2x) + (normWidth * -2 / 3), 1, true);
//double in size, no need to move as already in position
var mc4Width:Tween = new Tween (mc_4, “_width”, Elastic.easeOut, normWidth, normWidth * 2, 1, true);
mc_4.gotoAndPlay(“over”);
//trace(“rollOver 4”);
};
mc_4.onRollOut = function ():Void {
//Return to normal width and position
var mc3Move:Tween = new Tween (mc_3, “_width”, Elastic.easeOut, normWidth * 2/3, normWidth, 1, true);
var mc3X:Tween = new Tween (mc_3, “_x”, Elastic.easeOut, (mc3x) + (normWidth * – 3 / 3), mc3x, 1, true);
//Return to normal width and use mc_1 as basis for x
var mc1Width:Tween = new Tween (mc_1, “_width”, Elastic.easeOut, normWidth * 2 / 3, normWidth, 1, true);
//var mc2X:Tween = new Tween (mc_2, “_x”, Elastic.easeOut, mc_2._x, mc_1._x + (normWidth*2), 1, true);
//Return to normal width and use mc_4 as basis for x
var mc2Width:Tween = new Tween (mc_2, “_width”, Elastic.easeOut, normWidth * 2 / 3, normWidth, 1, true);
var mc2X:Tween = new Tween (mc_2, “_x”, Elastic.easeOut, (mc2x) + (normWidth * -2 / 3), mc2x, 1, true);
//Return to normal width
var mc4Width:Tween = new Tween (mc_4, “_width”, Elastic.easeOut, normWidth * 2, normWidth, 1, true);
mc_4.gotoAndStop(“up”);
//trace(“rollOut 4”);
};
[/cc]

Download

Here is the source file: rolloverResizeEvan.fla for those who want.

Dynamic Scrolling Buttons

[kml_flashembed publishmethod=”dynamic” fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2007/04/dynamicscrollingbuttons.swf” width=”550″ height=”400″ targetclass=”flashmovie”]

Get Adobe Flash player

[/kml_flashembed]
Here’s an example. A dynamic scroll, that changes speed according to your mouse. Here is the code for it as well, I tried to keep it pretty generic, just put this onto a movie clip I named “scroll.” And change the variables to fit your needs. Enjoy, and let me know what you make with it.

Actionscript (as2)

[cc lang=”actionscript” tab_size=”2″ lines=”40″]

onClipEvent(load) {
//variables
scrollMovieClipW = this._width – Stage.width;
leftScrollMargin = 175;
rightScrollMargin = 275;
verticalScrollMargin = 250;
//Note: The lower acceleration value the faster the scroll will be.
acceleration = 3;
}

onClipEvent (enterFrame) {
//to move left
//if mouse is right of 0 (left edge)
if (_root._xmouse >= 0 &&
//if mouse is left of left scroll margin
_root._xmouse <= leftScrollMargin && //if mouse is vertically below green line (over the scroll movie clip) _root._ymouse >= verticalScrollMargin &&
//if the scroll movie clip can still scroll further
_root.scroll._x <= 0) { this._x -= (_root._xmouse - leftScrollMargin) / acceleration; }//to move right else if (_root._xmouse >= rightScrollMargin &&
_root._xmouse <= Stage.width && _root._ymouse >= verticalScrollMargin &&
_root.scroll._x >= -scrollMovieClipW) {
//move right
this._x -= (_root._xmouse – rightScrollMargin) / acceleration;
}
}
[/cc]

Source

Download the example file: dynamicScrollingButtons.fla

Gravity

A variation of the gravity with throwable ball experiment. It has optional gravity.

Click the ball to drag and release to drop or throw it.
Press the space bar to add more balls (up to 30).
Press the down arrow to turn gravity off, and pres the up arrow to turn it back on.

You can make some pretty interesting movements. The source fla file is at the bottom of the post. I learned most of this from Keith Peter’s gravity tutorial.

[kml_flashembed publishmethod=”dynamic” fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2007/03/gravity07.swf” width=”550″ height=”550″ targetclass=”flashmovie”]

Get Adobe Flash player

[/kml_flashembed]

Actionscript (as2)

[cc lang=”actionscript” tab_size=”2″ lines=”40″]
//global variables initialized in frame 1
gravity=2;
drag=.99;
bounce=.8;
_root.id = 1;

//on the ball
onClipEvent (load) {
//random placement
this._x = random(Stage.width);
this._y = random(Stage.height);
//random x and y speed
this.xspeed = Math.random() * 30;
this.yspeed = Math.random() * 30;
//give this ball an id
this.ballNum = _root.id;
//increment id until 30 and then reset
if (_root.id < 30) {
_root.id++;
}
else {
_root.id = 2;
}
}
//drag ball on press
on (press) {
startDrag(this);
this.dragging = true;
}
//create new ball with space bar
on (keyPress “<Space>”) {
if (this.ballNum == 1) {
duplicateMovieClip(_root.ball, “ball” + _root.id, _root.id);
}
}
//toggle gravity with up and down arrow
on (keyPress “<Down>”) {
_root.gravity = 0;
}
on (keyPress “<Up>”) {
_root.gravity = 2;
}
//drop on release
on (release, releaseOutside) {
stopDrag();
this.dragging = false;
}
onClipEvent (enterFrame) {
//if not dragging
if (!this.dragging) {
//calculate new x and y position
this._y = this._y + this.yspeed;
this._x = this._x + this.xspeed;
//bounce off the bottom of stage and reverse yspeed
if (this._y + this._height / 2 > Stage.height) {
this._y = Stage.height – this._height / 2;
this.yspeed = -this.yspeed * _root.bounce;
}
//bounce off the top of stage and reverse yspeed
if (this._y – this._height / 2 < 0) {
this._y = this._height / 2;
this.yspeed = -this.yspeed * _root.bounce;
}
//bounce off right of stage
if (this._x + this._width / 2 > Stage.width) {
this._x = Stage.width – this._width / 2;
this.xspeed = -this.xspeed * _root.bounce;
}
//bounce off left of stage
if (this._x – this._width / 2 < 0) {
this._x = this._width / 2;
this.xspeed = -this.xspeed * _root.bounce;
}
//recalculate x and y speeds figuring in drag (friction) and gravity for y
this.yspeed = this.yspeed * _root.drag + _root.gravity;
this.xspeed = this.xspeed * _root.drag;
}
else {
//if dragging then calculate new speeds according to dragging movement and speed
this.xspeed = this._x – this.oldx;
this.yspeed = this._y – this.oldy;
this.oldx = this._x;
this.oldy = this._y;
}
}
[/cc]

Here is the gravity.fla to download and play with.

Drag Line

Actionscript exercise with a line connecting movable movieClips.

Flash Example

[kml_flashembed publishmethod=”dynamic” fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2007/02/dragline.swf” width=”550″ height=”550″ targetclass=”flashmovie”]

Get Adobe Flash player

[/kml_flashembed]

actionscript

[cc lang=”actionscript” tab_size=”2″ lines=”40″]
// get x and y coordinates of the circles
var ax = _root.point1._x;
var ay = _root.point1._y;
var bx = _root.point2._x;
var by = _root.point2._y;
var cx = _root.point3._x;
var cy = _root.point3._y;

// draw line between them
_root.createEmptyMovieClip (“line”, 0);
with (_root.line){
lineStyle (1, 0x999999, 100);
moveTo (ax, ay);
lineTo (bx, by);
lineTo (cx, cy);
lineTo (ax, ay);
}
[/cc]

Download

dragLine.fla

Multiple Targets

An actionscript exercise. Trying to get a movie clip to go to the closest dynamically created target. Click the button to make more targets. Dragable targets. Move the targets around to see the ball’s target change.

Example

[kml_flashembed publishmethod=”dynamic” fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2007/02/multipletargets.swf” width=”550″ height=”550″ targetclass=”flashmovie”]

Get Adobe Flash player

[/kml_flashembed]

actionscript

Here’s the actioscript for the little ball:
[cc lang=”actionscript” tab_size=”2″ lines=”40″]
onClipEvent (load) {
//random placement
_x = Math.random()* 450;
_y = Math.random()* 450;
//speed is speed, higher number is slower movement
speed = (Math.random()*5)+7;
//x and y distance from target
xdist = 0;
ydist = 0;
}

onClipEvent (enterFrame) {
//determine which target is closer a^2+b^2=c^2
//use a for loop to cycle through all targets,
//and assign the closest to be THE target
for (j=0; j <= _root.i; j++) {
//for the first automatically assign it as the closest
if (j == 0) {
xdist = _root.target0._x – _x;
ydist = _root.target0._y – _y;
}
//find the distance to next target
else {
//temporary distance variables
xdistT = _root[“target”+j]._x – _x;
ydistT = _root[“target”+j]._y – _y;
//if it is closer assign it to the closest, if not do nothing
if(Math.sqrt((xdist*xdist) + (ydist*ydist)) > Math.sqrt((xdistT*xdistT) + (ydistT*ydistT))) {
xdist = xdistT;
ydist = ydistT;
}
}
}

//move toward the selected target
_x += xdist/speed;
_y += ydist/speed;

//if target reached go to another random place
//if xdist is less than 2 and ydist is less than 2 and xdist is greater than 2 and ydist is greater than 2
if(xdist < 2 && ydist < 2 && xdist > -2 && ydist > -2) {
_x = Math.random() * 450;
_y = Math.random() * 450;
}
}
[/cc]

Source Flash File

download multipleTargets.fla