Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 Actionscript 3.0将函数组合在一起,形成一个可应用于多个图像的通用函数_Actionscript 3_Function_Generics - Fatal编程技术网

Actionscript 3 Actionscript 3.0将函数组合在一起,形成一个可应用于多个图像的通用函数

Actionscript 3 Actionscript 3.0将函数组合在一起,形成一个可应用于多个图像的通用函数,actionscript-3,function,generics,Actionscript 3,Function,Generics,当我输入一个特定的文本字符串时,我会加载这些图像,但我不知道如何组合函数来生成一个适用于所有图像的通用函数。 这只是两个,它们基本上是一样的,只是我必须让每个函数(某物)1,然后(某物)2用于下一张图片。 我需要一些帮助来做这件事 function onInput(event:TextEvent):void { if(ti.text.search('a')!=-1) addChild(ottefct); else if(ti.text.search('b')!=-1) addChild(rnd)

当我输入一个特定的文本字符串时,我会加载这些图像,但我不知道如何组合函数来生成一个适用于所有图像的通用函数。 这只是两个,它们基本上是一样的,只是我必须让每个函数(某物)1,然后(某物)2用于下一张图片。 我需要一些帮助来做这件事

function onInput(event:TextEvent):void {
if(ti.text.search('a')!=-1) addChild(ottefct);
else if(ti.text.search('b')!=-1) addChild(rnd);

var oeffect:Loader = new Loader();
oeffect.load(new URLRequest("http://i54.tinypic.com/anom5d.png"));
oeffect.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadingComplete);
var ottefct:Sprite = new Sprite();
 function onLoadingComplete(event:Event):void
 {
    ottefct.addChild( event.currentTarget.loader.content );
    ottefct.addEventListener(MouseEvent.MOUSE_DOWN, drag);
    ottefct.addEventListener(MouseEvent.MOUSE_UP, drop);
    ottefct.doubleClickEnabled = true;
    ottefct.addEventListener(MouseEvent.MOUSE_WHEEL, rotate)
    ottefct.addEventListener(MouseEvent.DOUBLE_CLICK, unrotate)
    ottefct.height=180
    ottefct.width=124
 }
 function drag(event:MouseEvent):void{
     ottefct.startDrag()
  }
 function drop(event:MouseEvent):void{
     ottefct.stopDrag()
 }
 function rotate(event:MouseEvent):void{
     ottefct.rotation = 90
     }
 function unrotate(event:MouseEvent):void{
     ottefct.rotation = 0
     }
//---------------------------

var rednova:Loader = new Loader();
rednova.load(new URLRequest("http://i53.tinypic.com/2dv7dao.png"));
rednova.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadingComplete1);
var rnd:Sprite = new Sprite();
 function onLoadingComplete1(event:Event):void
 {
    rnd.addChild( event.currentTarget.loader.content );
    rnd.addEventListener(MouseEvent.MOUSE_DOWN, drag1);
    rnd.addEventListener(MouseEvent.MOUSE_UP, drop1);
    ottefct.addEventListener(MouseEvent.MOUSE_WHEEL, rotate1)
    ottefct.addEventListener(MouseEvent.DOUBLE_CLICK, unrotate1)
    rnd.height=180
    rnd.width=124
 }
 function drag1(event:MouseEvent):void{
     rnd.startDrag()
  }
 function drop1(event:MouseEvent):void{
     rnd.stopDrag()
 }
 function rotate1(event:MouseEvent):void{
     rnd.rotation = 90
     }
 function unrotate1(event:MouseEvent):void{
     rnd.rotation = 0
     }

我觉得无聊。给你。然而,这真的应该是在课堂上

//Dictionary to store images (can use Array)
var loaded_images:Dictionary = new Dictionary();

function load_image(url:String, id_name:String)
{
    var loader:Loader = new Loader();
    loader.name = id_name;
    var url_req:URLRequest = new URLRequest(url);
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadingComplete);
    loader.load(url_req);
}

function onLoadingComplete(evt:Event):void
{
    var img_name:String = evt.currentTarget.loader.name
    var spr_box:Sprite = new Sprite();
    spr_box.addChild(evt.currentTarget.loader);

    spr_box.mouseChildren = false;
    spr_box.doubleClickEnabled = true;

    spr_box.addEventListener(MouseEvent.MOUSE_DOWN, drag);
    spr_box.addEventListener(MouseEvent.MOUSE_UP, drop);
    spr_box.addEventListener(MouseEvent.MOUSE_WHEEL, rotate);
    spr_box.addEventListener(MouseEvent.DOUBLE_CLICK , unrotate);

    //Shouldn't really hard code this here
    spr_box.width = 180;
    spr_box.height = 124;

    // - Since this isn't a class, I'd do this instead:
    //spr_box.addEventListener(Event.ADDED_TO_STAGE, resize_img);

    this.addChild(spr_box);
    loaded_images[img_name] = spr_box;
}

//Because this event function lets you control individual image dimensions
/*
function resize_img(evt:Event):void
{
    switch (evt.currentTarget.name)
    {
        case "ImageOne":
            evt.currentTarget.width = 250;
            evt.currentTarget.height = 250;
            break;
        default:
            evt.currentTarget.width = 180;
            evt.currentTarget.height = 124;
            break;
    }
}
*/

function drag(evt:MouseEvent):void
{
    evt.currentTarget.startDrag()
}

function drop(evt:MouseEvent):void
{
    evt.currentTarget.stopDrag()
}

function rotate(evt:MouseEvent):void
{
    evt.currentTarget.rotation = 90
}

function unrotate(evt:MouseEvent):void
{
    evt.currentTarget.rotation = 0
}

//Few examples of use...
load_image("http://www.google.co.nz/images/nav_logo26.png", "ImageOne");
load_image("http://l.yimg.com/ao/i/nzunihead/logo-yahooxtra_1_4.png", "RandomName");

我还没有测试过这个,但是你应该知道大概的意思。如果遵循相同的原则,您甚至可以进一步优化;)
另外,在使用完事件侦听器后,不要忘记删除它们

 function loadImage(url:String):void
 {
     var loader:Loader = new Loader();

     //use the name property to identify your Loader instance
     loader.name = url;

     //add the event listener before calling load()!
     loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadingComplete);
     loader.load(new URLRequest(url));
 }

 function onLoadingComplete(event:Event):void
 {
      switch(event.currentTarget.loader.name )
      {
          //depending on the url add to the relevant Sprite instance
          case "add the url here":
            ottefct.addChild( event.currentTarget.loader.content );
            addListeners(ottefct);
            break;

          case "add the url here":
            rnd.addChild( event.currentTarget.loader.content );
            addListeners(rnd);
            break;
      }
 }

 function addListeners(sp:Sprite):void
 {

    sp.addEventListener(MouseEvent.MOUSE_DOWN, drag);
    sp.addEventListener(MouseEvent.MOUSE_UP, drop);
    sp.doubleClickEnabled = true;
    sp.addEventListener(MouseEvent.MOUSE_WHEEL, rotate)
    sp.addEventListener(MouseEvent.DOUBLE_CLICK, unrotate)
 }