AS3 将变量传递给函数不起作用?

发布于 2024-12-08 22:15:23 字数 3152 浏览 1 评论 0原文

我使用 Adob​​e CS3 编写了这段代码,最近升级到了 CS5。我已经很久没有碰过这个网站了,上次我碰过它效果很好。问题如下。

页面类型:画廊 问题:传递变量

这是我上次检查时有效的原始代码,但此后已停止工作。

function onClick(e:MouseEvent):void
    {
            trace(e.currentTarget.name);
            loadPics(e.currentTarget.name);
    }

这是 onClick 函数。最棒的是,痕迹可以显示出照片的正确路径。问题发生在 loadPics() 函数中。正如您所看到的,我将相同的路径(正确打印到屏幕的路径)传递给该函数。这是该函数的代码,如果有点混乱,我深表歉意。

function loadPics(pic):void
{
    photoLoader.source = pic;
    photoLoader.y = 10;
    photoLoader.x = 10;
    loaderHolder.addChild(photoLoader);
    //trace (pic.name) yields nothing
    //trace (photoLoader.source.name) yields nothing
    //trace (photoLoader.name) yields the default instanceXX name
    if(photoLoader.source.width > photoLoader.source.height)
    {
            trace("landscape");
    }
    else if(photoLoader.source.width < photoLoader.source.height)
    {
    trace("portrait");
    }
    else
    {
            trace("fail");
    }

    //draw loader background
    loaderHolder.graphics.beginFill(0xffffff, 1);
    loaderHolder.graphics.drawRect(0, 0, photoLoader.source.width + 20,                photoLoader.source.height + 20);
    loaderHolder.graphics.endFill();
    loaderHolder.x = 230;
    loaderHolder.y = 70;
    this.addChild(loaderHolder);
}

请阐明这个问题。我的缩略图加载得很好,并且所有路径都是正确的。当我点击缩略图时出现的错误是这样的

ReferenceError:错误#1069:在字符串上找不到属性名称,并且没有默认值。 在portfolio_fla::MainTimeline/loadPics() 在 MethodInfo-379()

所以我推断没有加载正确的文件,因为根本没有加载文件。我传递的参数与 loadPic 没有将其作为字符串读取的事实之间是否存在脱节?请帮忙!

哎呀差点忘了这是我对持有者的声明

var loaderHolder:Sprite = new Sprite();
var photoLoader:UILoader = new UILoader();

这是我加载电影时调试器输出的错误

尝试使用 URL C:\Documents and Settings\Brodie\Desktop\stephTsai\portfolio.swf 启动并连接到 Player [SWF] C:\Documents and Settings\Brodie\Desktop\stephTsai\portfolio.swf - 解压后为 426245 字节

这是当我单击缩略图按钮时调试器输出的错误

画廊/大/test1.jpg ReferenceError:错误#1069:在字符串上找不到属性宽度并且没有默认值。 在portfolio_fla::MainTimeline/loadPics()[portfolio_fla.MainTimeline::frame1:45] 在函数/portfolio_fla:MainTimeline/loadThumbs/portfolio_fla:displayThumbs/portfolio_fla:onClick()[portfolio_fla.MainTimeline::frame1:163]

第一部分 'gallery/large/test1.jpg' 是跟踪末尾路径的程序loadThumbs() 函数...我很迷失,如果代码/方法很奇怪,我深表歉意,但我已经很久没有接触过actionscript了。

(编辑 - - - - - - - - - - - - - - - - - - - - - - - - -----------------) 我已经做了一些调查...

如果我将其添加到代码中 - this.addChild(photoLoader) ,那么当我单击时,它会将图片的一个非常小的版本加载到屏幕上。我还可以访问 photoLoader 项目的高度和宽度。所以我猜测搞砸的是本节中的内容:

if(photoLoader.source.width > photoLoader.source.height)
{
        trace("landscape");
}
else if(photoLoader.source.width < photoLoader.source.height)
{
trace("portrait");
}
else
{
        trace("fail");
}

并且

loaderHolder.graphics.drawRect(0, 0, photoLoader.source.width + 20,              photoLoader.source.height + 20);

该程序试图获取字符串的高度和宽度而不是图片。在执行其余功能之前,我是否应该使用不同的语法或应该采取的步骤以便正确加载图片?

I had written this code out using Adobe CS3 and have recently upgraded to CS5. I haven't touched this site in forever and the last time that I did it worked great. The problem is as follows.

type of page: gallery
problem: passing variables

this is the original code that worked last time I checked it but has since stopped working.

function onClick(e:MouseEvent):void
    {
            trace(e.currentTarget.name);
            loadPics(e.currentTarget.name);
    }

This is the onClick function. The great thing is that the trace spits out the correct path to the photo. The problem happens in the loadPics() function. As you can see I'm passing the same path (the one that prints to screen correctly) to the function. here's the code for the function, I apologize if it's a bit messy.

