Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Loading External SWF Files: Making Things Simple... Still need help here =)

Engaged ,
Oct 13, 2009 Oct 13, 2009

Hello everyone! First of all, sorry for my english... I'm Brazilian and english is not my first language.

This is my first post meant to be my first post and I'm learning AS3. Didn't know about this forum but now I hope to have the time to visit it a lot to help and be helped! 😃

Well, I'm trying to import some external files to my main flash file with buttons. Yes, this is a newbie question, but I'm learning... I click button 1 and content 1 is loaded, click button 2 and content 2 is loaded, and so on. So, I got two ways for doing it:

edit: The code below is working fine now...

montreal.addEventListener(MouseEvent.MOUSE_UP, loadCity);
dublin.addEventListener(MouseEvent.MOUSE_UP, loadCity);
sydney.addEventListener(MouseEvent.MOUSE_UP, loadCity);
...

var box:MovieClip = new MovieClip();
addChild(box);
box.x=20;
box.y=20;

function loadCity (event:MouseEvent):void {
    var currentCity = event.target.name;
    // remove all the children of the box
    while (box.numChildren > 0) {box.removeChildAt(0);}
    var swfRequest:URLRequest=new URLRequest(currentCity+".swf");
    var swfLoader:Loader = new Loader();
    swfLoader.load(swfRequest);
    box.addChild(swfLoader);}

The problem with this first option is that whenever I click a button, it don't renew the content of the "box"... instead, the content loaded is stacked over the last one and I don't know how to clean it... The user Andrei did the trick with the "while statement" so the above code is working but I don't know how to use tween with it...

So here comes the second option:

import fl.transitions.Tween;
import fl.transitions.easing.*;

montreal.buttonMode=true;
dublin.buttonMode=true;
sydney.buttonMode=true;
rio.buttonMode=true;
paris.buttonMode=true;
london.buttonMode=true;
home.buttonMode=true;

var box:MovieClip = new MovieClip();
addChild(box);
box.x=20;
box.y=20;

var cityLoader:Loader = new Loader();
var cityURL:URLRequest=new URLRequest("montreal.swf"); //I use this to show a city when the site is opened
cityLoader.load(cityURL);
box.addChild(cityLoader);

**Here I add the listeners to button, each one calling its own function like:

montreal.addEventListener(MouseEvent.CLICK, loadMontreal);

function cityTweens():void {
    var cityIn:Tween = new Tween(box, "y", Strong.easeOut, -350, 20, 1, true);}

function loadMontreal(event:MouseEvent):void {
    var cityURL:URLRequest=new URLRequest("montreal.swf");
    cityLoader.load(cityURL);
    cityTweens();}
   
function loadDublin(event:MouseEvent):void {
    var cityURL:URLRequest=new URLRequest("dublin.swf");
    cityLoader.load(cityURL);
    cityTweens();}
   
function loadSydney(event:MouseEvent):void {
    var cityURL:URLRequest=new URLRequest("sydney.swf");
    cityLoader.load(cityURL);
    cityTweens();}
   
function loadRio(event:MouseEvent):void {
    var cityURL:URLRequest=new URLRequest("rio.swf");
    cityLoader.load(cityURL);
    cityTweens();}
   
function loadParis(event:MouseEvent):void {
    var cityURL:URLRequest=new URLRequest("paris.swf");
    cityLoader.load(cityURL);
    cityTweens();}
   
function loadLondon(event:MouseEvent):void {
    var cityURL:URLRequest=new URLRequest("london.swf");
    cityLoader.load(cityURL);
    cityTweens();}
   
function loadHome(event:MouseEvent):void {
    var cityURL:URLRequest=new URLRequest("home.swf");
    cityLoader.load(cityURL);
    cityTweens();}


Well, the second option "is" working but I have some problems with the Tween... I have to wait the tween to finish before clicking another button or the tween will bug... I don't know how to disable the buttons until the tween is finished =/

Now, I used to have one tween for each function but now I got it inside a function and just call it inside each function. Simple for you but I was like WOW when I had the idea and put it to practice! But I'm still repeating a lot of codes here with the "cityLoader.load(cityURL);" but when I try to put it inside the function "cityTweens" it will just open the Montreal City, maybe because I'm calling it as soon as the site opens...

I'm almost sure I can make things simple, like mixing the idea of the first code (currentCity+".swf") so I don't need to call a function for each button. I just don't know how to do it...

Could anyone help me? I'll also be VERY happy if you point me to any tip like good practices that I'm not following.
Thanks in advance!

Message was edited by: newToAS3

TOPICS
ActionScript
2.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Oct 17, 2009 Oct 17, 2009

