创建动态图像 as2 代码到 as3

发布于 2024-12-01 04:29:22 字数 1395 浏览 1 评论 0原文

所以我有一个在as2中动态创建图像的函数,告诉一些参数,我面临的问题是我需要将此函数添加到具有as3的文件中,因此不兼容,有人可以帮我将其翻译为as3吗?

   function goGetUrl(dir:String){
    getURL(dir, "_blank");
  }

 function createImage(str:String,
                        movieName:String,
                        nombreIma:String,
                        index:Number,
                        dir:String, 
                        buttonName:String
                        )
    {   
        var mc:MovieClip    = MYMOVIECLIP.createEmptyMovieClip(movieName, MYMOVIECLIP.getNextHighestDepth());
        var image:MovieClip = mc.createEmptyMovieClip(nombreIma, mc.getNextHighestDepth());
        image.loadMovie(str);
        image._y = (index * 160);
        image._x = 10;
        mc.onRelease = function(){
            goGetUrl(dir);
        }
        target = MYMOVIECLIP.attachMovie("MYCUSTOMBUTTONONLIBRARY",buttonName,MYMOVIECLIP.getNextHighestDepth(),{_x: 300, _y: (image._y + 60) });
        target.onRelease = function(){
        goGetUrl(dir);
        }
    } 

所以我像在循环内一样调用它:

for (i=0; i < Proyecto.length; i++) {
    ...
    createImage(imagePro, "nom"+i,"im"+i, i , myurl, "btn"+i);  
   ...
}

例如 getURL(dir, "_blank"); 不起作用,我想我可以通过以下方式更改它:

navigateToURL(new URLRequest (dir));

我也知道 getNextHighestDepth() 在 as3 中不可用

So I have a function to create images dynamically in as2, telling some parameters, the problem I am facing is that I need to add this function to a file that has as3, so is incompatible, can anyone please help me translate it to as3?

   function goGetUrl(dir:String){
    getURL(dir, "_blank");
  }

 function createImage(str:String,
                        movieName:String,
                        nombreIma:String,
                        index:Number,
                        dir:String, 
                        buttonName:String
                        )
    {   
        var mc:MovieClip    = MYMOVIECLIP.createEmptyMovieClip(movieName, MYMOVIECLIP.getNextHighestDepth());
        var image:MovieClip = mc.createEmptyMovieClip(nombreIma, mc.getNextHighestDepth());
        image.loadMovie(str);
        image._y = (index * 160);
        image._x = 10;
        mc.onRelease = function(){
            goGetUrl(dir);
        }
        target = MYMOVIECLIP.attachMovie("MYCUSTOMBUTTONONLIBRARY",buttonName,MYMOVIECLIP.getNextHighestDepth(),{_x: 300, _y: (image._y + 60) });
        target.onRelease = function(){
        goGetUrl(dir);
        }
    } 

So I call it like inside a loop:

for (i=0; i < Proyecto.length; i++) {
    ...
    createImage(imagePro, "nom"+i,"im"+i, i , myurl, "btn"+i);  
   ...
}

For example the getURL(dir, "_blank"); does not work, I think I can chage it by:

navigateToURL(new URLRequest (dir));

also I know that getNextHighestDepth() is not available in as3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笙痞 2024-12-08 04:29:22

首先,这是直接翻译的样子:

function goGetUrl(dir:String)
{
    //getURL becomes navigateToURL
    navigateToURL(new URLRequest(dir), "_blank");
}

function createImage(str:String,movieName:String,nombreIma:String,index:Number,dir:String, buttonName:String)
{   
    //replace createEmptyMovieClip with a new movieclip.
    //addChild automatically adds to the highest available depth
    this[moviename] = MYMOVIECLIP.addChild(new MovieClip()) as MovieClip;
    //movieclip no longer has a load method, so use a Loader instead
    this[nombreIma] = MovieClip(this[moviename]).addChild(new Loader()) as Loader;
    Loader(this[nombreIma]).load(str);
    //properties no longer start with an underscore
    Loader(this[nombreIma]).y = (index * 160);
    Loader(this[nombreIma]).x = 10;
    //using an anonymous function here feels dirty
    MovieClip(this[moviename]).addEventListener(MouseEvent.CLICK,function() { goGetUrl(dir) });
    //AS2 identifiers are replaced by class names
    this[buttonName] = addChild(new MYCUSTOMBUTTONONLIBRARY()) as MYCUSTOMBUTTONONLIBRARY;
    this[buttonName].x = 300;
    this[buttonName].y = this[nombreIma].y + 60;
    MYCUSTOMBUTTONONLIBRARY(this[buttonName]).addEventListener(MouseEvent.CLICK,function() { goGetUrl(dir) });
} 

这在 AS3 术语中是相当令人不愉快的。需要进行大量转换,并且单击处理程序是匿名函数,其执行速度与事件处理程序一样慢。我个人会创建一个可以多次实例化的自定义类,并将引用存储在 Vector 中以便于访问。像这样的东西:

