Skip to main content
March 31, 2013
Question

Adding an image to stage using actionscript

  • March 31, 2013
  • 1 reply
  • 1983 views

I am new to adobe flash cs6 and actionscript and am having trouble doing what should be a simple task. I have created a new project and document class that looks like this:

package  {

   
import flash.display.MovieClip;

   
public class MyClass extends MovieClip {

       
public function MyClass() {

       
}
}

Now all I want to do is programmatically add an image to the stage when running.

So far, I imported the image to the library which is titled mytestimage which i converted to a symbol. The mytestimage symbol has linkage to a class mytestimage which looks like this:

package  {

   
import flash.display.Sprite;
   
import flash.events.Event;

   
public class mytestimage extends Sprite {

       
public function mytestimage () {
            addEventListener
(Event.ENTER_FRAME, enterFrame);
       
}
       
private function enterFrame(e:Event😞void {

           
}
       
}

}

I then updated my document class to the following :

package  {

   
import flash.display.MovieClip;

   
public class MyClass extends MovieClip {

       
public function MyClass() {
           
var myMovieClip = new mytestimage();
            addChild
(myMovieClip);
       
}
}

and when I debug the project nothing errors and the image isn't loaded - all I see is the white stage. Can somebody help me figure out what I am doing wrong here? Thanks

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
March 31, 2013

The code is fine as you show it, so if there is a problem it might lie in how you configured things.  Did you define MyClass in the Document Class field of the Properties panel?

You say you imported the image to the library and converted it to a symbol.  Did you convert it to a MovieClip?  How did you do the conversion while it is in the library?

Try putting traces in both of your class files to make sure they are being executed, as in....

package  {

   
import flash.display.MovieClip;

   
public class MyClass extends MovieClip {

       
public function MyClass() {

            trace("Document Class works");
           
var myMovieClip = new mytestimage();
            addChild
(myMovieClip);
       
}
}

package  {

   
import flash.display.Sprite;
   
import flash.events.Event;

   
public class mytestimage extends Sprite {

       
public function mytestimage () {

            trace("image class works");
            addEventListener
(Event.ENTER_FRAME, enterFrame);
       
}
       
private function enterFrame(e:Event):void {

           
}
       
}

}