Flex ActionScript 项目对象声明中出现错误。(project.mxml 文件)
我有一个 Flex ActionScript 应用程序,我需要在舞台上绘制一个简单的矩形。我正在使用 firstapp.mxml
和另一个名为 Book.as
的类;
这是我完成的完整代码。
firstapp.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:Script>
<![CDATA[
import com.books.Book;
import flash.display.*;
var n:Book = new Book;
//n.var1 = "Another String";
addChild(n);
]]>
</mx:Script>
</mx:Application>
Book.as
package com.books
{
import flash.display.*;
public class Book extends Sprite
{
public var var1:String = "Test Var";
public var var2:Number = 1000;
public function Book()
{
var b = new Sprite;
b.graphics.beginFill(0xFF0000, 1);
b.graphics.drawRect(0, 0, 500, 200);
b.graphics.endFill();
addChild(b);
}
}
}
我是 Flex 新手,所以请帮我解决这个问题。我想显示矩形。
I have a Flex ActionScript Application, I need to draw a simple rectangle in stage. I am usingfirstapp.mxml
and another Class called Book.as
;
here is the complete code which i have done.
firstapp.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:Script>
<![CDATA[
import com.books.Book;
import flash.display.*;
var n:Book = new Book;
//n.var1 = "Another String";
addChild(n);
]]>
</mx:Script>
</mx:Application>
Book.as
package com.books
{
import flash.display.*;
public class Book extends Sprite
{
public var var1:String = "Test Var";
public var var2:Number = 1000;
public function Book()
{
var b = new Sprite;
b.graphics.beginFill(0xFF0000, 1);
b.graphics.drawRect(0, 0, 500, 200);
b.graphics.endFill();
addChild(b);
}
}
}
I'm new in Flex, So please help me to fix this. I want to show the rectangle.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
USE var n:Book = new Book();而不是 var n:Book = new Book;
USE var n:Book = new Book(); instead of var n:Book = new Book;
麦克向你展示了好方法。但如果您只想快速显示某些内容,请更改
为
编辑:一些说明:应用程序是 UIComponent,因此其 addChild 方法需要 UIComponent。您想要添加精灵。这可以通过应用程序中的 rawChildren.addChild 方法来完成。 rawChildren 对于让 Sprites 与 UIComponent 一起工作非常有用,但是您必须自己管理 Sprites(大小和布局)。
macke showed you good way. But if you want just to show something quickly, change
to
Edit: Some clarifications: application is UIComponent, therefore its addChild method needs UIComponent. You want to add Sprite. This can be done with rawChildren.addChild method from application. rawChildren is useful to get Sprites work with UIComponents, but you have to manage sprites yourself (sizes and layout).
由于您尝试将其添加到 Flex 组件中,因此您可能需要将 Book 包装在 UIComponent 实例中:
另一种方法是让 Book 继承 UIComponent,如下所示:
然后您可以将 Book 直接添加到您的应用程序中,就像这样:
此外,我建议您阅读 Flex 组件架构。 Adobe 在主题,但您应该知道该信息特定于 Flex 3(我注意到您正在使用 Flex 3,因此有链接)。尽管许多信息可能仍然适用于 Flex 4(例如组件生命周期),但还是存在差异,尤其是在皮肤方面。
Since you're trying to add this to a flex component, you probably need to wrap Book in a UIComponent instance:
Another way of doing this would be to instead make Book inherit UIComponent, like so:
Then you can add Book directly to your application, like so:
Additionally, I suggest you read up on the Flex component architecture. There's some pretty good documentation from Adobe on the subject, but you should be aware that the information is specific to Flex 3 (I noticed you're using Flex 3, hence the link). Even while a lot of the information may still be applicable to Flex 4 (the component lifecycle for instance) there are differences, especially in terms of skinning.