基本 Flash/AS3 扩展 Sprite。我缺少什么?

发布于 2024-10-29 20:00:34 字数 1491 浏览 0 评论 0原文

我刚刚开始尝试学习 Actionscript 3。我目前正在使用 FlashDevelop 作为我的 IDE。虽然我可以用其他语言编写代码,但 AS3 对我来说还是很新的(Flash 也是如此)。

我创建了一个基本类:

package 
{
    import flash.display.Sprite;

    public class BouncingBox extends Sprite
    {
        public function BouncingBox() 
        {
            x = 10;
            y = 10;
            width = 100;
            height = 100;
            graphics.clear();
            graphics.beginFill(0xD4D4D4); // grey color
            graphics.drawRoundRect(0, 0, 100, 100, 10, 10); // x, y, width, height, ellipseW, ellipseH
            graphics.endFill();
        }
    }
}

然后,从我的 Main() 类中,我执行以下操作:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;

    public class Main extends Sprite 
    {
        var mySprite:BouncingBox;

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point
            mySprite = new BouncingBox();
            addChild(mySprite);         

            var t:TextField = new TextField();
            t.text = "Testing!";
            addChild(t);        
        }
    }
}

我期望发生的情况是文本出现在顶部(确实如此),并且 BouncingBox 的实例出现在 10, 10 处舞台(实际上没有)。

为了让这个精灵出现在舞台上,我错过了什么?

I'm right at the beginning of trying to learn Actionscript 3. I'm currently working with FlashDevelop as my IDE. Whilst I can code in other languages, AS3 is very new to me (as is Flash).

I have created a basic class:

package 
{
    import flash.display.Sprite;

    public class BouncingBox extends Sprite
    {
        public function BouncingBox() 
        {
            x = 10;
            y = 10;
            width = 100;
            height = 100;
            graphics.clear();
            graphics.beginFill(0xD4D4D4); // grey color
            graphics.drawRoundRect(0, 0, 100, 100, 10, 10); // x, y, width, height, ellipseW, ellipseH
            graphics.endFill();
        }
    }
}

Then, from my Main() class, I do:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;

    public class Main extends Sprite 
    {
        var mySprite:BouncingBox;

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point
            mySprite = new BouncingBox();
            addChild(mySprite);         

            var t:TextField = new TextField();
            t.text = "Testing!";
            addChild(t);        
        }
    }
}

What I expected to happen was the text to appear at the top (which it does), and the instance of BouncingBox to appear at 10, 10 on the stage (which is doesn't).

What have I missed to make this sprite appear on the stage?

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

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

发布评论

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

评论(2

倾听心声的旋律 2024-11-05 20:00:34

这。

width = 100;
height = 100;

当精灵为空时设置它,它会弄乱它的变换矩阵。当绘制矩形时,它会自己给精灵这个大小。如果您想稍后缩放,请设置宽度和高度。

This.

width = 100;
height = 100;

You set it when sprite is empty, and it messed up its transformation matrix. When rectangle is drawn, it will give sprite this size by itself. Set width and height if you want to scale it later.

居里长安 2024-11-05 20:00:34

直到附加到显示列表后,您的对象才真正初始化。
尝试添加以下代码。

package 
{
    import flash.display.Sprite;

    public class BouncingBox extends Sprite
    {
    public function BouncingBox() 
    {
         if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        x = 10;
        y = 10;
        //width = 100;
        //height = 100;
        graphics.clear();
        graphics.beginFill(0xD4D4D4); // grey color
        graphics.drawRoundRect(0, 0, 100, 100, 10, 10); // x, y, width, height, ellipseW, ellipseH
        graphics.endFill();
    }
    }
}

另外,您可以附加您的盒子,然后初始化其位置等。

mySprite = new BouncingBox();
addChild(mySprite); 
mySprite.x = mySprite.y = 10;

另外,请考虑@alxx 在他的答案中写的内容。

Your object is not really initialized until after is attached to the display list.
Try to add the following code.

package 
{
    import flash.display.Sprite;

    public class BouncingBox extends Sprite
    {
    public function BouncingBox() 
    {
         if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        x = 10;
        y = 10;
        //width = 100;
        //height = 100;
        graphics.clear();
        graphics.beginFill(0xD4D4D4); // grey color
        graphics.drawRoundRect(0, 0, 100, 100, 10, 10); // x, y, width, height, ellipseW, ellipseH
        graphics.endFill();
    }
    }
}

Also, you could attach your box, and then initialize its position, etc.

mySprite = new BouncingBox();
addChild(mySprite); 
mySprite.x = mySprite.y = 10;

Also, consider what @alxx wrote in his answer.

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