所以我有一个函数在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));
我也知道as3
中没有getNextHighestDepth()
答案 0 :(得分:3)
首先,这是看起来如何直接翻译:
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);
}
}