AS3 中的显示行
我对这个函数感到困惑,它是在此之前调用的,参数分别为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您已在某处声明了
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).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 containingmLine
. 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, becausestage
would benull
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.
你的代码看起来应该可以工作。我重写了它,以更好地符合 ActionScript 3 最佳实践。
形状的 x 和 y 属性都将为零,因为您从未设置它们。您只是在 xVal 和 yVal 处的形状内部绘制线条。你可以做同样的事情,像这样:
不知道为什么它根本没有出现在你面前。
Your code seems like it should work. I have rewrote it to conform better to ActionScript 3 best practices
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:
Not sure why its not showing up at all for you though.