Below are adjustments to the code that deal with it. This is not a full code - make full changes yourself.

import flash.events.EventDispatcher;
// hold all the buttons in an array
var buttons:Array = [montreal, dublin, sydney, rio, paris, london, home];
// add listeners
addListeners();
// function that adds listeners
function addListeners() {
     for (var k:String in buttons) {
          EventDispatcher(buttons).addEventListener(MouseEvent.CLICK, loadSWF);
     }
}
// removes event listeners
function remove

...
Translate
LEGEND ,
Oct 13, 2009 Oct 13, 2009

As I said in your previous post, removing the child would not do the trick. Technically, you need to remove it after the tween is finished.

In any case there are several issues with how you approach the whole thing. First of all, if you target your application to be on Internet - swf loading will not happen instantly - thus you, perhaps, need to consider waiting for swfs to be loaded before you doing anything with them. But if you just do it on local machine - you don't really need for them to show up. On the other hand, even with local environment it makes sense to take into account asynchronous/unpredicatable nature of external file loading.

Also, your code can be more comact and the same functionality delegated to a single function. The code below demonstrates one of the ways. This is not the bes way though because it forces to load objects again although they were loaded already. But this is another story. I did not check code in action, of course (this is just an idea) - it is up to your to work out bugs:

import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.events.Event;

montreal.buttonMode = true;
dublin.buttonMode = true;
sydney.buttonMode = true;
rio.buttonMode = true;
paris.buttonMode = true;
london.buttonMode = true;
home.buttonMode = true;

var box:MovieClip = new MovieClip();
addChild(box);
box.x=20;
box.y=20;

var cityLoader:Loader = new Loader();
// this line will allow to wait untill content is loaded
cityLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
var cityURL:URLRequest = new URLRequest("montreal.swf"); //I use this to show a city when the site is opened
cityLoader.load(cityURL);
// don't need the following line
// box.addChild(cityLoader);

// make it a single listener
montreal.addEventListener(MouseEvent.CLICK, loadSWF);
dublin.addEventListener(MouseEvent.CLICK, loadSWF);
sydney.addEventListener(MouseEvent.CLICK, loadSWF);
rio.addEventListener(MouseEvent.CLICK, loadSWF);
paris.addEventListener(MouseEvent.CLICK, loadSWF);
london.addEventListener(MouseEvent.CLICK, loadSWF);
home.addEventListener(MouseEvent.CLICK, loadSWF);

// the single function handles all the loading
function loadSWF(e:MouseEvent):void {
     switch(e.currentTarget) {
          case montreal:
              cityURL = new URLRequest("montreal.swf");
          break;
         
          case dublin:
              cityURL = new URLRequest("dublin.swf");
          break;
         
          case sydney:
              cityURL = new URLRequest("sydney.swf");
          break;
         
          case rio:
              cityURL = new URLRequest("rio.swf");
          break;
         
          case paris:
              cityURL = new URLRequest("paris.swf");
          break;
         
          case london:
              cityURL = new URLRequest("london.swf");
          break;
         
          case home:
              cityURL = new URLRequest("home.swf");
          break;
     }
     cityLoader.load(cityURL);
}

function onLoadComplete(e:Event):void {
     loadedContent = e.target.content;
     // assuming that previous content will be moved out -
     // place new content behind previous one
     box.addChildAt(e.target.content, 0);
     // now you can start tweening
     // if there are more than one child
     if (box.numChildren > 1) {
          cityTweens();
     }
}

function cityTweens():void {
     // tween the topmost object
      var cityIn:Tween = new Tween(box.getChildAt(box.numChildren - 1), "y", Strong.easeOut, -350, 20, 1, true);
     // wait until motion finished
     cityIn.addEventListener(TweenEvent.MOTION_FINISH, tweenFinished);
}

function tweenFinished(e:TweenEvent):void {
     // make Tween instance eligible for arbage collection
     e.target.removeEventListener(TweenEvent.MOTION_FINISH, tweenFinished);
     // now you can remove tweened object from the box
     box.removeChild(e.target.obj);
     e.target.obj = null;
}

Edited some code.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 14, 2009 Oct 14, 2009

Gosh, I have soooo much to learn =/

This AS thing is totally crazy, but I hope to get there!

Andrei, thank you very much again! I didn't have the time today (and won't have tomorrow) to try all the code you've posted... Also, I'll need some time to understand everything... The switch thing is awesome! =D

btw, I do want to give you the points for all your effort in helping me. I'm leaving the topic opened as unanswered and I'll try your coding to give you a feedback later!

