A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [RESOLVED] addChild inside manually or dynamically placed movieclip on stage

  1. #1
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971

    resolved [RESOLVED] addChild inside manually or dynamically placed movieclip on stage

    Hi,

    i have an issue in many of my games/apps that i can't understand.
    I want to add a child inside a manually or dynamically placed movieclip on stage, with a timer, every 100 milliseconds. I have this as I always do:
    PHP Code:
    var cont:container = new container();
    cont.stage.40;
    cont.stage.40;

    //code inside the timer function:
    var bub:bubble = new bubble();
    cont.addChild(bub); 
    And i get an TypeError: Error #1010: A term is undefined and has no properties. error.

    But if i add the bubble movieclip on the stage directly:
    PHP Code:
    //code inside the timer function:
    var bub:bubble = new bubble();
    addChild(bub); 
    I don't have any issue.

    AS3 is like a new language completely, I have to forget about how I did things in AS2, and adapt myself to the new ways of doing these things in AS3... thanks for the help
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    This works for me
    PHP Code:
    var cont:container = new container();
    addChild(cont); 
    cont.stage.40;
    cont.stage.40;

    var 
    bub:bubble = new bubble();
    cont.addChild(bub);
    bub.10;
    bub.10
    extra added for my testing purposes.

    That error is usually associated with an undeclared name, spelling mistake somewhere maybe.
    Did you export for actionscript both container and bubble? as linkage name of container and bubble basically.

    It will work on stage as you probably gave it an instance name to start with.

  3. #3
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Thanks for replying.

    I forgot to mention, or to post the TIMER thing. If I use the code without the timer, it works. The bubble is placed inside the container.
    But if I use a TIMER to add bubble childs like this:
    PHP Code:
    var j:Number 0;
    timer.addEventListener(TimerEvent.TIMERcreate);  
    function 
    create(e:TimerEvent):void
    {
     var 
    bub:bubble = new bubble();
    cont.addChild(bub);
    cont.bub.name "b" j;
    cont.bub.Math.random() * cont.width 1
    cont
    .bub.addEventListener(Event.ENTER_FRAMEmove);  
    function 
    move (e:Event):void
    {
      
    cont.bub.y++; 
    }
    j++;

    That same code works, if i don't add the bubble children inside the container movieclip, but I add them on the stage directly.

    I tried placing the container manually on the stage, instance name "cont", then create the var (var cont:MovieClip to set the container movieclip.
    I tried placing it dynamically, with class name "container", then creating it (var cont:container = new container(); cont.x = stage.x + 40; cont.y = stage.y + 40;

    The container is placed, but the when a bubble is added inside of it, i get the error and the bubble don't move at all.
    But it moves if I add the bubbles on the stage and not inside the container.
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    I'm surprised you get any of your AS3 stuff to work with all the errors you probably get, unless you just miss out the code when posting here.

    you need to declare the timer times, you also need to start it, peruse through this
    PHP Code:
    var j:int 0;
    //var timer:Timer = new Timer(1000, 6);// do every second, six times only
    var timer:Timer = new Timer(500);// do every half second continually
    timer.addEventListener(TimerEvent.TIMERcreate);
    timer.start();
    function 
    create(e:TimerEvent):void
    {
        var 
    bub:bubble = new bubble();
        
    bub.name "b" j;
        
    trace(bub.name);
        
    bub.Math.random() * cont.width 1;
        
    bub.addEventListener(Event.ENTER_FRAMEmover);
        
    cont.addChild(bub);
        function 
    mover(e:Event):void
        
    {
            
    bub.y++;
        }
        
    j++;

    if this does not help you and you still are getting errors then attach your fla and associated files.

    My Hobby
    Last edited by fruitbeard; 01-05-2014 at 02:18 AM. Reason: added home link

  5. #5
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Lol fruitbeard...i don't why you always have to make fun of my codes. Anyway, i have EVERYTHING declared in my .fla, I just didn't post it here because I didn't thought it was neccesary. I have exactly what you posted:

    PHP Code:
    var j:Number0;
    var 
    myTimer:Timer = new Timer(2000);
    myTimer.addEventListener(TimerEvent.TIMERcreate);
    myTimer.start();
    function 
    create(e:TimerEvent):void
    {
        var 
    bub:bubble = new bubble();
        
    cont.addChild(bub);
        
    cont.bub.name "b"+j;
        
    cont.bub.Math.random() * cont.width 1;
        
    cont.bub.addEventListener(Event.ENTER_FRAMEmover);
        
        function 
    mover(e:Event):void
        
    {
            
    cont.bub.y++;
        }
        
    j++;

    Solution:
    I noticed the problem (remember that i'm making the transition from AS2 to AS3, me falta mucho por entender what is allowed or not in AS3)
    The problem was using the "cont" in all the functions and variables. I removed it from everywhere but from "cont.addChild(bub), and that fixed all. Thanks.

    Working code:
    PHP Code:
    var j:Number0;
    var 
    myTimer:Timer = new Timer(2000);
    myTimer.addEventListener(TimerEvent.TIMERcreate);
    myTimer.start();
    function 
    create(e:TimerEvent):void
    {
        var 
    bub:bubble = new bubble();
        
    cont.addChild(bub);
        
    bub.name "b"+j;
        
    bub.Math.random() * cont.width 1;
        
    bub.addEventListener(Event.ENTER_FRAMEmover);
        
        function 
    mover(e:Event):void
        
    {
            
    bub.y++;
        }
        
    j++;

    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center