A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: ActionScript 3 splicing an array on the main timeline from within a movieclip

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    3

    ActionScript 3 splicing an array on the main timeline from within a movieclip

    Hi, sorry if this is a dumb question but basically i am trying to splice data to an array which is on the main timeline by using code placed in a timeline within a movieclip and no matter what i try i dont seem to be able to do it i am an absolute beginner so please bare with me

    Code on timeline

    Code:
    var inventory:Array = new Array();
    
    inventory[0] = "book";
    inventory[1] 
    inventory[2] 
    inventory[3] 
    inventory[4] 
    inventory[5] 
    
    
    
    trace(inventory);
    
    for ( var i:int = 0; i < inventory.length; i++ )
    {
        if ( inventory[i] == "book" )
        {
            var bookMC:MovieClip = new oneMC();
            addChild( bookMC );
            bookMC.x = (64 * i) + ( 5 + (5 * i));
    		bookMC.y = 595;
        }
        else if ( inventory[i] == "coin" )
        {
            var coinMC:MovieClip = new twoMC();
            addChild( coinMC );
            coinMC.x = (64 * i) + ( 5 + (5 * i));
    		coinMC.y = 595;
        }
    	
    	else if ( inventory[i] == "card" )
        {
            var cardMC:MovieClip = new threeMC();
            addChild( cardMC );
            cardMC.x = (64 * i) + ( 5 + (5 * i));
    		cardMC.y = 595;
        }
    
      
    }
    code in movieclip

    Code:
    this.addEventListener(MouseEvent.CLICK,addBook); 
    
    function addBook(event:MouseEvent):void
    
    { 
    
    this.parent.inventory.push("card");
    }

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    1. What exactly do you think these lines are doing?
    Code:
    inventory[1] 
    inventory[2] 
    inventory[3] 
    inventory[4] 
    inventory[5]
    They are doing nothing. It's probably harmless as the compiler will interpret them as expressions evaluating to undefined, then throw that undefined away because you didn't tell it to do anything with it.

    2. This code does not appear to be in an event handler or function, so it will only execute once. Even if you did manage to update the inventory, it wouldn't update the screen.

    3. Why didn't you post the error message you are getting? It's probably something about "possibly undefined property inventory on DisplayObjectContainer". That is because the parent property of a DisplayObject is typed as DisplayObjectContainer which is neither dynamic nor declares an inventory. To tell the compiler that the parent is a MovieClip, you need to cast:
    Code:
    MovieClip(parent).inventory.push("card");
    4. It seems pretty silly to add a card in a method called "addBook".

    5. You shouldn't have the children manipulating parent internals at all. At the very least, the child should call a method of the parent. But more ideally, you'd either define an interface that the parent implements and set an instance of that interface in the child, then use that reference. OR use the event system to have the child notify the parent of changes.

    6. "bear with me", not "bare with me". I am not getting naked with you, I don't even know you.

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