Actionscript 3 using flash cs3 Tutorial loading external swfs,pngs,jpgs or bitmaps

Wednesday, August 29, 2007 6:54

This tutorial will teach you how to load an external file that may be say on a server somewhere in the world into your application using actionscript 3 in flash cs3. This tutorial requires that you have a copy of Flash CS3 and have a basic knowledge of actionscript. Its a very simple tutorial and you should pick it up right away as each part of the code is explained in detail. Dont forget to request a tute and subcribe to our feed.


As usual i started off with our package

package {

i imported all the necessary classes i was going to need during run time

    import flash.display.MovieClip;

    import flash.display.Loader;

    import flash.net.URLRequest;

i named the class the same name as the .as file this is important for it to work correctly
 



    public class Loaders extends MovieClip { 

same name with the constructor function also 

        public function Loaders() { 

declared a new variable loader set that with Loader functionality then added it into runtime using addChild():
 

            var loader:Loader = new Loader();

            addChild(loader); 

set a new variable to get the image from a http:// adress on my server to retreive a image 

            var request:URLRequest = new URLRequest("http://www.miraztutorials.com/wp-content/uploads/image/3.jpg");
 

set the loader to load the url onto the stage at runtime

 
            loader.load(request);

 

        }

    }

}
 

That is all you need to load in any kind of swf, png, jpg or bitmap. you can also use (this.x  = / this.y =) to set the possitions of the image when placed on stage.

 

please comment

Similar Posts:

Share/Save/Bookmark

You can leave a response, or trackback from your own site.

