Skip to main content
Known Participant
July 31, 2023
Answered

simmple code with addChild - but doesn't return a visible clip

  • July 31, 2023
  • 2 replies
  • 929 views

 

package {
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.utils.getDefinitionByName;

    public class Main extends Sprite {
        public function Main() {
            loadMovieClip("C_10");
        }

        private function loadMovieClip(linkageId:String):void {
            var MovieClipClass:Class = getDefinitionByName(linkageId) as Class;
            var empty:MovieClip = new MovieClipClass();
			empty.visible = true;
            empty.x = 100;
            empty.y = 100;
            empty.width = 100;
            addChild(empty);
			trace("clip "+empty);
        }
    }
}

 

trace() returns:

 

clip [object C_10]

 

but the object is not visible, even though being told to be. Why?

When alternatively I drag it to the stage, it's a simple black square, nothing wrong with it...

 

Thnks for helping out

Bettina

This topic has been closed for replies.
Correct answer kglad

Thank you helping me with my mixed up coding.

I changed it to Movieclip:

package {
    import flash.display.MovieClip;    
	import flash.utils.getDefinitionByName;

    public class Main extends MovieClip {
        public function Main() {
            loadMovieClip("C_10");
        }

        private function loadMovieClip(linkageId:String):void {
            var MovieClipClass:Class = getDefinitionByName(linkageId) as Class;
            var empty:MovieClip = new MovieClipClass();
            empty.visible = true;
            empty.x = 100;
            empty.y = 100;
            empty.width = 100;
            addChild(empty);

            trace("clip " + empty);
            trace("emptystage " + empty.stage);
        }
    }
}

Still get an

Error: Error #2136: The SWF file file:////Volumes/Macintosh%20HD/Users/2023%5Fkeep/2023%5FKUNST/schweben/AS/schweben.swf contains invalid data.
at Main/frame1()


You are trying to use the "new" constructor with a document class.  To remedy, either remove that class from your document class or remove the "new" constructor applied to that class.

 

eg, if Main is the document class and you add,

 

var whatever:MovieClip = new Main();

 

you'll trigger a 2136

 

 

2 replies

JoãoCésar17023019
Community Expert
Community Expert
July 31, 2023

Also, you can do the same thing without using getDefinitionByName like this:

 

 

 

package
{
	import flash.display.Sprite;
	import flash.display.MovieClip;

	public class Main extends Sprite
	{
		public function Main()
		{
			loadMovieClip();
		}

		private function loadMovieClip():void
		{
			var empty:MovieClip = new C_10();
			empty.visible = true;
			empty.x = 100;
			empty.y = 100;
			empty.width = 100;
			addChild(empty);
			trace("clip " + empty);
		}
	}
}

 

 

 

JoãoCésar17023019
Community Expert
Community Expert
July 31, 2023

Hi.

 

Your code works for me without a problem.

 

Maybe you're not seeing the instance because of position/registration point or because of the current frame of the main timeline?

 

Can you provide more details?

 

Regards,

JC

Known Participant
July 31, 2023

(getDefinitionByName was left over from my more dynamic script)

I also had the idea that I might be in the wrong frame, so I added stop() to my timeline in the.fla

/**/

import Main;

var main:Main = new Main();

stop();

**/

didn't help.

What puzzles me is that in the properties Export for ActionScript I cannot choose Frame 1

JoãoCésar17023019
Community Expert
Community Expert
July 31, 2023

Are you setting your Main class as your document class?