|
-
Translation! Please help!
I was wondering if someone could put this in as3 form for me.
I just can't quite figure it out.
Code:
onload(){
oxml = new XML();
oxml.ignoreWhite = true;
oxml.onload = function(loaded){
if(loaded){
buildMenu(this);
}else{
btn0.label.text = "ERROR: no xml data";
}
oxml.load("menu.xml");
}
}
function buildMenu(xmlObj){
var menuNode = xmlObj.firstChild;
for(i=0; i<menuNode.childNodes.length; i++){
var itemNode = menuNode.childNodes[i];
var menuItem = (i==0) ? btn0 : btn0.duplicateSprite("btn"+i, i);
menuItem.label.text = itemNode.attributes.label;
menuItem._link = itemNode.attributes.url;
menuItem._y += btn0._height * i;
menuItem.trigger.onRelease = function(){
this.getURL(this._link, "_blank");
}
}
}
Thanks in advance.
-
Senior Member
Almost got it translated, but I kinda need to see your XML to see how it's set up to translate this completely. Thanks.
-
Well I was trying to implement the code above to into the code below.
All I really need to know how to do is add action to the buttons, such as going to a url or external swf.
The function for this is at the bottom of the script.
Code:
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import caurina.transitions.Tweener;
import caurina.transitions.properties.ColorShortcuts;
ColorShortcuts.init();
public class FluidMenu extends Sprite {
private var _menuItem:MovieClip;
private var _menuContainer:MovieClip = new MovieClip();
private var _xml:XML;
private var _xmlList:XMLList;
private var _xmlLoader:URLLoader = new URLLoader();
private var _lastMenuItemWidth:Array = new Array();
private var _lastMenuItemX:Array = new Array();
private var _menuItems:Array = new Array();
private var settings:Array = new Array();
private var _menuItemDefaultX:Array = new Array();
private var _format:TextFormat;
public function FluidMenu()
{
_xmlLoader.load(new URLRequest("data/fluidmenu.xml"));
_xmlLoader.addEventListener(Event.COMPLETE, constructMenu);
}
private function constructMenu(event:Event):void
{
_xml = XML(event.target.data);
_xmlList = _xml.menu.item;
settings['positionX'] = _xml.settings.positionX;
settings['positionY'] = _xml.settings.positionY;
settings['normalColor'] = "0x"+_xml.settings.normalColor.substr(1,6);
settings['overColor'] = "0x"+_xml.settings.overColor.substr(1,6);
settings['overZoomRatio'] = _xml.settings.overZoomRatio;
for(var i:int = 0; i < _xmlList.length(); i++)
{
_menuItem = new Button();
_format = new TextFormat();
_format.color = settings['normalColor'];
_menuItem._butTxt.autoSize = TextFieldAutoSize.LEFT;
_menuItem._butTxt.defaultTextFormat = _format;
_menuItem._butTxt.text = _xmlList[i];
_menuItem.fakeOver.x = _menuItem._butTxt.x;
_menuItem.fakeOver.width = _menuItem._butTxt.textWidth+4;
_menuItem.initialWidth = _menuItem.width;
_lastMenuItemWidth.push(_menuItem.width);
_menuItem.addEventListener(MouseEvent.MOUSE_OVER, mouseOverButton);
_menuItem.addEventListener(MouseEvent.MOUSE_OUT, mouseOutButton);
_menuItem.addEventListener(MouseEvent.CLICK, mouseOnButton);
_menuItem._link = _menuItem.attributes.url;
if(i!=0){_menuItem.x = _lastMenuItemX[i-1]+_lastMenuItemWidth[i-1];}
_lastMenuItemX.push(_menuItem.x);
_menuItem.name = _xmlList[i];
_menuItem.index = i;
_menuItem.buttonMode = true;
_menuItems.push(_menuItem.name);
_menuItemDefaultX.push(_menuItem.x);
_menuContainer.addChild(_menuItem);
}
if(settings['positionX'] == "CENTER") {
_menuContainer.x = stage.stageWidth/2 - _menuContainer.width/2;
}
else
{
_menuContainer.x = int(settings['positionX']);
}
if(settings['positionY'] == "CENTER") {
_menuContainer.y = stage.stageHeight/2 - _menuContainer.height/2;
}
else
{
_menuContainer.y = int(settings['positionX']);
}
addChild(_menuContainer);
}
private function mouseOverButton(event:MouseEvent):void
{
Tweener.addTween(event.currentTarget, {scaleX:settings['overZoomRatio'], scaleY:settings['overZoomRatio'], time:.5});
Tweener.addTween(event.currentTarget._butTxt, {_color:settings['overColor'], time:.5});
for(var i:int = 0; i < _menuItems.length; i++)
{
if(event.currentTarget != _menuContainer.getChildByName(_menuItems[i]))
{
if(i > event.currentTarget.index)
{
Tweener.addTween(_menuContainer.getChildByName(_menuItems[i]), {x:_menuItemDefaultX[i] + (event.currentTarget.initialWidth*settings['overZoomRatio'] - event.currentTarget.initialWidth) , time:.5});
}
}
}
}
private function mouseOutButton(event:MouseEvent):void
{
Tweener.addTween(event.currentTarget, {scaleX:1, scaleY:1, time:.5});
Tweener.addTween(event.currentTarget._butTxt, {_color:0xFFFFFF, time:.5});
for(var i:int = 0; i < _menuItems.length; i++)
{
if(event.currentTarget != _menuContainer.getChildByName(_menuItems[i]))
{
Tweener.addTween(_menuContainer.getChildByName(_menuItems[i]), {x:_menuItemDefaultX[i], time:.5});
}
}
}
private function mouseOnButton(event:MouseEvent):void
{
//trace(event.currentTarget+' was clicked!');
//mouseOnButton.getURL(mouseOnButton._link, "_blank");
this.navigateToURL(new URLRequest(this._link), "_blank");
}
}
}
Here is my xml file:
Code:
<FluidMenu>
<settings>
<positionX>CENTER</positionX>
<positionY>CENTER</positionY>
<normalColor>#FFFFFF</normalColor>
<overColor>#DDCC3C</overColor>
<overZoomRatio>1.5</overZoomRatio>
</settings>
<menu>
<item>HOME</item>
<item>ABOUT US</item>
<item>WORK</item>
<item>CONTACT</item>
<item>OTHER</item>
<item>STUFF</item>
</menu>
</FluidMenu>
Please help me try to get each xml button to go to an external swf.
Thanks for the reply.
-
Senior Member
Okay, so you have got your XML imported and everything, right? So you don't need me to continue on that translation, right?
In that case, I would go about it kind of like this:
Add another tag to your menu items, like this for example:
<menu>
<item linkURL="http://www.google.com">item1txt</item>
</menu>
Then when you load your XML on the complete handler poplulate arrays with your your buttons created and URLs:
btnArray.push(btnNameInFunction);
linkArray.push(linkNameInFunction);
The way you have your XML set up you would import the link like I listed similar to this:
Say your imported xml variable was myXML.
And your using a loop to call these
btnNameInFunction.linkName = myXML.menu.item[i].@linkURL
Then in your function you would do something like this:
private function linkIt(evt:Event):void {
if(evt.target.name == btnArray[i]) {
this.navigateToURL(new URLRequest(this[linkArray[i]]), "_blank");
}
}
Something similar to this, where I just had tracing calls.
var btnArray:Array = new Array();
var linkArray:Array = new Array("blah1","blah2","blah3");
var linkName:String;
for (var i:Number = 0; i<linkArray.length; i++) {
btnArray.push(this["mc"+i]);
trace(btnArray);
}
for (var a:Number = 0; a<btnArray.length; a++) {
btnArray[a].addEventListener(MouseEvent.CLICK, linkIt);
btnArray[a].linkName = linkArray[a];
trace(linkArray[a]);
trace(btnArray[a].linkName);
}
function linkIt(evt:Event):void {
for (var i:Number = 0; i<linkArray.length; i++) {
if (this[evt.target.name].linkName == linkArray[i]) {
trace(linkArray[i]);
}
}
}
Am I totally off kilter in helping? If so let me know and I'll try to go more in depth, and more specific. Good luck.
-
Thanks again for the reply but I am a little new to as3 and I don't quite understand. Maybe if you can give me an example using the code I showed in the last post.
Thanks.
-
Senior Member
Sure thing, sorry about the confusion, give me a few minutes to put that together and I'll post it.
-
Senior Member
Looks like I may have to get back to you tomorrow on this because I have to leave work in a min, and I've had to go through your code taking out the classes that you added that i don't have, and I'm going from there. Sorry for the delay.
-
Thats ok. Take your time.
Thanks again.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|