Flash 创建as2代码到as3的动态图像
因此,我有一个在as2中动态创建图像的函数,告诉一些参数,我面临的问题是,我需要将此函数添加到一个包含as3的文件中,因此不兼容,是否有人可以帮助我将其转换为as3Flash 创建as2代码到as3的动态图像,flash,actionscript-3,actionscript,dynamic,actionscript-2,Flash,Actionscript 3,Actionscript,Dynamic,Actionscript 2,因此,我有一个在as2中动态创建图像的函数,告诉一些参数,我面临的问题是,我需要将此函数添加到一个包含as3的文件中,因此不兼容,是否有人可以帮助我将其转换为as3 function goGetUrl(dir:String){ getURL(dir, "_blank"); } function createImage(str:String, movieName:String, n
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);
...
}
另外,我知道as3中没有
getNextHighestDepth()
首先,以下是is作为直接翻译的方式:
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术语中是相当令人不快的。需要进行大量的强制转换,而且单击处理程序是匿名函数,执行速度与事件处理程序一样慢。我将亲自创建一个自定义类,您可以多次实例化该类,并将引用存储在向量中以便于访问。大概是这样的:
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);
}
}
你可以这样使用:
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);
}
}
回答得很好,关于单击处理程序的提示是受欢迎的,因为提高性能始终是一个问题。