-2

I've been learning AS3 for a class project for just over a few months, most of what I know is by looking at how others problems were solved on here.

To the point.

I've got a class called Generate so I import that to my main timeline, in that class it creates a child and adds it.

Now I can't figure out how to remove it, it always says it's null: "TypeError: Error #2007: Parameter child must be non-null."

package  {

import MonsterOne;
import MonsterTwo;
import MonsterThree;
import MonsterFour;
import flash.display.*;

public class Generate extends MovieClip{

    public static var monsterID = String(monsterID);
    monsterID = Math.ceil(Math.random() * 4).toString();

    public function Generate(parent:Object){
        if (monsterID == 1){
            var monsOne:MonsterOne = new MonsterOne();
            monsOne.name = "monsterOne";
            parent.addChild(monsOne);
            monsOne.x = 100;
            monsOne.y = 200;
            trace("spawn1");
        }

This is how it's generated. There is a little more code for monsTwo, three and four. Same code different names. That's all that is in the file.

I've tried all sorts to remove the code but always with an error and no actual removal of the child.

stage.removeChild(monsOne);
monsOne.parent.removeChild(monsOne);
removeChild(monsOne);

And who knows how many others.

Am I missing something or just doing it completely wrong.

Thank you

--EDIT--

if(o.monHP <= 0) {
    turnTimer.stop();
    turnTimer.removeEventListener(TimerEvent.TIMER, CounterA);
    var monsOne:MovieClip = getChildByName('monsterOne') as MovieClip;
    var monsTwo:MovieClip = getChildByName('monsterTwo') as MovieClip;
    var monsThree:MovieClip = getChildByName('monsterThree') as MovieClip;
    var monsFour:MovieClip = getChildByName('monsterFour') as MovieClip;
    parent.removeChild(monsOne);
    parent.removeChild(monsTwo);
    parent.removeChild(monsThree);
    parent.removeChild(monsFour);

    gotoAndStop('win');
}

How I'm trying to remove the child.

package  {

import MonsterOne;
import MonsterTwo;
import MonsterThree;
import MonsterFour;
import flash.display.*;

public class Generate extends MovieClip{

    public static var monsterID = String(monsterID);
    monsterID = Math.ceil(Math.random() * 4).toString();

    public function Generate(parent:Object){
        if (monsterID == 1){
            var monsOne:MonsterOne = new MonsterOne();
            monsOne.name = "monsterOne";
            parent.addChild(monsOne);
            monsOne.x = 100;
            monsOne.y = 200;
            trace("spawn1");
        }

        if (monsterID == 2){
            var monsTwo:MonsterTwo = new MonsterTwo();
            monsTwo.name = "monsterTwo";
            parent.addChild(monsTwo);
            monsTwo.x = 100;
            monsTwo.y = 200;
            trace("spawn2");
        }

        if (monsterID == 3){
            var monsThree:MonsterThree = new MonsterThree();
            monsThree.name = "monsterThree";
            parent.addChild(monsThree);
            monsThree.x = 100;
            monsThree.y = 200;
            trace("spawn3");
        }

        if (monsterID == 4){
            var monsFour:MonsterFour = new MonsterFour();
            monsFour.name = "monsterFour";
            parent.addChild(monsFour);
            monsFour.x = 100;
            monsFour.y = 200;
            trace("spawn4");
        }
    }
}

} Full Generate File

  • You can use, for example, [`getChildByName()`](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#getChildByName()) to get your object then you can remove it ... – akmozo Feb 03 '16 at 13:35
  • 1
    To remove the `monsOne` use `parent.removeChild(monsOne);` – Daniil Subbotin Feb 03 '16 at 14:08
  • @akmozo I've tried that still get the #2007 error. I Used (http://stackoverflow.com/questions/24040609/action-script-3-how-to-remove-child-which-is-created-from-another-function). Thanks though. Any other Ideas? – Richard Barrett Feb 03 '16 at 14:13
  • @subdan I still get the #2007 Parameter child must not be non-null error using that. Thanks, any other ideas? – Richard Barrett Feb 03 '16 at 14:14
  • @Richard Barrett Can you show all code of the Generate.as file? – Daniil Subbotin Feb 03 '16 at 14:15
  • @RichardBarrett The problem here is not the idea, all these functions can do the job, but the problem is how did you used them ? – akmozo Feb 03 '16 at 14:16
  • @Richard Barrett Do you call `parent.removeChild(monsOne);` outside the constructor? – Daniil Subbotin Feb 03 '16 at 14:19
  • @akmozo **-- add it to the main post --** Is one way, obviously it's a mess at the moment. – Richard Barrett Feb 03 '16 at 14:20
  • @sudban I added it all as I said it's pretty much just a quick repeat, i'm trying to remove the child on the main timeline of my main flash document, not in the Generate.as – Richard Barrett Feb 03 '16 at 14:25
  • @RichardBarrett Try to remove `parent` from `parent.removeChild(monsOne);`. Use `removeChild(monsOne);`. – Daniil Subbotin Feb 03 '16 at 14:27
  • @subdan "TypeError: Error #2007: Parameter child must be non-null." Same thing, no matter what I try. I use `new Generate(parent);` – Richard Barrett Feb 03 '16 at 14:30
  • @RichardBarrett What's `parent` in the `Generate` class ? Is it the same `parent` of the "remove" block ? – akmozo Feb 03 '16 at 14:30
  • @RichardBarrett How do you create an instance of the Generate class? Like this? `new Generate(this);` – Daniil Subbotin Feb 03 '16 at 14:31
  • As I used `new Generate(parent);` I assumed that I was linking them that way? Did I misconstrue the meaning? As I said I'm very new a month or so of learning, mostly of my own back as we only have 4 months to do the project with little to no instruction, I believe I was a little over ambitious. – Richard Barrett Feb 03 '16 at 14:35
  • @RichardBarrett Can you upload the full source code with FLA? – Daniil Subbotin Feb 03 '16 at 14:40
  • @subdan I take it you mean though a 3rd party site? I'll zip it all up in a minute. – Richard Barrett Feb 03 '16 at 14:41
  • @RichardBarrett Yes, 3rd party site. – Daniil Subbotin Feb 03 '16 at 14:42
  • @subdan [link](http://s000.tinyupload.com/index.php?file_id=01116778013312458133) I can use a different one if prefer that was just first one I saw on google. The removal code is right at the bottom of the 6th frame 'fight' label – Richard Barrett Feb 03 '16 at 14:45
  • @RichardBarrett Replace `new Generate(parent);` with `new Generate(this);`. Add `if (monsOne)` before `removeChild(monsOne);` for every monster. – Daniil Subbotin Feb 03 '16 at 14:59
  • @subdan Awesome thank you! I take it I got the parent idea way wrong. If you set it as an answer I'll upvote it!(or not I can't) Thank you so much! Now to sort the rest of it out haha! – Richard Barrett Feb 03 '16 at 15:05

2 Answers2

2

There's so many things wrong with your code that it's not easy to know where to begin. You lack very basic understanding of the display list and most important scope.

Display List:

when you do something like this:

var monsOne:MovieClip = getChildByName('monsterOne') as MovieClip;
parent.removeChild(monsOne);

That can be translated to "no idea what I'm doing here". Why? Because the code means it expects a DisplayObject named "monsterOne" to exist in the display list of "this" (the display list in scope) and then tries to remove that object from "parent". None of this makes sense, if the object exist in display list A then remove it from display list A not from display list B.

Programming:

You are using variable and parameters that mirror existing properties, ex: "parent". Are you sure at your level of programming you can afford to confuse yourself with that kind of dangerous programming behavior? The answer is no, don't mirror properties names.

Scope:

You keep losing scope all over the place but keep writing code like scope doesn't even exist. Stop that. A variable named monsOne in one scope doesn't exist in any other scope, period.

More weirdness:

public static var monsterID = String(monsterID);
monsterID = Math.ceil(Math.random() * 4).toString();

You do not know what this does obviously or else you would not do it that way.

What does your code do?:

It creates one and one only movieclip type and each time it creates it it gives it the exact same name and add it to a phantom "parent" we know nothing about. Then when you try to remove it you don't even know or remember what this phantom parent is and you can't really use getChildByName() either cos you need to know the parent in order to do that and even worse by then so many MovieClip might be there and all have the same name.

How to fix?

This section will be filled later as you ask specific questions on how to fix what.

BotMaster
  • 2,233
  • 1
  • 13
  • 16
  • It's working now, but thank you for your input, the name was set in another file. Generate.as `var monsOne:MonsterOne = new MonsterOne(); monsOne.name = "monsterOne";` As I said I am new, I know the code isn't particularly good. But it's a design course not a programming course. `public static var monsterID = String(monsterID); monsterID = Math.ceil(Math.random() * 4).toString();` Is to set a number randomly between 1 and 4 and sets it to a string so I can call it else where. It does exactly what I want, I don't see the problem? Thanks for looking into it though. – Richard Barrett Feb 03 '16 at 15:12
0

The Generate class generates only one monster. In the gameOver function you are trying to remove all 4 monsters and you get an error that the monster doesn't exist. You should add condition:

if (monsOne) removeChild(monsOne);
if (monsTwo) removeChild(monsTwo);
if (monsThree) removeChild(monsThree);
if (monsFour) removeChild(monsFour);
Daniil Subbotin
  • 6,138
  • 5
  • 20
  • 24
  • Wonderful that you get an answer and already accepted it, too bad that your code is still very much not working and far from being fixed. – BotMaster Feb 03 '16 at 15:08
  • @BotMaster I accepted it because it works.. We sorted it out in the comments of the main post. Thank you for your input below though. – Richard Barrett Feb 03 '16 at 15:45