PhotoDune

Authors writing in AS3 please read this!

1234 posts
  • Bought between 50 and 99 items
  • Elite Author
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 50 and 99 users
  • Sold between 50 000 and 100 000 dollars
  • United States
MBMedia says

Remember way long ago when you wrote it AS2 how whenever you were writing a swf that would be pulled into another swf you were really careful with _root, because it changed places?

AS3 has two things like that, root (same as _root) and stage. Using either without properly thinking through the consequences can be bad. The references to stage are what I seem to be running into the most.

Why? stage is only a valid reference once the loaded swf is added to the display list, via addChild. Until then it is null. A loading swf starts executing code when the first frame is ready, and it doesn’t always make sense in many template cases to add a swf until it is fully loaded, since you don’t know it’s content.

Therefore it is good practice to not reference stage in the code of any swf that is designed for the purposes of being a part of a website rather than a full website, such as a contact page or an image viewer. Because it creates many possible problems for someone when they try to implement it.

If you need stage in your components, try running an if statement if (stage) before using it, or only using stage on the Event.ADDED_TO_STAGE event so that you know it’s in the display list. OR, simply comment the area that uses stage to make sure that the user knows how to edit it should they be intending to pull it into another swf.

After recognizing that this issue can cause people a lot of stress, I am going to start building templates in a manner that avoids the problem, as well as document the problem and put in a help file for my stuff. BUT I ’m sure it would raise customer satisfaction and lower the time we spend fixing customer problems if we all worked to make this not an issue. And we all know that both of those things are better for us all, and following good practices never hurt anybody anyway :)

Honestly, I’d like to appeal to reviewers to look for this in files as well, and hopefully implement some rules, watching for this in components that are obviously meant to be used inside other flash projects, because of the potential conflicts.

2309 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 10 and 49 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 100 and 1 000 dollars
  • United States
theflyingtinman says

Where were you last night when I was up all night trying to get a Flashden 3D Gallery to load as an external swf. I discovered ADDED _TO_STAGE, after tearing out most of my hair, about 6 hours before you posted this :)

It may also be worth noting that another a useful event to listen for when using externally loaded swfs (also dicovered last night) is REMOVED _FROM_STAGE. If your externally loaded swf uses resources that need to be freed up when the swf is removed from the stage you can do it in a REMOVED _FROM_STAGE listener

1234 posts
  • Bought between 50 and 99 items
  • Elite Author
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 50 and 99 users
  • Sold between 50 000 and 100 000 dollars
  • United States
MBMedia says

Very nice addition tinman, and very true. Sorry I was a little late for you on that :)

AS3 has yet to see many birthdays, so correct practices for different things are still up in the air, but this is something that I guarantee will save you and others some time answering questions if you apply it to your projects.

What it comes down to is that if you are making a component, you need to think about what will happen when it’s used in another flash file, and this is a very important practice to keep in mind.

When a customer comes to me with questions, I can’t tell them a solution that involves re-writing their component to run certain code on certain events, etc, since many of them are not expert coders, and that’s why they need templates. So it’s up to us to make sure our components are as usable as possible.

Also a component should not set things like stage.align, and stage.scaleMode PERIOD , since it’s parent swf may need to have certain settings on those to run correctly.

If anyone has questions about how to make components as interoperable as possible in AS3 , ask me. I know AS3 completely, inside outside and backwards, and I’d rather answer your questions and have move versatile components on flashden than have to figure out answers for every individual that buys one of my templates :) In fact, ask on this thread so that others can see answers.

730 posts
  • Bought between 1 and 9 items
  • China
  • Elite Author
  • Exclusive Author
  • Has been a member for 6-7 years
  • Referred between 10 and 49 users
  • Sold between 100 000 and 250 000 dollars
wangruyi says

a useful post.

7340 posts
  • Attended a Community Meetup
  • Community Moderator
  • Has been a member for 5-6 years
  • United Kingdom
  • Contributed a Tutorial to a Tuts+ Site
  • Won a Competition
  • Contributed a Blog Post
  • Beta Tester
  • Bought between 50 and 99 items
+4 more
MSFX moderator says

I never knew about those listeners, may try having a go later, for now I use this though..

...
swf = MovieClip(e.currentTarget.content);
addChild(swf);
swf.init()
...

where swf.init() runs the initialisation function within the loaded swf, prior to that nothing is on stage, or loaded within that swf..

When I wish to unload the swf I call swf.kill(), a function that runs through the swf removing listeners, masks, timers and anything else that may use up resources..

In CS4 this is now not neccessary as they have introduced a method stopAndUnload() which completely nukes any loaded content, lets hope it works :)

With regards to referencing the stage within my loaded swf, I dont..

I create a function called resize(sWidth, sHeight)

which when the parent swf is resized, its resize handler is called, within that I call swf.resize(stage.stageWidth, stage.stageHeight)

