SimpleButton Positions Being Inconsistent

Hello,

I am modernizing an AS3 game and have stumbled upon an issue with placing multiple SimpleButtons in a MovieClip class. The intention here is to create a dialog box with 3 different options.

button1 = new SimpleButton(upState);
button1.x = 245.05;
button1.y = 149.3;
this.addChild(button1);

button2 = new SimpleButton(upState);
button2.x = 140.2;
button2.y = 149.3;
this.addChild(button2);

button3 = new SimpleButton(upState);
button3.x = 35.35;
button3.y = 149.3;
this.addChild(button3);

For some reason, the creation of another button makes the previous button move to it’s position when the three should all be separated. You can’t tell from here, but the buttons are slightly transparent. I know all three are being rendered on top of each other as the border is darker than it should be.
Screenshot 2023-04-27 143233

If I remove button3's x and y declarations to default to (0, 0), button1 and button2 also shift there. I have also hidden button1 and button3 here for comparison. (The MovieClip is held within another.)

Screenshot 2023-04-27 144035

button1 = new SimpleButton(upState);
button1.x = 245.05;
button1.y = 149.3;
this.addChild(button1);
button1.visible = false;

button2 = new SimpleButton(upState);
button2.x = 140.2;
button2.y = 149.3;
this.addChild(button2);

button3 = new SimpleButton(upState);
this.addChild(button3);
button3.visible = false;

Additional details that might help: I am using the latest version of OpenFL and Lime. I am exporting to HTML5. The button states are .SVGs loaded into Shapes. I am using a GlowFilter on them. I originally removed this.addChild(button3) in the last snippet, it completely removed the other buttons.

Edit: I’ve temporarily fixed my problem by creating two identical yet separate states, but that seems like bad practice.

No, it’s good practice. Normally, in Flash or OpenFL, a display object may have one parent only. If you were to try adding upState to two different parents of type Sprite instead of two instances of SimpleButton, you wouldn’t be able to successfully call sprite1.addChild(upState) then sprite2.addChild(upState) and have upState render inside both sprites. That’s true in Flash as well as OpenFL.

With that in mind, if Flash/AS3 allowed you use the same upState with multiple SimpleButton instances, that’s really surprising to me. It goes against how every other display object works in Flash. Ideally, we’d emulate that behavior somehow (I guess maybe with BitmapData.draw()?), but obviously we didn’t implement it like that yet because it’s really quite unexpected.

You should definitely do something like this (I’d recommend this even if we were to “fix” the behavior):

var upState1 = new Whatever();
var button1 = new SimpleButton(upState1);
var upState2 = new Whatever();
var button2 = new SimpleButton(upState2);
1 Like

Alright, thanks so much.