2

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);
        }

    }

}
Jorjon
  • 5,316
  • 1
  • 41
  • 58

4 Answers4

4

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.

Sean Fujiwara
  • 4,506
  • 22
  • 34
2

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.

Max Dohme
  • 710
  • 5
  • 12
  • Yes, those are solutions that would work, but still it's strange that we ActionScript developers can't stop this default behavior. Right now I'm listening to ADDED_TO_STAGE, storing the height, and making further calculations based upon that stored height, instead of the actual one. – Jorjon Mar 08 '11 at 14:31
  • The more you use AS, the more you notice that these all-in-one classes like MovieClip were not created with programmers in mind, but are more of a relic of the pure animation era of Flash. – Max Dohme Mar 08 '11 at 14:40
  • 1
    I would not even call this "hacky", it is normal practice to maintain size. – alxx Mar 11 '11 at 08:08
1

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.

Aditya P
  • 1,862
  • 5
  • 28
  • 39
0

Try

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

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
jedierikb
  • 12,752
  • 22
  • 95
  • 166
  • No, that doesn't solve the issue. I'm not changing the windows size or something like that. – Jorjon Mar 08 '11 at 12:32