Main Content

You are here:

Using an Image from the Library

Hello and welcome. Here is how we left our circle last time:

package {
        import flash.display.*;
        import flash.events.*;
        public class graphical extends MovieClip {
                public var mc1:MovieClip = new MovieClip();
                public function graphical() {
                        mc1.graphics.lineStyle(1);
                        mc1.graphics.beginFill(0xff0000);
                        mc1.graphics.drawCircle(200,200,50);
                        mc1.addEventListener("click",click_handler);
                        this.addChild(mc1);
                }
                private function enterFrame_handler(e:Event):void {
                        mc1.x += 3
                }
                private function click_handler(e:Event):void {
                        if(mc1.hasEventListener("enterFrame")){
                                mc1.removeEventListener("enterFrame",enterFrame_handler);
                        } else{
                                mc1.addEventListener("enterFrame",enterFrame_handler);
                        }
                }
        }
}

But supposing we don't want a circle! We want a nice picture of a car or something! Well, your first step is to , something that I am not going to do for you. While I'm waiting, I should add that you will be relieved to know (so am I) that we will actually cut down on the code used. Hooray!

Today you will learn:

So, presumably you have found your image by now. Copy and paste it onto the main stage, and press F8 to convert it into a Movie Clip. What you call it doesn't matter. Next you need to link it .That tutorial is slightly out of date though, but all you need to do is check the export for ActionScript box, and in the box labelled "Class:", type "car_image". eg.

Linkage box

Now you must delete the image on the stage, otherwise we'd have two of them.Got it? Good. Now we move to the ActionScript.

Basically, instead of drawing a circle, we want to draw a predefined car image. So you can get rid of the lines that deal with drawing the circle:

mc1.graphics.lineStyle(1);
mc1.graphics.beginFill(0xff0000);
mc1.graphics.drawCircle(200,200,50);

DELETE. and delete the mc1.graphic.endFill() line as well. Oh good, they're gone. Instead we need only to replace them with one line:

mc1 = new car_image;

"car_image" is the name you linked the symbol to. In fact, that is it. Done. Pretty simple, eh? But you can see that could be a very powerful way of making bullets appear, or something like that. For all you lazy people, the finished code is at car.txt, but really it isn't very different. I should add that if you didn't want it at the top of the screen, or at the left of the screen, you could use:

mc1.x = 30;
mc1.y = 50;

See you next time,

Harry.

Comments