This seems to work pretty nicely :)

7340 posts
  • Attended a Community Meetup
  • Community Moderator
  • Has been a member for 5-6 years
  • United Kingdom
  • Contributed a Tutorial to a Tuts+ Site
  • Won a Competition
  • Contributed a Blog Post
  • Beta Tester
  • Bought between 50 and 99 items
+4 more
MSFX moderator says
Also a component should not set things like stage.align, and stage.scaleMode PERIOD , since it’s parent swf may need to have certain settings on those to run correctly.

If im right it actually wont let you, you get a runtime error for accessing the Stage class from within a child… because if you think about it, the Stage.align & scaleMode properties are defined from within the parent, it would get SO complicated allowing them to be specified within a child as well…

1234 posts
  • Bought between 50 and 99 items
  • Elite Author
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 50 and 99 users
  • Sold between 50 000 and 100 000 dollars
  • United States
MBMedia says

Thats the problem, it does get all mucked up when a child is setting align and scaleMode, but flash does allow it, and I’ve personally had people adding FlashDen bought components to my template that do it. It’s not their fault, they just bought a component, and a component should be built to be allowed to go into another swf without problems, unfortunately that is not always the case.

The solution you have used is perfect as long as you are making both the parent and child swf, and is a good thing for these authors to read. My particular problem is that I’m making templates that can accept swf files, and then people are buying components here on FlashDen, and FlashDen authors are not building AS3 components that will load into another swf correctly, and I answer the same question about error #1009 every day :)

So the idea is for this post to spread a little awareness of how to make flash components that can go inside other swfs seamlessly, since we all will benefit from that. I also truly hope that the reviewers take note and start looking for this, since it will truly make FlashDen a much better place to buy Flash components, and I believe that is an worth trying for $$$ if you catch my drift… more sales with less support…. oh yeah :)

2988 posts
  • Community Superstar
  • Has been a member for 5-6 years
  • Won a Competition
  • Sold between 50 000 and 100 000 dollars
  • Bought between 10 and 49 items
  • Referred between 50 and 99 users
  • Europe
+1 more
wickedpixel says
My particular problem is that I’m making templates that can accept swf files, and then people are buying components here on FlashDen, and FlashDen authors are not building AS3 components that will load into another swf correctly, and I answer the same question about error #1009 every day :)
Yeah.. that is a serious problem… i don’t put swf loading support for my files because of that matter… who needs swf support i teach them hot to use the thing…
A while ago i started to make all my files initialize with a public init function(to be called after the file is loaded) before using stage related actions… and i think all the people must use this way.

690 posts
  • Bought between 10 and 49 items
  • Canada
  • Exclusive Author
  • Has been a member for 6-7 years
  • Sold between 1 000 and 5 000 dollars
geoken says

If your building a component who do you want to gear it towards, an HTML developer who may not even on a copy of Flash or a Flash developer? For example, if I make a video player, I want to set stage align and add a stage resize event because this will allow an HTML developer to simply resize the file in his embed code and have it work as seamlessly as he’d expect.

This isn’t a clear cut issue because you’re going to negatively affect people no matter which choice you make. HTML devs will be negatively affected if you don’t use stage and Flash devs may be negatively affected if you do use stage.

I think the best solution is to have it access stage by default but make it easy to change this behavior. For example, I usually access stage indirectly through in intermediate variable (ie. container). So in my resize function I’d have something like this

container.width = Stage.stageWidth container.height = Stage.stageHeight //all remaining resize/alignment code reference ‘container’

Then, if someone doesn’t want to use stage, they simply break the link between container and stage by commenting out a couple of lines.

2988 posts
  • Community Superstar
  • Has been a member for 5-6 years
  • Won a Competition
  • Sold between 50 000 and 100 000 dollars
  • Bought between 10 and 49 items
  • Referred between 50 and 99 users
  • Europe
+1 more
wickedpixel says
If your building a component who do you want to gear it towards, an HTML developer who may not even on a copy of Flash or a Flash developer? For example, if I make a video player, I want to set stage align and add a stage resize event because this will allow an HTML developer to simply resize the file in his embed code and have it work as seamlessly as he’d expect.

This isn’t a clear cut issue because you’re going to negatively affect people no matter which choice you make. HTML devs will be negatively affected if you don’t use stage and Flash devs may be negatively affected if you do use stage.

I think the best solution is to have it access stage by default but make it easy to change this behavior. For example, I usually access stage indirectly through in intermediate variable (ie. container). So in my resize function I’d have something like this

container.width = Stage.stageWidth container.height = Stage.stageHeight //all remaining resize/alignment code reference ‘container’

Then, if someone doesn’t want to use stage, they simply break the link between container and stage by commenting out a couple of lines.

i have made another thing… i always put a variable setting that is named like “stand_alone”. that must be set to true if users want the file to use the entire stage size…

by
by
by
by
by