0

I've made a new question since it makes sense since the problem isn't the same anymore.

I'm making a colouring game. CanvPark_mc is the Movieclip with the details of creating the canvas and where all the brush details are located. The Huge, Medium and SmallSelected1 variables, are variables that are changed when you click the brushes, so you're able to identify them.

I want to switch cursors depending on the brush that was last clicked. This can be done through the first if paramenters. Right now, this is my code to switch, thanks to the help of @NBooo in the previous question

var cursor_mc: MovieClip = new MovieClip();

if (CanvPark_mc.HugeSelected1 == true) {
    cursor_mc = cursor1_mc; //Big Cursor
    removeChild(cursor_mc);
}

if (CanvPark_mc.MediumSelected1 == true) {
    cursor_mc = cursor2_mc; //Medium Cursor
    removeChild(cursor_mc);
}

if (CanvPark_mc.SmallSelected1 == true) {
    cursor_mc = cursor3_mc; //Small Cursor
    removeChild(cursor_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 = stage.mouseX;
        cursor_mc.y = stage.mouseY;
        cursor_mc.mouseChildren = cursor_mc.mouseEnabled = false;
        Mouse.hide();

    }

}

The problem with this code, unfortunately, is that, whenever I click the said buttons, the cursor disappears. It doesn't change itself to any of the MovieClips I previously made.

Note that the removeChild in the if blocks is there in hopes of removing previous instances of cursor_mc on the screen after clicking them in the past.

In tests, it only changes, in the first if blocks, one of the parameters has a = instead of ==.

Example :

if (CanvPark_mc.HugeSelected1 == true) {
    cursor_mc = cursor1_mc; // Big Cursor
    removeChild(cursor_mc);
}

if (CanvPark_mc.MediumSelected1 == true) {
    cursor_mc = cursor2_mc; //Medium Cursor
    removeChild(cursor_mc);
}

if (CanvPark_mc.SmallSelected1 = true) {
    cursor_mc = cursor3_mc; //This is the one he'll run, showing the smallest cursor
    removeChild(cursor_mc);
}

In addition, if they are all reduced down to just =, the code will opt for the last if in the list. I think there's a problem with my code and I can't tell what it is after hours of trying.

Could any of you help me figure this out?

Community
  • 1
  • 1
  • You are setting `cursor_mc` to a the new cursor, but then you immediately remove it from the stage? I think you want to remove the OLD cursor, not the new one. EG: try doing `removeChild` before you change the cursor. – Sunil D. Aug 16 '16 at 16:48
  • @SunilD. Even before the `removeChild(cursor_mc);` was there, it was already doing this problem. And, if I do it before hand, it gives me this error: `Error #2025: The supplied DisplayObject must be a child of the caller.` – TwistedRaven Aug 16 '16 at 16:49
  • Regarding your last code example where you change `==` to `=`, that makes sense because a single `=` is doing an assignment, it is not a boolean expression. So when you use `=` in the condition for an `if` statement it will usually resolve as "truthy". When you change all the if statements to use a single `=`, they are all true and hence you see the results of the last if statement. All of this is a long way of saying that your conditions in your `if` statements are probably not evaluating to "true"... and you might try checking them. – Sunil D. Aug 16 '16 at 16:52
  • @SunilD. Okay. My mistake. But then, how can I make sure that it will read that the variable on `CanvPark_mc` is being set to `true` and, therefore, changing the Cursor without turning it invisible? Does the code need to be reformed? – TwistedRaven Aug 16 '16 at 16:54
  • Is there anywhere in your code where you set `CanvPark_mc.HugeSelected1` (or the other variables) to true so that those conditions in your code above can evaluate to true? – Sunil D. Aug 16 '16 at 19:01
  • @SunilD. Yes, in the EventListeners to change the LineSize of the brush. On Linesize 5, `SmallSelected1 = true`, Linesize 8, `MediumSelected1 = true` and so on. And, in addition, in these EventListeners, I also set the others to false, so it "resets" their value. Sorry for the slow response. – TwistedRaven Aug 17 '16 at 07:35
  • UPDATE : @SunilD. I have put the three if blocks inside the function and they show up now, but whenever I click a different brush, the previous brush is left where I clicked with it. If I do add the removeChild beforehand, gives me the error I already told you about. – TwistedRaven Aug 17 '16 at 08:23

1 Answers1

0

I found the answer to my own question, so I'll be posting the code to help others out.

var cursor_mc: MovieClip = new MovieClip();

    stage.addEventListener(MouseEvent.MOUSE_MOVE, moveCursor);

    function moveCursor(myEvent: MouseEvent) {

    if (CanvPark_mc.HugeSelected1 == true) {

        cursor_mc = cursor1_mc;
        cursor1_mc.visible = true;
        cursor2_mc.visible = false;
        cursor3_mc.visible = false;
    }

    if (CanvPark_mc.MediumSelected1 == true) {

        cursor_mc = cursor2_mc;
        cursor1_mc.visible = false;
        cursor2_mc.visible = true;
        cursor3_mc.visible = false;
    }

    if (CanvPark_mc.SmallSelected1 == true) {

        cursor_mc = cursor3_mc;
        cursor1_mc.visible = false;
        cursor2_mc.visible = false;
        cursor3_mc.visible = true;
    }
    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 = stage.mouseX;
        cursor_mc.y = stage.mouseY;
        cursor_mc.mouseChildren = cursor_mc.mouseEnabled = false;
        Mouse.hide();

    }

}

The if block wasn't inside the function, so it wasn't correlating the code togheter. I put it inside as a test and it worked. Since when you click to change your mouse to a brush, the Movieclip starts following your mouse around. If you change to another brush, the movieclip gets left there. This is fixed by toggling it's visibility in the if's. :)

It's working as intended now.

Hope this helps someone!