Flex ActionScript 项目对象声明中出现错误。(project.mxml 文件)

发布于 2024-10-02 21:31:47 字数 1136 浏览 4 评论 0原文

我有一个 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 using
firstapp.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 技术交流群。

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

发布评论

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

评论(3

゛时过境迁 2024-10-09 21:31:48

USE var n:Book = new Book();而不是 var n:Book = new Book;

USE var n:Book = new Book(); instead of var n:Book = new Book;

潇烟暮雨 2024-10-09 21:31:48

麦克向你展示了好方法。但如果您只想快速显示某些内容,请更改

addChild(b);

rawChildren.addChild(b);

编辑:一些说明:应用程序是 UIComponent,因此其 addChild 方法需要 UIComponent。您想要添加精灵。这可以通过应用程序中的 rawChildren.addChild 方法来完成。 rawChildren 对于让 Sprites 与 UIComponent 一起工作非常有用,但是您必须自己管理 Sprites(大小和布局)。

macke showed you good way. But if you want just to show something quickly, change

addChild(b);

to

rawChildren.addChild(b);

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).

長街聽風 2024-10-09 21:31:47

由于您尝试将其添加到 Flex 组件中,因此您可能需要将 Book 包装在 UIComponent 实例中:

<?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:UIComponent = new UIComponent;
            n.addChild(new Book);
            addChild(n);
        ]]>
    </mx:Script>

</mx:Application>

另一种方法是让 Book 继承 UIComponent,如下所示:

package com.books
{
    import flash.display.*;

    public class Book extends UIComponent
    {
        public var var1:String = "Test Var";
        public var var2:Number = 1000;

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            graphics.beginFill(0xff0000, 1);
            graphics.drawRect(0, 0, 500, 200);
            graphics.endfill();
        }
    }
}

然后您可以将 Book 直接添加到您的应用程序中,就像这样:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:books="com.books.*"
    layout="absolute">

    <books:Book />

</mx:Application>

此外,我建议您阅读 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:

<?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:UIComponent = new UIComponent;
            n.addChild(new Book);
            addChild(n);
        ]]>
    </mx:Script>

</mx:Application>

Another way of doing this would be to instead make Book inherit UIComponent, like so:

package com.books
{
    import flash.display.*;

    public class Book extends UIComponent
    {
        public var var1:String = "Test Var";
        public var var2:Number = 1000;

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            graphics.beginFill(0xff0000, 1);
            graphics.drawRect(0, 0, 500, 200);
            graphics.endfill();
        }
    }
}

Then you can add Book directly to your application, like so:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:books="com.books.*"
    layout="absolute">

    <books:Book />

</mx:Application>

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.

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