问题
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, wich 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 wich one is selected and wich 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?
回答1:
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();
}
}
来源:https://stackoverflow.com/questions/38970630/as3-custom-cursor-on-click