7 Responses to “Actionscript 3 using flash cs3 Tutorial loading external swfs,pngs,jpgs or bitmaps”

  1. mike says:

    September 18th, 2007 at 12:08 am

    Great tutorial thanks im going to use this to make my gallery project

  2. Kerry Ann says:

    November 22nd, 2007 at 11:52 pm

    How do I follow the note for actionscript CS3 flash, from MX flash.
    Multimedia — Learning Resource — Version 2, November 2003 Copyright © 2003 QANTM Australia CMC Pty Ltd PAGE 1
    ICPMM15DA - DEVELOP A MULTIMEDIA SCRIPT ACTIONSCRIPTING
    Menu
    ActionScript in Flash ……………………………………………………………………………… 2
    Alternatives …………………………………………………………………………………………. 2
    Functionality of ActionScript …………………………………………………………………….. 3
    Your First Script ……………………………………………………………………………………. 3
    Creating Scripts in Flash ………………………………………………………………………….. 4
    Exercise 1 - Frame Actions ……………………………………………………………………….. 5
    Programming and Documentation Styles ……………………………………………………….. 7
    Comments …………………………………………………………………………………………… 7
    Conditions …………………………………………………………………………………………… 8
    Exercise 2 - Conditions ……………………………………………………………………………. 9
    Loops ………………………………………………………………………………………………..10
    Exercise 3 - Loops …………………………………………………………………………………11
    Exercise 4 - Loops using the Timeline …………………………………………………………..12
    Loops Using the Timeline ………………………………………………………………………….13
    Events and Triggering Actions ……………………………………………………………………13
    Creating a Custom Mouse Cursor ………………………………………………………………..15
    Exercise 5 - Custom Mouse Cursor………………………………………………………………15
    Object Properties …………………………………………………………………………………..18
    Conclusion …………………………………………………………………………………………..20
    ActionScript in Flash
    ActionScript is the computer language built into Macromedia Flash. It alows
    developers quick and easy access to variables and operations within the flash movie.
    ActionScript was first released in Flash3 and was further developed in Flash4.
    It was Flash5 however, which really transformed Flash from being an animation tool
    with some scripting ability into a powerful scripting environment. This version
    introduced, among other improvements, scripting options to allow control of other
    Flash movies from within a movie. This created a whole new way of building
    complext projects. Taking this even further, FlashMX allows sophisticated video
    input and control features that lets users integrate full motion video into their
    projects. ActionScript in FlashMX can control imported videos from within a flash
    movie. Other ActionScript options were also introduced in MX, including enhanced
    dragging and layer controls. ActionScript control for sound has also been enhanced
    in FlashMX.
    ActionScript allows you to add interactivity and functionality to a Flash movie. The
    language is quite easy to use for simple tasks such as playing and stopping movies
    and powerful enough to extend to very complex tasks such as interacting with a
    web server.
    It is recommended that you have knowledge of the non-ActionScript parts of Flash
    for the following exercises. You should be familiar with drawing, creating symbols
    (Buttons and Movie Clips), creating keyframes and exporting a movie. You can
    review the lessons included within Flash to get a better understanding of these
    areas, or refer to the notes on Flash Fundamentals from CUFIMA03A Create
    2D Digital Animation. The exercises in this unit will focus mainly on the scripting
    areas.
    Alternatives
    ActionScript is very similar to some other languages including JavaScript and C.
    The reason for this is that ActionScript and JavaScript are both based on a standard
    called ECMA-262 or ECMAScript. The advantage of this similarity is that once you
    are familiar with one language, it is quite easy to take your knowledge and apply it in
    a different area, for example using JavaScript to add interactivity to a HTML web
    page, or writing a simple program in C.
    ActionScript in Flash
    ActionScript is the computer language built into Macromedia Flash. It alows
    developers quick and easy access to variables and operations within the flash movie.
    ActionScript was first released in Flash3 and was further developed in Flash4.
    It was Flash5 however, which really transformed Flash from being an animation tool
    with some scripting ability into a powerful scripting environment. This version
    introduced, among other improvements, scripting options to allow control of other
    Flash movies from within a movie. This created a whole new way of building
    complext projects. Taking this even further, FlashMX allows sophisticated video
    input and control features that lets users integrate full motion video into their
    projects. ActionScript in FlashMX can control imported videos from within a flash
    movie. Other ActionScript options were also introduced in MX, including enhanced
    dragging and layer controls. ActionScript control for sound has also been enhanced
    in FlashMX.
    ActionScript allows you to add interactivity and functionality to a Flash movie. The
    language is quite easy to use for simple tasks such as playing and stopping movies
    and powerful enough to extend to very complex tasks such as interacting with a
    web server.
    It is recommended that you have knowledge of the non-ActionScript parts of Flash
    for the following exercises. You should be familiar with drawing, creating symbols
    (Buttons and Movie Clips), creating keyframes and exporting a movie. You can
    review the lessons included within Flash to get a better understanding of these
    areas, or refer to the notes on Flash Fundamentals from CUFIMA03A Create
    2D Digital Animation. The exercises in this unit will focus mainly on the scripting
    areas.
    Alternatives
    ActionScript is very similar to some other languages including JavaScript and C.
    The reason for this is that ActionScript and JavaScript are both based on a standard
    called ECMA-262 or ECMAScript. The advantage of this similarity is that once you
    are familiar with one language, it is quite easy to take your knowledge and apply it in
    a different area, for example using JavaScript to add interactivity to a HTML web
    page, or writing a simple program in C.
    Functionality of ActionScript
    With a fully fledged language such as ActionScript, combined with the abilities of
    Flash, there is practically no limit to the interactive applications you can create.
    ActionScript allows you control over almost all aspects of the Flash
    environment.
    Control over the Timeline
    Within Flash, the sequence of events is controlled by the timeline, which is
    a series of keyframes. ActionScript allows you to control the playback of
    the timeline, to play, stop, go back or forward, or jump to a specific frame.
    Interactive Elements
    An interactive movie responds to user input. With ActionScript, it is
    possible to create buttons that respond to a mouse click or rollover,
    elements that move or change based on keyboard presses or mouse
    movement or text fields that the user can type into.
    Content Control
    ActionScript can be used to change the properties of an element within
    your movie or create new elements. For example, you can change the
    colour, size, position and rotation of a movie clip. You can also duplicate
    existing movie clips or generate new movie clips.
    Server Communications
    ActionScript allows Flash to talk to a web server, to interact with web
    scripts, databases and other users.
    Your First Script
    The next section covers the use of a few actions to get an idea of how to use
    the scripting within Flash. When creating the scripts, spend some time
    experimenting and changing values, to get a better understanding of exactly
    how each line works. If any mistakes in the code are made, the Output window
    will be displayed and will give details of where the error is.
    Creating Scripts in Flash
    The basics of scripting in Flash are:
    • A variable holds some data, which could be a number or some text.
    For example, there may be a variable called score used to remember
    the player’s score and you can add or subtract to/from this score
    depending on the actions of the player.
    • Quotes “” are used around pieces of text, and there are no quotes
    around variable names or numbers.
    • At the end of each line, there should be a semicolon - ;
    This does not apply to lines that already end with a curly bracket.
    • Curly brackets { } are used around sections of code that must be
    grouped together. Generally the lines between the brackets are
    indented.
    • In most cases, you can put as much space between words and blank
    lines as necessary to make your code easy to read.
    Some common actions you would use within a script are:
    counter++ Adds 1 to the variable counter
    counter– Subtracts 1 from the variable counter
    counter += 5 Adds 5 (or any number) to the variable counter
    counter -= 8 Subtracts 8 (or any number) from the variable
    counter
    myNum = random(10)
    Chooses a number from 0 to 9 (not 1 to 10) and
    assigns it to the variable myNum
    trace(“Hello”) Displays the message Hello in the Output window
    trace(myNum) Displays the value of the variable myNum in the
    Output window
    trace(“My number is ” + myNum)
    Displays the message My number is xxx (where xxx
    is the value of myNum).
    Exercise 1 - Frame Actions
    1. Start a new Flash movie.
    2. Select the first keyframe on the timeline, right-click and choose
    Actions to open the Frame Actions panel.
    3. If the Frame Actions panel is split into two windows, then click
    the arrow in the divider between the columns to close the left
    panel. We do not need it and closing it will give us more room
    (see Figure 4).
    In Flash, you generally insert actions by choosing them from the
    list.
    4. From the drop down menu at the top-right of the Frame
    Actions panel, select Expert mode. You can now type directly
    into the Actions window. See Figure 5.
    Figure 4
    Figure 5
    5. In the Frame Actions panel, erase anything you may have written and
    type the script: trace (“Hello World!”); like the example provided in
    Figure 6. The trace action can be used to show some text or a value in
    the Output window. The Output window appears when you test the
    movie and it can be used to learn about scripting.
    6. Press Control+Enter, or from the main menu choose Control > Test
    Movie. Your movie will be tested (it’s blank since there is nothing on the
    Stage), but the Output window will appear and say Hello World!. (See
    Figure 7).
    7. Close the Output window and the tested movie you can press
    Control+W to return to the editing mode.
    Your Actions panel should still be visible and your Action should still be
    shown. If not, make sure the first keyframe on the timeline is selected.
    8. Add actions so your script now looks like this (replace Joe with your
    name):
    trace (“Hello World!”);
    myName = “Joe”;
    trace (“My name is ” + myName);
    score = 5;
    trace (“The score is ” + score);
    score++;
    trace (“The score is now ” + score);
    score += 5;
    trace (“The score is now ” + score);
    9. Test the movie (Control+Enter) and your Output window should say:
    Hello World!
    My name is Joe
    The score is 5
    The score is now 6
    The score is now 11
    10. Experiment with the scripts to learn more about how they work.
    Try out some of the common actions discussed on page 14.
    Programming and Documentation Styles
    Comments
    Comments are used to clarify what the code is doing. They are optional,
    but the advantages well outweigh the effort to add comments as you are
    writing code. Comments can be used if the code needs to go to another
    developer or when you return to the code at a later time. Reading through the
    comments allows you to easily identify what the code is doing.
    Within ActionScript, you add a comment by starting the line with two slashes,
    for example:
    // this line is a comment
    With regards to programming style, there are many thoughts on what is good
    and bad style. Style includes things such as:
    • the layout of the code (indenting and spacing)
    • what kind of names are given to variables
    • how comments are used within the code etc
    Conditions
    A condition is used to execute a section of code in some situations and can
    execute another section of code if the condition is not met. In code, a condition
    generally takes one of the following forms:
    if (this condition is true) {
    execute these statements
    }
    if (this condition is true) {
    execute this set of statements
    } else {
    execute this other set of statements
    }
    For example, this is pseudo-code to pick a number and see if it is 5:
    pick a random number between 0 and 9
    display the number
    if (the number is 5) {
    display a message saying “Yes that is the correct number”
    } else {
    display a message saying “No that is the wrong number”
    }
    In ActionScript, the action to create a condition is if. The action if will execute a
    section of code enclosed in {curly brackets} if some condition is true. In the
    case above, the condition is if the number is 5. This if action can be combined
    with the else action to execute a section of code if the condition is NOT true.Important!
    Notice that in the line
    if (myNumber == 5)
    that a double-equals sign is used (called the equality
    operator). The condition will not work with a single
    equals sign.
    The conditions you can use in an if statement are:
    if (myNumber == 5) myNumber is EQUAL TO 5
    if (myName == “Joe”) myName is EQUAL TO “Joe”
    if (myNumber != 5) myNumber is NOT EQUAL TO 5
    if (myNumber > 5) myNumber is GREATER THAN 5
    if (myNumber >= 5) myNumber is GREATER THAN OR
    EQUAL TO 5
    if (myNumber

  3. mode says:

    December 20th, 2007 at 4:14 pm

    so can i use the loader object twice, ie show the loaded swf,jpeg twice in my main swf?

  4. johnny says:

    April 10th, 2008 at 10:52 am

    How can I use your example to load the pictures randomly on the stage. I don’t mean to have their position random but the order in which they are loaded. Thanks in advance. :-)

  5. John Patrick Lim says:

    August 15th, 2008 at 2:43 am

    Thumbs up to a handy code. I found it working well within button functions too.

    :D

  6. Home Contents Insurance Quote Australia says:

    September 12th, 2008 at 9:25 am

    I couldn’t understand some parts of this article, but I guess I just need to check some more resources regarding this, because it sounds interesting.

  7. Eddie says:

    September 20th, 2008 at 11:43 am

    got one quation how can i close that external load image… for instance i am using swf instead of jpg with close button but cant find the way how to close that file any ideas ?

Leave a Reply