PDA

View Full Version : Using tweens with switch and if statements



RRebel
March 10th, 2009, 05:33 PM
In this example, is there anyway to fade in and out 'mcContactAmer' and 'mcY' as well as place them at specific coordinates, other than externally load them as swfs? This is the only thing holding me back from moving forward on this project and my boss would like to move forward, so any help would be greatly appreciated.

This is what my code looks like:

import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.geom.Point;

var goCompany:Point = new Point(-365, -471);
var goSvs:Point = new Point(-1085, -1078);
var goContact:Point = new Point(-967, -116);
var goHome:Point = new Point(-512.8, -1045.6);

var currentRequest:Class;
var loader:MovieClip;
var fin = Tween = new Tween (loader, "alpha", Strong.easeOut, 1, 0, 10, true);

btn_company.addEventListener(MouseEvent.CLICK, contentLoader);
btn_home.addEventListener(MouseEvent.CLICK, contentLoader);


function contentLoader(e:MouseEvent):void {
switch(e.target.name) {
case "btn_company":
currentRequest = mcContactAmer;
new Tween (mc_map, 'x', null, mc_map.x, goCompany.x, 1.2, true);
new Tween (mc_map, 'y', null, mc_map.y, goCompany.y, 1.2, true);
break;
case "btn_home":
currentRequest = mcY;
new Tween (mc_map, 'x', null, mc_map.x, goHome.x, 1.2, true);
new Tween (mc_map, 'y', null, mc_map.y, goHome.y, 1.2, true);
break;
}
loadIt();
}

function loadIt():void {
if(loader != null) {
removeChild(loader);
}
loader = new currentRequest;
addChild(loader);
}

snickelfritz
March 10th, 2009, 08:42 PM
There's a trick often used to create "active" button states, that can be used here very effectively.
Here's the concept:
Basically, you toggle the values of two variables when a button is clicked.
Those variables hold references to the current target and previous target.
In this case, you assign the values from an array of info clips, which correlate with their respective button id numbers.

basic procedure:
Create two empty movieclip variables:


var currentInfo:MovieClip;
var previousInfo:MovieClip;

These variables will hold references to the current and previous info movieclips.
In your click function, you assign currentInfo as the movieclip stored in index of the infoArray, using e.target.id. (which will have been assigned to each button in an initialization loop)
Something like this:


var infoArray:Array =
[
map_mc.info1,
map_mc.info2,
map_mc.info3,
map_mc.info4
];

function btnClick(e:MouseEvent):void
{
previousInfo.alpha = 0; // existing infoclip is made invisible
currentInfo = infoArray[e.target.id]; // grab the corresponding infoclip from the infoArray
currentInfo.alpha = 1; // make the current infoclip visible
currentInfo = previousInfo; // convert current infoclip to "previousInfo";
}


Nest the info clips inside of the map_mc at their respective locations on the map: where they should be when that section is loaded.
This has a fairly significant upside: the info clips have xy properties that can potetnially be used as targets when moving the map_mc.
ie: When I click button1, send the map to -info1 xy coordinates and set the alpha for the currentInfo to 1;

I'll try to assemble a functioning example later tonight, but this is the basic concept I think I would use.
Not only is it fairly straightforward to set up, it is also very easy to extend, since no arcane point data has to be calculated; just place the info clips on the map and add them to the array.