function loadPics(pic):void
{
    photoLoader.source = pic;
    photoLoader.y = 10;
    photoLoader.x = 10;
    loaderHolder.addChild(photoLoader);
    //trace (pic.name) yields nothing
    //trace (photoLoader.source.name) yields nothing
    //trace (photoLoader.name) yields the default instanceXX name
    if(photoLoader.source.width > photoLoader.source.height)
    {
            trace("landscape");
    }
    else if(photoLoader.source.width < photoLoader.source.height)
    {
    trace("portrait");
    }
    else
    {
            trace("fail");
    }

    //draw loader background
    loaderHolder.graphics.beginFill(0xffffff, 1);
    loaderHolder.graphics.drawRect(0, 0, photoLoader.source.width + 20,                photoLoader.source.height + 20);
    loaderHolder.graphics.endFill();
    loaderHolder.x = 230;
    loaderHolder.y = 70;
    this.addChild(loaderHolder);
}

Please shed some light on this issue. My thumbnails load great, and all the paths are correct. The error that spits out when I click a thumbnail is this

ReferenceError: Error #1069: Property name not found on String and there is no default value.
at portfolio_fla::MainTimeline/loadPics()
at MethodInfo-379()

So I'm deducing that the correct file isn't being loaded because no file is being loaded at all. Is there a disconnect between the argument I'm passing and the fact that loadPic isn't reading it as a string? Please help!

Oops almost forgot here are my declarations for the holders

var loaderHolder:Sprite = new Sprite();
var photoLoader:UILoader = new UILoader();

This is the error the debugger outputs when I load the movie

Attempting to launch and connect to Player using URL C:\Documents and Settings\Brodie\Desktop\stephTsai\portfolio.swf
[SWF] C:\Documents and Settings\Brodie\Desktop\stephTsai\portfolio.swf - 426245 bytes after decompression

This is the error the debugger outputs when I click a thumbnail button

gallery/large/test1.jpg
ReferenceError: Error #1069: Property width not found on String and there is no default value.
at portfolio_fla::MainTimeline/loadPics()[portfolio_fla.MainTimeline::frame1:45]
at Function/portfolio_fla:MainTimeline/loadThumbs/portfolio_fla:displayThumbs/portfolio_fla:onClick()[portfolio_fla.MainTimeline::frame1:163]

That first part 'gallery/large/test1.jpg' is the program tracing the path at the end of the loadThumbs() function... I'm so lost and I apologize if the code/methodology is weird but I haven't touched actionscript in forever.

(EDIT-----------------------------------------------------------------)
I've done some investigating...

If I add this in - this.addChild(photoLoader) into the code than it loads a very small version of the picture onto the screen when i click. I can also access the height and width of the photoLoader item. So I'm guessing that what's screwing it up is that in this section:

if(photoLoader.source.width > photoLoader.source.height)
{
        trace("landscape");
}
else if(photoLoader.source.width < photoLoader.source.height)
{
trace("portrait");
}
else
{
        trace("fail");
}

and

loaderHolder.graphics.drawRect(0, 0, photoLoader.source.width + 20,              photoLoader.source.height + 20);

The program is trying to get the height and width of the string not the picture. Is there a different sytax I should be using or a step I should be taking in order to load picture in correctly before executing the rest of the function?

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

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

发布评论

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

评论(3

梦与时光遇 2024-12-15 22:15:23

photoLoader.source 是字符串类型。这就是运行时在尝试获取源属性的宽度时所抱怨的。您需要使用的是 photoLoader.width 和 photoLoader.height。

顺便说一句,我认为在资源(图片)实际加载之前代码不会获得正确的宽度和高度。因此,您可能需要等待 Event.COMPLETE,然后再检查 photoLoader 的高度和宽度。

photoLoader.source is of type string. That's is what the runtime is complaining, when it tries to fetch width on the source property. What you need to use is photoLoader.width and photoLoader.height.

BTW, I think the code won't get correct width and height until the resource (the picture) actually gets loaded. So, you'd probably need to wait for Event.COMPLETE before checking on height and width of photoLoader.

故事与诗 2024-12-15 22:15:23

这更像是评论而不是答案,但我无法发表评论(不知道为什么)。无论如何,user985138 是对的。例如:
- onClick 是 CLICK 事件的处理程序,但是你将它附加到哪里
- function loadPics(pic):void - pic 应该有一个类型

我建议您在 Flash 中单击 Ctrl+Shift+F12,转到 Flash 选项卡,单击脚本值的“设置...”按钮,然后勾选“字符串”和“警告模式”。然后进行编译和调查。

this is more like a comment not an answer, but I can't post comments (have no idea why). Anyway, user985138 is right. For example:
- onClick is a handler of CLICK event, but where did you attach it
- function loadPics(pic):void - pic should have a type

I suggest you to click Ctrl+Shift+F12 in Flash, go to Flash tab, click on Settings... button of Script value and tick String and Warning modes. Then to compile and investigate.

原野 2024-12-15 22:15:23

我看不到代码中的错误在哪里。你在追踪痕迹吗?您确实应该开始输入变量。例如,您将 String 传递到函数 loadPics 中:
loadPics(e.currentTarget.name);

然后您尝试访问String 的变量名称。这毫无意义。

I cant see where you get the Error in the code. Are you running the traces? You should really start to type your variables. For instance, you are passing a String into the function loadPics:
loadPics(e.currentTarget.name);

Then you are trying to access the variable name of the String. That makes no sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文