This should be a website, so I know that I should use something like a loading bar... I've followed some some tutorials and managed to make one to open the website but have no idea about how to make one that I could use with everything else inside the website... Anyway, don't want to take more of your time with this... I'll do a search here and I'm sure I'll find something useful 😃

Thanks again Andrei!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 14, 2009 Oct 14, 2009

You are welcome.

Since you are new to AS and have no attachments to timeline coding, I would like to suggest you learn what is called OOP approaches from the beginning. This involves learning how to use classes and architect framework for your applications. This will allow you to stay out of timeline altogether which is a good and extremely convenient thing. You will realize that learning classes gives you much more power and flexibility as well as ability to recycle your code. Case in point is your thinking about reusing loading bar - you think in a right direction.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 15, 2009 Oct 15, 2009

Hi again Andrei. Thanks again for your message!

Well, I have to tell you that I already tried to take a look at Object Oriented Programming in AS3 but man, it's so complicated to understand. It's really hard to understand the basics, to get started with external classes, which I think is the base of everything. The file should have the same name of the class, then you have things that should be public, others are private and classes that extends other classes... wow, it's just too confusing. Even the linkage properties inside Flash and what should I put in the "base class" area makes no real sense yet... but I'm trying and willing to get there!

It's hard to know where to start and here where I live, in Brazil, it's difficult to find a place to learn AS3... We have some courses of AS2 but AS3, just to learn the basics... At university my "Flash teatcher" don't know a thing about AS3... Everything she teatch us to do in AS2 I try to do in AS3 =/

Well, I've heard before that's a good idea to start with OOP so, I'm willing to do so. I just don't know where to start... Could you, please, tell me of, like two books that I could buy, one for a true beginner like me and a second one to complement my studies?

I've seen that lynda.com has some courses about OOP and at the internet I'm always looking for video tutorials...do you know those courses from lynda.com? do you think it's a good idea? Well, you already helped me A LOT and I'm really thankful for the time you've taken to respond to my posts.

If you got enough of this, that's ok for me 😃

And as I could imagine, I didn't have the time to check the code yet, but I will as soon as I can!

Thanks once more Andrei!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 15, 2009 Oct 15, 2009

Hey,

I hear Linda.com is pretty good for beginners. Although I see a value in them, video tutorials are not for me - I am too impatient.

Out all of the books that I read/looked at on ActionScript I am a fun of Essential ActionScript 3 by Colin Moock. It is very comprehensive and covers both basics and advanced things.It is intimidating huge but the truth is that you don't have to make a mission to read it all. Somehow it combines two sides - as an introduction to the most important aspects of AS/OOP and, later, as a reference. In any case, it is impossible to expect to understand and remember all things - letting your brain absorb whatever it can is the key.

If you don't have an access to books in store - you can download them from O'Reilly (publisher) website.

This doesn't mean this is the only good source. I am sure there are other great books.Perhaps, reading what people say on Amazon can help.

Also, while getting deeper into programming you will realize that, as with learning and becoming an expert in anything, memorization is not as important as method and knowing where to find information. When there - Adobe documentation will cover 90% of your needs - after all there is a limited amount of information for programming languages are not created by Gods. The rest will come from learning more general concepts of programming and software design no matter language.

Regarding the first part of your post. Actually using classes outside of timeline is more elegant and straightforward than learning Flash IDE and timeline. I believe what makes it confusing is whole old Flash timeline approach. This is not to say Flash IDE comes sometimes handy but in the vast majority of cases it is much easier and faster to accomplish things with just code.

I suggest you look into FlashDevelop. It is an open source AS development tool that clears up one's mind, makes you break free from timeline code and start writing really well designed applications.

And is helpful most of the time to ask away here, on the forum. People are willing to explain/share basic and not so basic concepts.

So, what is confusing you? Want to start a new thread?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 16, 2009 Oct 16, 2009

Hello Andrei!

First of all, thanks yet once again for your reply 😃

Well, I love video tutorials but I'm willing to try a book since it's easier to get back to topics, mark topics, take notes and all that. Maybe it's just what's missing for me to get really started.

I do believe I'll be able to find this book in here. We have some great bookstores here with manybooks in other languages. And e-books are not for me. So I'll try to buy one.Maybe this is not the right one for me yet... the two first comments we read at amazon about this book says:

A lot of content...but so @#&% frustrating "and" Fantastic, if you know your way already. I'm not sure if I already know my way =/

One thing I already noticed in me is that I just can't sit and write all the code I already understand and know, I mean, I already know some codes, understand them, but when it comes to sit down and write my codes I discover I don't know all of it by heart. I always have to get back to some notations to accomplish some tasks. Thanks for letting me know that this is normal hehe. And I think I'll improve with time. =P

