Skip to main content
Inspiring
January 24, 2018
Answered

Code error

  • January 24, 2018
  • 2 replies
  • 1074 views

Using Animate CC, I have developed hundreds of Flash animations to help teachers instruct full classes of music students.

With the decline of Flash Player, I am moving to an AIR application that can load/unload swfs with a button click.

With this code, I get the following errors (Line 14 is the “removeChild” line.):

Scene 1, Layer 'Action', Frame 1, Line 14, Column 13 1084: Syntax error: expecting colon before leftparen.

Scene 1, Layer 'Action', Frame 1, Line 15, Column 2        1084: Syntax error: expecting identifier before var.

Scene 1, Layer 'Action', Frame 1, Line 15, Column 19 1084: Syntax error: expecting rightbrace before colon.

  1. StageScaleMode.EXACT_FIT
  2. import flash.display.Loader;
  3. import flash.net.URLRequest;
  4. var Xpos:Number = 0
  5. var Ypos:Number = 40
  6. var swf:MovieClip;
  7. var loader:Loader = new Loader();
  8. //Btns universal function
  9. function btnClick(event:MouseEvent);void{
  10. removeChild(loader);
  11. var newSWFRequest:URLRequest = new URLRequest("RhythmBee/" + event.target.name + ".swf");
  12. loader.load(newSWFRequest);
  13. loader.x = Xpos;
  14. loader.y = Ypos;
  15. addChild(loader);
  16. }
  17. /// btn listeners
  18. Unit1.addEventListener(MouseEvent.Click.btnClick);

Any help will be appreciated.

This topic has been closed for replies.
Correct answer Colin Holgate

I don't think that removeChild of a Loader will unload the SWF. Doing loader.unload() should do that. Not sure if it would complain if you had never done loader.load().

In general, if you want to remove something that may not have a parent at the time, you can do something like this:

if(loader.parent) loader.parent.removeChild(loader);

Or a sloppier but easier to think of way would be:

addChild(loader);

removeChild(loader);

Nobody would see it pop on top, because it's immediately gone after that.

The reason that code in an external SWF that you load into a mobile AIR app at runtime, isn't allowed, is because of app store rules, most notably Apple's rules. If you could load any online SWF with code you could set up your own competing App Store. People would get your shell app for free, then would pay you directly for modules, cutting out Apple, Google, Amazon, and so on.

Now, ironically it's fairly easy to achieve that by using HTML and web views, and essentially Safari is doing that, every web page is a new experience, without any store approval. But we still have to live with the external code rules.

There is a way to publish server based SWFs that do need code. It's not trivial, but is possible. Should you ever need to do that.

2 replies

IGZN
Inspiring
January 25, 2018

In Line 12 there's a semicolon instead of a colon

function btnClick(event:MouseEvent);void {

should be

function btnClick(event:MouseEvent):void {

Colin Holgate
Inspiring
January 24, 2018

At the time you remove loader, you haven't yet added it, and so it has no parent, to remove from. As you're about to add it anyway, just remove that line.

Something to know about, if you load SWFs into a mobile AIR app the SWF normally can't contain any code. If you're just building an app to load onto your own mobile device, there is an interpreter mode you can use, and then SWFs with code are allowed.

If you're doing desktop (and this may apply to mobile), SWFs with code may be allowed, I'm not sure! In any case, there are security concerns you need to know about. Read through this topic to get an idea of what's involved:

Loading external swf into Air App

By the way, if your animations have no code in them you might be able to convert them to HTML5 Canvas, and then play them as part of a web page, that would also be able to work on mobile. If there is a little bit of code, you could manually convert the ActionScript to JavaScript, it need not be very hard.

BuzzyTooAuthor
Inspiring
January 24, 2018

Thanks Colin!

Regarding “remove loader” - I have programmed that as a universal button click that will need to unload any SWF that has been previously loaded as it also loads the new SWF.

Regarding code in SWFs. I am aware of that problem, but I can’t imagine why that issue exists. Why!!!

My swfs are code-rich – but at this point I am only working toward desktop functionality. So I hope things will work ok.

Thanks again

Colin Holgate
Colin HolgateCorrect answer
Inspiring
January 24, 2018

I don't think that removeChild of a Loader will unload the SWF. Doing loader.unload() should do that. Not sure if it would complain if you had never done loader.load().

In general, if you want to remove something that may not have a parent at the time, you can do something like this:

if(loader.parent) loader.parent.removeChild(loader);

Or a sloppier but easier to think of way would be:

addChild(loader);

removeChild(loader);

Nobody would see it pop on top, because it's immediately gone after that.

The reason that code in an external SWF that you load into a mobile AIR app at runtime, isn't allowed, is because of app store rules, most notably Apple's rules. If you could load any online SWF with code you could set up your own competing App Store. People would get your shell app for free, then would pay you directly for modules, cutting out Apple, Google, Amazon, and so on.

Now, ironically it's fairly easy to achieve that by using HTML and web views, and essentially Safari is doing that, every web page is a new experience, without any store approval. But we still have to live with the external code rules.

There is a way to publish server based SWFs that do need code. It's not trivial, but is possible. Should you ever need to do that.