0

I have a movieclip symbol named "Main", which I am trying to use as an empty base to add other movieclips to.

I have two additional movieclip symbols, named "A" and "B", of whom both have .png images in them.

I am trying to have both movieclip symbols "A" and "B" show up dynamically when my actionscript tells them to. In the following code, movieclip "A" shows up fine, while movieclip "B" does not.

package  {

import flash.display.MovieClip;
import flash.events.MouseEvent;

public class Main extends MovieClip {

    var A:Symbol_A;
    var B:Symbol_B;

    public function Main() {
        A = new Symbol_A();
        addChild(A);

        A.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
    }

    function MouseUp(event:MouseEvent):void
    {
        B = new Symbol_B();
        addChild(B);
    }
}

}

I must be missing something!

I have read several things about addChild, but am not sure how much of it applies to me. My post here is an attempt to focus my search for information about adding movieclips dynamically, so I very much appreciate anyone who has advice on this matter.

EDIT: I no longer believe that there is anything wrong with this code. It was a silly typo that was giving me problems. I am not sure whether to delete the question, mark it answered, or just leave it be!

3
  • 1
    There is nothing wrong with this code. If you are not getting any errors, and A doesn't have another object covering it in the display list, then I guess you can try A.mouseEnabled = true but the default value is true, so unless you changed that it's likely not the issue. Otherwise your code is fine, and this won't be enough information to go off of. Commented Mar 10, 2014 at 16:27
  • Thanks very much Bennett for your comment. You stating that there was nothing wrong with my code made me discover a silly typo that was my only issue :( Thanks very much! Commented Mar 11, 2014 at 13:39
  • Excellent! glad you figured it out. Commented Mar 11, 2014 at 13:41

1 Answer 1

0

First of all check if your Symbol_A is clickable. Simply add trace("Mouse up triggered!") in your MouseUp method. Maybe some display object lays over your Main, so It can't receive MouseEvent. Also, if both Symbols include same image, you will not see difference, try to add Symbol_B in another position.

Here example for you, every time you click square (Display object with image inside, like in your case) will be added one more object at random position:

package {

    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class StackOverflow extends Sprite {

        public function StackOverflow() {
            addEventListener(Event.ADDED_TO_STAGE, onAdded);
        }

        private function setup():void {
            var simpleMc:Sprite = createWithImage(100, 100);
            addChild(simpleMc);
            simpleMc.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
        }

        private function createWithImage(width:int, height:int):Sprite {
            var result:Sprite = new Sprite();
            var bitmapData:BitmapData = new BitmapData(width, height, false, Math.random() * 0xFFFFFF);

            result.addChild(new Bitmap(bitmapData));
            return result;
        }

        private function onDown(e:MouseEvent):void {
            var addOneMore: Sprite = createWithImage(100, 100);
            //place object under that are already in the list
            addChildAt(addOneMore, 0);
            addOneMore.x = stage.stageWidth * Math.random();
            addOneMore.y = stage.stageHeight * Math.random();
        }

        private function onAdded(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, onAdded);

            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;

            setup();
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It turns out that a typo was my only problem. I do appreciate this nice example however, thank you for posting it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.