3 Replies Latest reply on Sep 19, 2007 10:24 PM by tnylsej

    External actionscript file

    tnylsej
      Hi, I've more than two components accessing the same function. Hence, I would like the function to be on an external actionscript file.

      My codes are as followed:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:HDividedBox xmlns:mx=" http://www.adobe.com/2006/mxml" width="100%" height="100%">

      <mx:Metadata>
      [Event(name="scaleChange", type="actionscripts.sliderChange")]
      </mx:Metadata>

      <mx:Script>
      <![CDATA[
      import actionscripts.sliderChange;
      import mx.managers.DragManager;
      import mx.core.DragSource;
      import mx.events.DragEvent;
      import flash.events.MouseEvent;
      import mx.containers.Box;

      [Bindable]
      [Embed(source="/images/map.jpg")]
      public var map:Class;

      public var oWidth:Number = 2800; //original map width - 1200
      public var oHeight:Number = 2100; //original map height - 900

      public var xOff:Number;
      public var yOff:Number;

      private function dragMap(event:Event):void{
      event.currentTarget.addEventListener(MouseEvent.MOUSE_DOWN, mouseMoveHandler);
      }

      private function doDragEnter(event:DragEvent):void{
      DragManager.acceptDragDrop(Box(event.target));
      }

      private function mouseMoveHandler(event:MouseEvent):void{
      event.currentTarget.useHandCursor=true;

      var dragInitiator:Image=Image(event.currentTarget);

      var ds:DragSource = new DragSource();
      ds.addData(event.currentTarget, 'img');

      var imageProxy:Image = new Image();
      imageProxy.source = map;

      xOff = event.currentTarget.mouseX;
      yOff = event.currentTarget.mouseY;

      DragManager.doDrag(dragInitiator, ds, event, imageProxy, 0, 0, 0.00);
      }

      private function doDragDrop(event:DragEvent):void{
      targetMap.x = event.currentTarget.mouseX - xOff;
      targetMap.y = event.currentTarget.mouseY - yOff;
      }


      public function onSliderChange():void{
      var temp:Number = 100-scale.value;

      targetMap.width = temp/100*oWidth+1200;
      targetMap.height = temp/100*oHeight+900;

      map.smoothing = true;

      var nWidth:Number = targetMap.width;
      var nHeight:Number = targetMap.height;

      var sliderObj:sliderChange = new sliderChange(nWidth as Number,nHeight as Number,"scaleChange");
      dispatchEvent(sliderObj);
      }
      ]]>
      </mx:Script>

      <mx:Canvas id="scaling" width="75%" height="100%">
      <mx:VBox width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
      <mx:Box id="mapContainer" width="500" height="500" horizontalAlign="center" verticalAlign="middle" horizontalScrollPolicy="off" verticalScrollPolicy="off" dragEnter="doDragEnter(event);" dragDrop="doDragDrop(event);">
      <mx:Image source="{map}" id="targetMap" scaleContent="true" creationComplete="dragMap(event);" />
      </mx:Box>

      <mx:HSlider id="scale" liveDragging="true" minimum="1" maximum="100" labels="['1','100']" width="500" height="30" change="onSliderChange()" />
      <mx:Text text="{scale.value.toFixed(2)} km" />
      </mx:VBox>
      </mx:Canvas>

      <mx:Canvas width="25%" height="100%" styleName="sideBar">
      <mx:Label text="test test" />
      </mx:Canvas>

      </mx:HDividedBox>

      How do I make the drag and drop functions in the <mx:Script></mx:Script> to be in an external actionscript file so that this component can access it for the drag and drop to work?

      Any help is very much appreciated. ^^
        • 1. Re: External actionscript file
          JKohn99 Level 1
          You can reference a external actionscript file in a
          <mx:Script source="myScripts.as"/>

          I do not use it this way as it is not a vary OO way to do things.
          I will generally create a singleton class (so it is only instantiated once)
          and use it for utility type functions.

          For example:

          public class UtilityClass
          {
          private static var _instance:UtilityClass;

          public function UtilityClass()
          {

          }
          public static function getInstance():UtilityClass
          {
          if(_instance == null)
          _instance = new UtilityClass();
          return _instance;
          }
          public function myFunction():void
          {

          }
          }

          Then in a mxml
          <mx:list id="list" dragDrop="UtilityClass.getInstance().myfunction()"


          • 2. Re: External actionscript file
            tnylsej Level 1
            Thanks for your reply.

            Can you give me a simple example? I don't know how to apply your example to my codes as it is too general.

            Thanks. ^^
            • 3. Re: External actionscript file
              tnylsej Level 1
              Finally got it working after so many days! =D

              Thank you very much. =D