package {

    import flash.display.Sprite;
    import flash.display.Loader;
    import CustomLibraryButton;
    import flash.net.URLRequest;
    import flash.net.navigateToURL;

    public class CustomImageDisplay extends Sprite
    {
        private var loader:Loader
        private var button:CustomLibraryButton;
        private var link:String;

        public var index:int;

        public function CustomImageDisplay($index:int,$img:String,$link:String):void
        {
            _index = $index;
            link = $link;
            init($img);
        }

        private function init($img:String):void
        {
            //init image loader
            loader = addChild(new Loader()) as Loader;
            loader.load($img);
            loader.x = 10;
            loader.y = _index * 160;

            //init button from library
            button = addChild(new CustomLibraryButton()) as CustomLibraryButton;
            button.x = 300;
            button.y = loader.y + 60;

            //add a listener to the whole thing
            this.addEventListener(MouseEvent.CLICK, handleClickEvent);
        }

        private function handleClickEvent(evt:MouseEvent):void
        {
            navigateToURL(new URLRequest(link), "_blank");
        }

    }
}

您可以这样使用它:

var imageButtons:Vector.<CustomDisplayImage> = new Vector.<CustomDisplayImage>(Proyecto.length);
for (i=0; i < Proyecto.length; i++) {
    imageButtons[i] = this.addChild(new CustomImageDisplay(i,imagePro,myurl)) as CustomDisplayImage;
}

这里很酷的事情是类中的鼠标事件会冒泡,因此如果您想知道从主代码中单击了哪个图像组件,您可以向舞台添加一个侦听器,并像这样检查事件对象:

this.stage.addEventListener(MouseEvent.CLICK,handleMouseClick);

function handleMouseClick(evt:MouseEvent):void
{
    if(evt.currentTarget is CustomDisplayImage) {
        trace("The image button index is: " + CustomDisplayImage(evt.currentTarget).index);
    }
}

First, here's how is looks as a direct translation:

function goGetUrl(dir:String)
{
    //getURL becomes navigateToURL
    navigateToURL(new URLRequest(dir), "_blank");
}

function createImage(str:String,movieName:String,nombreIma:String,index:Number,dir:String, buttonName:String)
{   
    //replace createEmptyMovieClip with a new movieclip.
    //addChild automatically adds to the highest available depth
    this[moviename] = MYMOVIECLIP.addChild(new MovieClip()) as MovieClip;
    //movieclip no longer has a load method, so use a Loader instead
    this[nombreIma] = MovieClip(this[moviename]).addChild(new Loader()) as Loader;
    Loader(this[nombreIma]).load(str);
    //properties no longer start with an underscore
    Loader(this[nombreIma]).y = (index * 160);
    Loader(this[nombreIma]).x = 10;
    //using an anonymous function here feels dirty
    MovieClip(this[moviename]).addEventListener(MouseEvent.CLICK,function() { goGetUrl(dir) });
    //AS2 identifiers are replaced by class names
    this[buttonName] = addChild(new MYCUSTOMBUTTONONLIBRARY()) as MYCUSTOMBUTTONONLIBRARY;
    this[buttonName].x = 300;
    this[buttonName].y = this[nombreIma].y + 60;
    MYCUSTOMBUTTONONLIBRARY(this[buttonName]).addEventListener(MouseEvent.CLICK,function() { goGetUrl(dir) });
} 

This is quite unpleasant in AS3 terms. There is lots of casting required and the click handlers are anonymous functions, which is about as slow to execute as an event handler will get. I would personally create a custom class that you can instantiate multiple times, and store the references in a Vector for easy access. Something like this:

package {

    import flash.display.Sprite;
    import flash.display.Loader;
    import CustomLibraryButton;
    import flash.net.URLRequest;
    import flash.net.navigateToURL;

    public class CustomImageDisplay extends Sprite
    {
        private var loader:Loader
        private var button:CustomLibraryButton;
        private var link:String;

        public var index:int;

        public function CustomImageDisplay($index:int,$img:String,$link:String):void
        {
            _index = $index;
            link = $link;
            init($img);
        }

        private function init($img:String):void
        {
            //init image loader
            loader = addChild(new Loader()) as Loader;
            loader.load($img);
            loader.x = 10;
            loader.y = _index * 160;

            //init button from library
            button = addChild(new CustomLibraryButton()) as CustomLibraryButton;
            button.x = 300;
            button.y = loader.y + 60;

            //add a listener to the whole thing
            this.addEventListener(MouseEvent.CLICK, handleClickEvent);
        }

        private function handleClickEvent(evt:MouseEvent):void
        {
            navigateToURL(new URLRequest(link), "_blank");
        }

    }
}

You could use that like this:

var imageButtons:Vector.<CustomDisplayImage> = new Vector.<CustomDisplayImage>(Proyecto.length);
for (i=0; i < Proyecto.length; i++) {
    imageButtons[i] = this.addChild(new CustomImageDisplay(i,imagePro,myurl)) as CustomDisplayImage;
}

The cool thing here is that mouse event in the class will bubble, so if you wanted to know which image component had been clicked from your main code you would add a listener to the stage, and check the event object like this:

this.stage.addEventListener(MouseEvent.CLICK,handleMouseClick);

function handleMouseClick(evt:MouseEvent):void
{
    if(evt.currentTarget is CustomDisplayImage) {
        trace("The image button index is: " + CustomDisplayImage(evt.currentTarget).index);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文