0

I'm working on a painting game, that once you click on the brushes, the mouse switches to the graphical counterpart of said brushes and will let you paint on screen. If no brush has been selected, the mouse will remain the same.

The Rectangle and the brushes are on a separate Movieclip, which allows me to layer png lines over it so you can fill in and draw.

In the actions layer in the Scene 1, this is my code for changing the mouse:

var cursor_mc:MovieClip;

if (CanvPark_mc.HugeSelected1 == true){
    cursor_mc = cursor1_mc;
}else if(CanvPark_mc.MediumSelected1 == true) {
    cursor_mc = cursor2_mc;
}else if(CanvPark_mc.SmallSelected1 == true) {
    cursor_mc = cursor3_mc;
}


stage.addEventListener(MouseEvent.MOUSE_MOVE,moveCursor);    
function moveCursor(myEvent:MouseEvent) { 
    if(CanvPark_mc.SmallSelected1 == false, CanvPark_mc.MediumSelected1 == false, CanvPark_mc.HugeSelected1 == false)
    { Mouse.cursor="auto";
    }else{
    setChildIndex(cursor_mc, this.numChildren-1);
    cursor_mc.x = (mouseX); 
    cursor_mc.y = (mouseY); 
    Mouse.hide();  
    }
}

Each brush has a boolean variable associated to it: Small, Medium and HugeSelected1, so that way, I can tell at all times in code which one is selected and which one isn't.

Right now, running this code, in the start, nothing happens, but if I click any of the brushes, this pops up in the output.

TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/setChildIndex()
    at visibilityToggle/moveCursor()[visibilityToggle::frame1:42]

Seems to be pointing specifically at

 setChildIndex(cursor_mc, this.numChildren-1);

I honestly don't know what's causing this error. I thought it would be this straight forward to change my mouse cursor.

How can I fix this?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Well, the error text says pretty clear that your child movie clip is null (cursor_mc I mean). You don't have any default value assigned to it and your condition doesn't have the "else" part, so if none of those conditions is true, the coursor_mc will be left unassigned. I suggest either editing the if-else conditions block or assigning some default value. – Nbooo Aug 16 '16 at 09:25
  • Also, with a comma in your condition only the last expression will be checked. – Nbooo Aug 16 '16 at 09:30
  • Thank you for your reply. Turned them to separate if's instead and assigned `new MovieClip()` to `cursor_mc`. But right now, it's giving me another error. `Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/setChildIndex() at visibilityToggle/moveCursor()[visibilityToggle::frame1:53]` – TwistedRaven Aug 16 '16 at 09:36
  • This error means that the object you call setChildIndex is not a parent of cursor_mc. In general, you can make something like this to avoid this error: `const theParent:DisplayObjectConatiner = cursor_mc ? cursor_mc.parent : null; if (theParent) { theParent.setChildIndex(cursor_mc, theParent.numChildren - 1) }` – Nbooo Aug 16 '16 at 09:42
  • Edit. Formatted the code. Pasting came with issues It's working and allowing me to draw on the Rectangle, BUT, the mouse is being hidden. Not actually being replaced by the cursor movieclips I created and that are off scene. Any idea why? – TwistedRaven Aug 16 '16 at 10:06
  • I suppose that you didn't add cursor_mc to the stage. At least, I don't see any related code in the question. – Nbooo Aug 16 '16 at 10:15

1 Answers1

0

Regarding the error you posted in your first post - such error occurs when some object you work with is null, i.e. not initialized or already destroyed. The error usually generalized to NPE (null pointer exception). When such error occurs, you should check if all your objects exist.

The second error occurs because your cursor_mc doesn't have parent clip (i.e. it wasn't added to stage) or the parent object is not the same object you call the setChildIndex. I suggest reading this doc

To solve the second problem you can check if the parent clip actually exists. Also, keep in mind, that if you have reassigned a value of cursor_mc you need to add it to stage again and, perhaps, you want to remove the previous clip from stage (assuming that cursor1_mc, cursor2_mc, cursor3_mc are not on stage.)

Here is a rough example:

var cursor_mc:MovieClip;

if (CanvPark_mc.HugeSelected1 == true){
    cursor_mc = cursor1_mc;
}else if(CanvPark_mc.MediumSelected1 == true) {
    cursor_mc = cursor2_mc;
}else if(CanvPark_mc.SmallSelected1 == true) {
    cursor_mc = cursor3_mc;
}


stage.addEventListener(MouseEvent.MOUSE_MOVE,moveCursor); 

function moveCursor(myEvent:MouseEvent) { 
    if(CanvPark_mc.SmallSelected1 == false && CanvPark_mc.MediumSelected1 == false && CanvPark_mc.HugeSelected1 == false)
    {
        Mouse.cursor="auto";
    }
    else if (cursor_mc)
    {
        addChild(cursor_mc);
        setChildIndex(cursor_mc, this.numChildren-1);
        cursor_mc.x = (mouseX); 
        cursor_mc.y = (mouseY); 
        Mouse.hide();  
    }
}
Nbooo
  • 865
  • 7
  • 13
  • I made a change. Added && instead of || to the if statement. Even after adding cursor_mc to the stage, it's still hiding. I don't understand. Also, by commenting out the if statement, and naming the cursor to cursor1 for testing purposes, it adds it as the mouse, but it won't paint on the Rectangle at all... – TwistedRaven Aug 16 '16 at 10:38
  • probably, it happens because the cursor_mc captures the event. Try to disable it: `cursor_mc.mouseChildren = cursor_mc.mouseEnabled = false;` – Nbooo Aug 16 '16 at 10:45
  • With the if commented, yup, that fixed the no-drawing situation. Thank you for all your tremendous help thus far, you've been a life saver. But the matter of it not showing up at all with the if not commented is still the problem. :/ – TwistedRaven Aug 16 '16 at 10:52
  • @TwistedRaven please take a look at this example, maybe it will help? Example: http://wonderfl.net/c/4fLb – Nbooo Aug 16 '16 at 11:08
  • Think he's not receiving the variables right in the initial if statements to begin with? I mean, cursor_mc is not present on the scene, it's created entirely by code. Edit : changed the initial statement before the if from `var cursor_mc: MovieClip = new MovieClip();` to `var cursor_mc: MovieClip = cursor1_mc;` and it reads it straight away. I think that's precisely the problem. I have no clue how to define it any other way though. – TwistedRaven Aug 16 '16 at 11:43