Perhaps you have a good way to keep track of what you're learning... a way to take notes or something. Anyway, what works for you maybe not work for me, so...

Working with the timeline seems to be easier (even more logical) for me but I know it's not the best way to do things. If I want to learn something, I try to do it the best way I can... I already work with applications like Photoshop, InDesign, Illustrator (I'm a prepress professional) and I feel more confortable looking to the Flash IDE than to a lot of codes =P

I've learned a little of xhtml, css and now Flash and already getting into AS3, the thing I'm loving the most about web. Maybe I should have studied this in the first place haha =P

Anyway, tomorrow I'll have the time to take a deeper look into your code ok? I'll give you a feed back and close this topic. I'll try to buy a book, maybe even this one from Colin Moock. Read it and probably start loads of topics to ask you new questions huhauahuah... for now, I have to get back to my notes and doubts before starting a new topic.

Gosh! I write to much =/

Thanks again and I guess I they won't let me give you more points (you deserve it), but know that your last post was equally helpful to me ok? 😃

See ya

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Oct 15, 2009 Oct 15, 2009

Bom dia,

If you feel comfortable reading English (seems like you do quite well, Portugues is native I assume), I suggest reading Colin Moock's book Essential Actionscript 3. It will be confusing the first time through (though he's a good writer) but as you go through it the second and third time, it gets easier and easier. Colin also has a two-video walkthrough of the book in which he does a good job of being conversational and aiming at the new AS3 programmer, explaining OOP principles in ways that make sense to first-time users. Unfortunately, it's a bit expensive. But if you want to get the basics, it's an enjoyable view that's something like 12-14 hours. http://oreilly.com/store/law.csp?CMP=OTC-orm-law-fitc-toronto

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 16, 2009 Oct 16, 2009

Bom dia John!

Yes, portuguese is my native language, so until now  your "bom dia" was the easiest thing for me to understand =D

The hardest thing for me is (and always was) write in English. I don't have many problems about reading it. Anyway, I have dictionaries in case I need them hehe =P About the book, I already know where I can find it here. Since I live in São Paulo, I knew I wouldn't have a problem in finding it 😃

I just checked the link and saw the sample videos. That looks awesome but those guys are crazy... they love errors? Don't tell me you all love errors too?! =/ Ah, and do you think they have a version with subtitles to the bald guy? I couldn't understand a word of what he said... my bad I know =(

Well, the course is not "so" expensive I think... a good course of Photoshop would cost me more than that for example. So, if it's a good course, the price is fine. And I could watch it via Streaming or a friend could buy me a copy and bring me at the end of this year. But one step at a time. I think I'll stick with the book for now or things could get overwhelming =/

I'll keep your sugestion in mind too John. Thanks! 😃

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 17, 2009 Oct 17, 2009

Andrei, just checked your code and it's working just fine 😃

Just had to add a "var" before this like that was giving a error

loadedContent = e.target.content;

I just don't know how to remove the listeners from the buttons until the tween finishes =/

Anyway, thank you very much! 😃

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 17, 2009 Oct 17, 2009

Below are adjustments to the code that deal with it. This is not a full code - make full changes yourself.

import flash.events.EventDispatcher;
// hold all the buttons in an array
var buttons:Array = [montreal, dublin, sydney, rio, paris, london, home];
// add listeners
addListeners();
// function that adds listeners
function addListeners() {
     for (var k:String in buttons) {
          EventDispatcher(buttons).addEventListener(MouseEvent.CLICK, loadSWF);
     }
}
// removes event listeners
function removeListeners() {
     for (var k:String in buttons) {
          EventDispatcher(buttons).removeEventListener(MouseEvent.CLICK, loadSWF);
     }
}

function loadSWF(e:MouseEvent):void {

     // remove listeners
     removeListeners();
     // the rest of code goes here
}

function tweenFinished(e:TweenEvent):void {
     // add listeners back
     addListeners();
     // make Tween instance eligible for arbage collection
     e.target.removeEventListener(TweenEvent.MOTION_FINISH, tweenFinished);
     // now you can remove tweened object from the box
     box.removeChild(e.target.obj);
     e.target.obj = null;
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 18, 2009 Oct 18, 2009
LATEST

Gosh! I have to learn how to think logically =/

The trick is to remove the listeners when the loadSWF runs... not that I knew how to do it but I should at least have thought about it =/

Thanks Andrei for teatching me all this tips and how to make buttons from a array 😃

I know how an array works but can't think about the many ways it can be used =/ I guess it will take some time to start thinking the way I should

thanks once again! 😃

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines