我有.ai格式的美国地图。我想在我的flex应用程序中使用它 问题是地图是在状态行上切片的,我想操纵应用程序中的状态。
说明:在我的应用程序中,我希望能够单击状态以突出显示它,并将所有其他状态驱逐出场景,然后将所有状态恢复到整个国家。就像一个拼图游戏。
我将Adobe Illustrator中的FXG文件导入Flash Builder 4中的Flex应用程序,但我无法一次操作每个状态!
非常感谢帮助! 欢呼声
答案 0 :(得分:1)
有一个FxgParser库,但如果在.fxg文件中将它们标记为d:userLabel
属性,它似乎不会输出状态名称。您可以修改源代码来修复它。
以下是::
编辑fxgparser.parser.Path.as然后在该类的顶部添加此命名空间:
public static const aab:Namespace = new Namespace("aab", "http://ns.adobe.com/fxg/2008/dt");
然后在parse()方法结束时添加:
target.name = data.currentXml.@aab::userLabel;
因为Illustrator CS6似乎添加了userLabel属性而不是id或其他东西,而且默认情况下FxgParser忽略了这一点。
代码与上面代码非常类似:
package {
import flash.display.*;
import flash.events.*;
import flash.geom.Point;
import flash.net.*;
import flash.utils.Dictionary;
import com.greensock.*;
import fxgparser.FxgDisplay;
[SWF(width='1368', height='936', backgroundColor='#ffffff', frameRate='30')]
public class FXGTest extends Sprite{
private var states:Sprite;//container holding each state Shape
private var positions:Dictionary = new Dictionary();//original position to restore states to
public function FXGTest() {
addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(e:Event):void{
new URLLoader(new URLRequest("usa-wikipedia.fxg")).addEventListener(Event.COMPLETE,fxgLoaded);
}
private function fxgLoaded(e:Event):void{
//trace(e.target.data);
var map:FxgDisplay = addChild(new FxgDisplay( new XML(e.target.data) )) as FxgDisplay;
trace(listChildren(map));
//map.item.item.item.*
states = Sprite(Sprite(Sprite(map.getChildAt(0)).getChildAt(0)).getChildAt(0));
for(var i:int = 0 ; i < states.numChildren; i++){
var s:DisplayObject = DisplayObject(states.getChildAt(i));//can't add mouse listeners because the states are Shape instances
positions[s.name] = new Point(s.x,s.y);//store the positions in a hash to get the state position by name
}
stage.addEventListener(MouseEvent.CLICK,handleClick);
}
private function handleClick(e:MouseEvent):void{
if(e.target is Stage) restore();
else blast(getObjectsUnderPoint(new Point(mouseX,mouseY))[0].name);//named shapes are stored in a sprite so use getObjetsUnderPoint
}
private function blast(name:String):void{
var blastRadius:Number = stage.stageWidth+stage.stageHeight;//or some other number
var c:DisplayObject = states.getChildByName(name);//selected
for(var i:int = 0 ; i < states.numChildren; i++){
var s:DisplayObject = states.getChildAt(i);
if(s != c){//everything else goes except this
var angle:Number = Math.atan2(s.y-c.y,s.x-c.x);//direction relative to the clicked state
var x:Number = s.x + (Math.cos(angle) * blastRadius);//get a position offset from the clicked state (blast centre)
var y:Number = s.y + (Math.sin(angle) * blastRadius);//outwards at a random angle
TweenLite.to(s,1+Math.random() * 2,{x:x,y:y});//tween with different speeds for a bit of depth
}
}
}
private function restore():void{
for(var i:int = 0 ; i < states.numChildren; i++){
var s:DisplayObject = DisplayObject(states.getChildAt(i));
TweenLite.to(s,.5+Math.random() * .5,{x:positions[s.name].x,y:positions[s.name].y});//tween states back into the original positions
}
}
private function listChildren(d:DisplayObjectContainer):String{
var result = 'parent: '+d.name+'\n\t';
for(var i:int = 0 ; i < d.numChildren; i++){
var c:DisplayObject = d.getChildAt(i);
result += '\tchild: '+c.name+'\n';
if(c is DisplayObjectContainer) result += listChildren(DisplayObjectContainer(c));
}
return result;
}
}
}
您还可以下载上述类,修改后的fxgparser库并说明fxg here并查看正在运行的演示here 或者你可以使用SVG而不是FXG。同样的开发人员也有一个SvgParser库你可以这样:
svn export http://www.libspark.org/svn/as3/SvgParser
您可以轻松地将.ai文件另存为.svg文件。 以下是使用US map from wikipedia:
的简单示例package {
import flash.display.*;
import flash.events.*;
import flash.geom.Point;
import flash.net.*;
import flash.utils.Dictionary;
import svgparser.SvgDisplay;
import com.greensock.*;
[SWF(width='1368', height='936', backgroundColor='#ffffff', frameRate='30')]
public class SVGTest extends Sprite {
private var states:Sprite;//container holding each state Shape
private var positions:Dictionary = new Dictionary();//original position to restore states to
public function SVGTest() {
addEventListener(Event.ADDED_TO_STAGE,init);//make sure we have the stage ready
}
private function init(e:Event):void{
removeEventListener(Event.ADDED_TO_STAGE,init);//stage ready, load the svg
new URLLoader(new URLRequest("http://upload.wikimedia.org/wikipedia/commons/3/32/Blank_US_Map.svg")).addEventListener(Event.COMPLETE,svgLoaded);
}
private function svgLoaded(e:Event):void{//svg loaded
var map:SvgDisplay = addChild(new SvgDisplay( new XML(e.target.data) )) as SvgDisplay;//use the parser
trace(listChildren(map));//quick debug of the maps display list
states = Sprite(map.getChildAt(0));//we want to access the main container
for(var i:int = 0 ; i < states.numChildren; i++){
var s:DisplayObject = DisplayObject(states.getChildAt(i));//can't add mouse listeners because the states are Shape instances
positions[s.name] = new Point(s.x,s.y);//store the positions in a hash to get the state position by name
}
stage.addEventListener(MouseEvent.CLICK,handleClick);
}
private function handleClick(e:MouseEvent):void{
if(e.target is Stage) restore();
else blast(getObjectsUnderPoint(new Point(mouseX,mouseY))[0].name);//named shapes are stored in a sprite so use getObjetsUnderPoint
}
private function blast(name:String):void{
var blastRadius:Number = stage.stageWidth+stage.stageHeight;//or some other number
for(var i:int = 0 ; i < states.numChildren; i++){
var s:DisplayObject = states.getChildAt(i);
if(s.name != name){//everything else goes except this
var angle:Number = Math.random() * Math.PI * 2;//pick a random angle
var x:Number = s.x + Math.cos(angle) * blastRadius;//get a position offset from the clicked state (blast centre)
var y:Number = s.y + Math.sin(angle) * blastRadius;//outwards at a random angle
TweenLite.to(s,1+Math.random() * 2,{x:x,y:y});//tween with different speeds for a bit of depth
}
}
}
private function restore():void{
for(var i:int = 0 ; i < states.numChildren; i++){
var s:DisplayObject = DisplayObject(states.getChildAt(i));
TweenLite.to(s,.5+Math.random() * .5,{x:positions[s.name].x,y:positions[s.name].y});//tween states back into the original positions
}
}
private function listChildren(d:DisplayObjectContainer):String{//recursively traverse a display list and list children names
var result = 'parent: '+d.name+'\n\t';
for(var i:int = 0 ; i < d.numChildren; i++){
var c:DisplayObject = d.getChildAt(i);
result += '\tchild: '+c.name+'\n';
if(c is DisplayObjectContainer) result += listChildren(DisplayObjectContainer(c));
}
return result;
}
}
}
答案 1 :(得分:1)