AS3 中的显示行

发布于 2024-12-06 07:26:25 字数 776 浏览 1 评论 0原文

我对这个函数感到困惑,它是在此之前调用的,参数分别为 xVal 和 yVal 的参数 22 和 58。编译和测试 swf 时,它不会显示任何内容,并且没有错误。代码在文档类中:

    private function mLine(xVal : int, yVal : int) {
        var rCol = 0x0000FF;
        var incr = Math.round((Math.random() * 20) + 8);
        lns.push(new Shape());
        var i = lns.length - 1;

        this.addChild(lns[i]);
        lns[i].graphics.moveTo(xVal, yVal);
        lns[i].graphics.lineStyle(10, rCol);
        lns[i].graphics.lineTo(xVal, yVal + 20);
        lns[i].name = incr;
        trace("lns[" + i + "] x is " + lns[i].x); // outputs 'lns[0] x is 0'
        trace("xVal is " + xVal); // outputs 'xVal is 22'
        trace("yVal is " + yVal); //outputs 'yVal is 58'
        trace(stage.contains(lns[i])); // outputs 'true'
    }

I am baffled by this function, which is called prior to this with parameters 22 and 58 for xVal ad yVal respectively. It doesn't display anything when the swf is compiled and tested, and it's error free. The code is in the document class:

    private function mLine(xVal : int, yVal : int) {
        var rCol = 0x0000FF;
        var incr = Math.round((Math.random() * 20) + 8);
        lns.push(new Shape());
        var i = lns.length - 1;

        this.addChild(lns[i]);
        lns[i].graphics.moveTo(xVal, yVal);
        lns[i].graphics.lineStyle(10, rCol);
        lns[i].graphics.lineTo(xVal, yVal + 20);
        lns[i].name = incr;
        trace("lns[" + i + "] x is " + lns[i].x); // outputs 'lns[0] x is 0'
        trace("xVal is " + xVal); // outputs 'xVal is 22'
        trace("yVal is " + yVal); //outputs 'yVal is 58'
        trace(stage.contains(lns[i])); // outputs 'true'
    }

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

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

发布评论

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

评论(2

澜川若宁 2024-12-13 07:26:25

假设您已在某处声明了 private var lns = [];,它会绘制一条蓝线(从给定位置垂直向下 20px)。

不显示任何内容

这意味着舞台上可能没有该类的对象。在文档类中,您应该使用 addChild 来显示包含 mLine 的类的实例。显然需要以某种方式调用 mLine。您可以在类的构造函数中执行此操作,但需要删除最后一个跟踪语句以避免空指针错误,因为此时 stage 将为 null

编辑:错过了您所说的它在 Document 类中。所以,尝试看看画其他东西是否有效。问题似乎不在于这个功能。

Assuming you have declared private var lns = []; somewhere, it draws a blue line (20px straight down from the given position).

It doesn't display anything

That means you probably don't have an object of that class on the stage. In your document class, you should use addChild to display an instance of the class containing mLine. mLine needs to be called somehow obviously. You could do this in the class' constructor, but you'd need to remove the last trace statement to avoid a null pointer error, because stage would be null then.

Edit: Missed that you said it is in the Document class. So, try and see if drawing anything else works. The problem doesn't seem to be with this function.

一指流沙 2024-12-13 07:26:25

你的代码看起来应该可以工作。我重写了它,以更好地符合 ActionScript 3 最佳实践。

private function drawLine(xVal:int, yVal:int):void 
{
    var lineColor:uint = 0x0000FF;

    var lineShape:Shape = new Shape();
    //lineShape.name = String(Math.round((Math.random() * 20) + 8));
    lineShape.graphics.lineStyle(10, lineColor);
    lineShape.graphics.moveTo(xVal, yVal);
    lineShape.graphics.lineTo(xVal, yVal + 20);
    addChild(lineShape);
    lines.push(lineShape);
}

形状的 x 和 y 属性都将为零,因为您从未设置它们。您只是在 xVal 和 yVal 处的形状内部绘制线条。你可以做同样的事情,像这样:

private function mLine(xVal:int, yVal:int) 
{
    var lineColor:uint = 0x0000FF;

    var lineShape:Shape = new Shape();
    //lineShape.name = String(Math.round((Math.random() * 20) + 8));
    lineShape.graphics.lineStyle(10, lineColor);
    lineShape.graphics.moveTo(0, 0);
    lineShape.graphics.lineTo(0, 20);
    lineShape.x = xVal;
    lineShape.y = yVal;
    addChild(lineShape);
    lines.push(lineShape);
}

不知道为什么它根本没有出现在你面前。

Your code seems like it should work. I have rewrote it to conform better to ActionScript 3 best practices

private function drawLine(xVal:int, yVal:int):void 
{
    var lineColor:uint = 0x0000FF;

    var lineShape:Shape = new Shape();
    //lineShape.name = String(Math.round((Math.random() * 20) + 8));
    lineShape.graphics.lineStyle(10, lineColor);
    lineShape.graphics.moveTo(xVal, yVal);
    lineShape.graphics.lineTo(xVal, yVal + 20);
    addChild(lineShape);
    lines.push(lineShape);
}

The x and y properties of your shape will both be zero because you never set them. you are just drawing lines inside the shape at the xVal and yVal. You could do the same thing like this:

private function mLine(xVal:int, yVal:int) 
{
    var lineColor:uint = 0x0000FF;

    var lineShape:Shape = new Shape();
    //lineShape.name = String(Math.round((Math.random() * 20) + 8));
    lineShape.graphics.lineStyle(10, lineColor);
    lineShape.graphics.moveTo(0, 0);
    lineShape.graphics.lineTo(0, 20);
    lineShape.x = xVal;
    lineShape.y = yVal;
    addChild(lineShape);
    lines.push(lineShape);
}

Not sure why its not showing up at all for you though.

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