A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: pushing a child object into an array [Help]

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    22

    pushing a child object into an array [Help]

    i want to add a movie clip at a certain time and then add more movieclips of the same class, but later on i want to be able to remove any of those movieclips here is my code sofar:
    for (var i:int = 0; i < elementclicked.length; i++)
    {
    if (event.target.name == elementclicked[i])
    {
    if (elementused[i] == false)
    {
    elementused[i] = true
    var rod:rodMain = new rodMain
    rodnum[i].push(rod)
    addChild(rod)

    }else{
    elementused[i] = false
    removeChild(rodnum[i])
    }

    }
    and i get this error:
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at test/continentClicked()
    i am sure this is a stupid question but i am still new to this stuff. Thanks upfront!

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You can only remove a child, when it is there.

    }else{
    if(rodnum[i] != null)
    {
    elementused[i] = false
    removeChild(rodnum[i])
    }
    }
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    here is the proper code that works
    thank you cancerinform for help
    /////////////////////////////////////////////////
    import flash.display.MovieClip;

    var myMC:Vector.<MovieClip> = new Vector.<MovieClip>
    var index:int = 0
    container.addEventListener(MouseEvent.CLICK, fl_clicked);

    function fl_clicked(event:MouseEvent):void
    {
    if (event.target == container)
    {
    var rod:c = new c ();
    rod.x = container.mouseX
    rod.y = container.mouseY
    myMC[index] = rod
    index +=1
    container.addChild(rod)
    }else
    for (var i:int = 0; i < myMC.length; i++)
    {
    if (event.target == myMC[i])
    {
    container.removeChild(myMC[i])
    return;
    }
    }
    }
    ////////////////////////////////////////////////////////

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