0

The plan was very simple: Using MouseEvent.CLICK to hide/show a Sprite. The first click should make it disappear, the second make it visible again.
What actually happened was really odd, as the Sprite didn't become visible when alpha was set to 1 (unless I zoom in or open the Settings menu). Here's an example: http://www.fastswf.com/8BuuY14

        private function doStuff(e:MouseEvent):void {
            (e.target.alpha == 1) ? e.target.alpha = 0 : e.target.alpha = 1;
        }

        //Sprite on the left
        var outter:Sprite = new Sprite(); //Container sprite (gray background)
        outter.x = outter.y = 20;
        outter.opaqueBackground = 0xCCCCCC;
        outter.addEventListener(MouseEvent.CLICK, doStuff);

        var inner:Sprite = new Sprite(); //Interactive child (red square)
        inner.graphics.beginFill(0xFF0000);
        inner.graphics.drawRect(0, 0, 50, 50);

        var speck:Shape = new Shape(); //Reference child (tiny black square)
        speck.graphics.beginFill(0x000000);
        speck.graphics.drawRect(50, 50, 5, 5);

        outter.addChild(inner);
        outter.addChild(speck);
        addChild(outter);

        //Sprite on the right
        var cont:Sprite = new Sprite();
        cont.x = 100; cont.y = 20;
        cont.graphics.beginFill(0xFF0000);
        cont.graphics.drawRect(0, 0, 50, 50);
        cont.addEventListener(MouseEvent.CLICK, doStuff);
        addChild(cont);

I did manage to make it work, by using alpha values equal to or larger than 0.0078125 (in true alpha value), but not 0. Why is this happening?


[EDIT]
Since I established the error could be caused by my IDE, I requested help also at the FlashDevelop community (see comments for link).

  • What do you click if your sprite's alpha is 0? There's no visible sprite at that time. – Vesper Jul 05 '15 at 04:10
  • @Vesper Display objects with [alpha](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#alpha) set to 0 are active, even though they are invisible, unlike hiding it using `display_object.visible = false;`. – akmozo Jul 05 '15 at 14:42
  • @Stefano I'm not able to reproduce the odd behavior that you have spoken. Try to use `0.0`instead of `0` and `1.0` instead of `1` ... – akmozo Jul 05 '15 at 14:46
  • @Vesper: Yes. When I add a trace to the doStuff function, the shown alpha value reflects exactly what I expected to happen, but not visually. –  Jul 05 '15 at 15:04
  • @akmozo: Thank you! I'll try it and report back. –  Jul 05 '15 at 15:04
  • Unfortunately it didn't do much. While running some extra tests, I realized that if I saved the SWF to my desktop then ran directly on FF, it would work as expected. If I uploaded to the hosting service I used to test my code or compiled directly from FlashDevelop it would not. Using `flash.system.Capabilities.version` I found out that 18.0.0.194 was being used in the version that worked. All other methods were using version 16.0.0.305. Very strange. –  Jul 05 '15 at 19:35
  • Forget about the 'wrong version' theory. I tested it directly from FlashDevelop (Tools > Settings > FlashViewer) using version 18.0.0.194 and the issue persisted. –  Jul 06 '15 at 12:39
  • @Stefano Could you tell us what's the problem exactly and how to reproduce it ? – akmozo Jul 06 '15 at 16:11
  • @akmozo I'm still unable to find a way to reproduce this error in every scenario. Here's what it look like to me: https://vid.me/GtsQ I'll keep trying to find something that makes it 100% reproducible. –  Jul 06 '15 at 20:36
  • @Stefano Hi, I reproduced the behavior, and I think that there are many factors for that problem : wmode, flash player version, ... I'm debugging the app and hope to find a logical explanation ... – akmozo Jul 07 '15 at 19:29
  • @akmozo That's great news! I found that older versions of the external player display my swf without the error (the ones I tested: 11.7.700.279, 12.0.0.77 and 13.0.0.296), while every player from version 14.0.0.176 on, contains the flawed behaviour. On the other hand I'm beginning to think that maybe it's a problem related to the FlashDevelop compilation as wonderfl.net (http://wonderfl.net/c/mYdx ) compiled the same code correctly and displayed it on a 18.0.0.194 player... –  Jul 07 '15 at 20:08
  • FlashDevelop community bug report post: http://www.flashdevelop.org/community/viewtopic.php?f=6&t=12324&sid=87cce1e7c4f9644a7918382af93d9a28 –  Jul 08 '15 at 14:07
  • @Stefano No, It's not a FlashDevelop bug, it's in the flash player. I'll put an answer just I was busy and didn't have time ... – akmozo Jul 08 '15 at 15:03

1 Answers1

0

After many tests ( on windows 7 with firefox, chrome and IE ), I can confirm that the "problem" ( the abnormal behavior, at least ) is linked to these factors :

  • Using a MovieClip, Sprite or a Shape ( and some other objects ).

  • The wmode ( when used ) : direct and gpu.

  • The Flash Player version : ActiveX, NPAPI and Standalone for the version 14 and up.

I think that's like the Flash Player is "ignoring" to render the object may be for a performance issue that's doing that ( not rendering the object when its alpha is 0 from the 1st time ), because sometimes when I used a Button or CheckBox, ... and I rollover it, a Event.RENDER event is fired on the object and the code is working one time. I saw also that the code is working when executing it for two objects in the same time ...

So to avoid that behavior, you can use the cacheAsBitmap property :

object.cacheAsBitmap = true;

You can also using a ColorTransform object when setting the alpha :

object.transform.colorTransform = new ColorTransform(1, 1, 1, (object.alpha == 1 ? 0 : 1), 0, 0, 0, 1);

Or simply, avoid to use the direct or gpu wmodes ...

Hope that can help.

akmozo
  • 9,829
  • 3
  • 28
  • 44
  • Thank you so much for taking your time and trying to help me decipher the issue. –  Jul 12 '15 at 19:08
  • It makes sense what you said about `wmode` but in my case, the tested swf wasn't embedded on an html page. Just to be sure, I tried setting `wmode` on a metatag, but it did nothing. –  Jul 12 '15 at 19:08
  • `cacheAsBitmap` seemed like a quick and easy solution, but then I bumped into an article informing of [the toll it takes on performance](http://www.bytearray.org/?p=290) and decided not to risk it. The article also suggests manual rasterization as a better alternative. Maybe I'll test it later… –  Jul 12 '15 at 19:09
  • `colorTransform` is great, but it's not actually not fully transparent, right? It's using `alphaOffset` to add a tiny amount of opacity when alpha is 0, like the solution I presented along with my question. The improvement of your solution is that it managed to get rid of the problem with a even tinier value of [true alpha](http://www.flashandmath.com/howtos/alpha/) (0.00390625 or 1/256). I took a screen shot of both solutions and reduced the brightness slightly so the "fully transparent" squares became visible: http://i.imgur.com/yQDtm4F.png –  Jul 12 '15 at 19:10
  • Addendum: After you mentioned `wmode` for the first time I came up with another workaround to fix the problem, that I'll now add to our stack, just for the record: in the constructor of the main class, draw a white (or whatever color you want your background to be) rectangle on `Main.graphics` using `stageWidth` and `stageHeight` as dimentions. –  Jul 12 '15 at 19:11