防止 DisplayObject 的高度变化

发布于 2024-10-20 23:46:07 字数 958 浏览 1 评论 0原文

有没有办法防止 DisplayObject 的高度属性自动更改?尽管我的 swf 文件高度为 32 像素,但它会自动调整大小以匹配内容。下面的代码可以证明这一点,第一帧 enemy.height 是 32,但后来是 27.5,这弄乱了我的脚本。

getRect() 和 getBounds() 返回完全相同。另外,我想知道为什么在第一帧中它显示正确的高度,而在第二帧中它发生变化,它应该从一开始就显示 27.5。

package {
    import flash.display.Sprite;
    import flash.events.Event;
    public class Main extends Sprite {
        private var enemy:Sprite;
        [Embed(source = '../lib/enemy.swf')] private var swf:Class;
        public function Main():void {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function update(e:Event):void {
            trace(enemy.height);
        }

        private function init(e:Event = null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            enemy = new swf();
            addChild(enemy);
            addEventListener(Event.ENTER_FRAME, update);
        }

    }

}

Is there a way to prevent the automatic change of the height property of a DisplayObject? It automatically resizes to match content, though my swf file is 32 pixels height. The code below can show prove of this, first frame enemy.height is 32 but later is 27.5, and this messes up my script.

getRect() and getBounds() return exactly the same. Also, I want to know why in the first frame it shows the correct height and in the second it changes, it should show 27.5 from the beginning.

package {
    import flash.display.Sprite;
    import flash.events.Event;
    public class Main extends Sprite {
        private var enemy:Sprite;
        [Embed(source = '../lib/enemy.swf')] private var swf:Class;
        public function Main():void {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function update(e:Event):void {
            trace(enemy.height);
        }

        private function init(e:Event = null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            enemy = new swf();
            addChild(enemy);
            addEventListener(Event.ENTER_FRAME, update);
        }

    }

}

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

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

发布评论

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

评论(4

腹黑女流氓 2024-10-27 23:46:07

这与您实例化整个 SWF 的事实有关,并且必须经过 1 帧才能将其同步到主 swf。我要做的就是导出 .fla 中的符号,然后在 Flex 中使用“嵌入符号”语法:

[Embed(source='enemy.swf#Symbol1')]
private var swf:Class;

在这种情况下,即使在第一个 ENTER_FRAME 中,高度也将保持一致。如果不是您想要的高度,您可以使用不可见的形状来设置边界。

This has to do with the fact that you're instantiating a whole SWF, and 1 frame has to pass for it to be synced to the Main swf. What I would do is export the symbol in the .fla, then use the "embed symbol" syntax in Flex:

[Embed(source='enemy.swf#Symbol1')]
private var swf:Class;

In this case, the height will be consistent even in the first ENTER_FRAME. If it's not the height you want, you can use the invisible shape to set the bounds.

死开点丶别碍眼 2024-10-27 23:46:07

一个“hacky”解决方案可能是向敌人添加一个具有您想要的最大尺寸的形状,然后将其设置为不可见。我已经用这种方式为对象创建了命中框,效果非常好。

一种方法是在 Flash IDE 中创建对象时添加它。只需绘制它并将其放置在您想要的形状中,然后为其指定一个实例名称,例如“sizeHolder”。创建敌人后,您可以调用

enemy.sizeHolder.visible = false;

在 Flash IDE 中,您可以将其放置在另一个时间轴上,然后使该时间轴不可见并锁定它,这样在编辑实际对象时它就不会妨碍您。

另一种方法是通过代码添加它。在另一个 DisplayObject 中绘制该对象,将其设置为不可见,然后将其 addChild 给敌人。

A "hacky" solution might be to add a shape to the enemy that has the max size you want, then set it to be invisible. I have created hit boxes for objects that way and it worked quite well.

One way would be to add it when creating the object in the Flash IDE. Just draw it and position it as you want the shape to be, then give it an instance name, like "sizeHolder". After you create the enemy you would then call

enemy.sizeHolder.visible = false;

In the Flash IDE you could place it on another timeline, then make that timeline invisible and lock it, so it wouldn't get in your way when editing the actual object.

The other way would be to add it by code. Draw the object in another DisplayObject, set it to invisible and then addChild it to enemy.

晨敛清荷 2024-10-27 23:46:07

敌人精灵动画序列
高度会有所不同。这是的一部分
动画过程。因此
每帧的实际高度会有所不同
基于精灵的边界框。
正如您所观察到的,这是
默认行为。

方法:

  • 您目前正在遵循的方法存储高度并用于计算。(更好)
  • 正如 EyeSeeEm 所建议的,如果我理解正确的话,他有一个不可见的高度精灵背景,影片剪辑中心点正确居中,每一帧中的精灵居中包含在不可见的高度精灵边界内。

您经常会发现操作脚本中的功能无法按照您希望的方式工作。重要的是您要调整编码以促进解决方案。当编码/方法允许轻松地进行进一步的更改/扩展时,这并不是黑客行为或效率低下。

PS:

这取决于具体情况,但就我个人而言,我希望高度根据精灵动画实例是动态的,这样任何射弹击中敌人几乎在其头部上方并不会真正杀死敌人。

The enemy sprite animation sequence
will vary in height.This is part of
the animation process. Hence the
actual height per frame will vary
based on bounding box of the sprite.
As you very well observed this is the
default behavior.

The ways:

  • The One you are presently following storing height and using for calculations.(better)
  • As EyeSeeEm suggested if i understood him correctly having an invisible height sprite background with proper centering of the movie clip center point and centering of the sprite in each frame to be contained in the invisible height sprite bounds.

You will often find that you will come across features in action script not working the way you want it to.Whats important is that you adapt the coding to facilitate the solution . It is not hack y or inefficient when and coding/method allows for easily making further changes/extensions.

P.S:

This would depend on situation but personally i would rather the height be dynamic according to the sprite animation instance so any hits to the enemy just nearly over its head by projectiles doesn't actually kill the enemy.

玉环 2024-10-27 23:46:07

尝试

import flash.display.StageAlign;
import flash.display.StageScaleMode;

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

Try

import flash.display.StageAlign;
import flash.display.StageScaleMode;

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