防止 DisplayObject 的高度变化
有没有办法防止 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这与您实例化整个 SWF 的事实有关,并且必须经过 1 帧才能将其同步到主 swf。我要做的就是导出 .fla 中的符号,然后在 Flex 中使用“嵌入符号”语法:
在这种情况下,即使在第一个
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:
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.一个“hacky”解决方案可能是向敌人添加一个具有您想要的最大尺寸的形状,然后将其设置为不可见。我已经用这种方式为对象创建了命中框,效果非常好。
一种方法是在 Flash IDE 中创建对象时添加它。只需绘制它并将其放置在您想要的形状中,然后为其指定一个实例名称,例如“sizeHolder”。创建敌人后,您可以调用
在 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
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.
方法:
您经常会发现操作脚本中的功能无法按照您希望的方式工作。重要的是您要调整编码以促进解决方案。当编码/方法允许轻松地进行进一步的更改/扩展时,这并不是黑客行为或效率低下。
PS:
这取决于具体情况,但就我个人而言,我希望高度根据精灵动画实例是动态的,这样任何射弹击中敌人几乎在其头部上方并不会真正杀死敌人。
The ways:
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.
尝试
Try