AS3自定义光标点击

AS3自定义光标点击

本文介绍了AS3自定义光标点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘画游戏,一旦你点击画笔,鼠标切换到所述画笔的图形对应,并让你在屏幕上绘画。如果没有选择画笔,鼠标将保持不变。

矩形和画笔位于一个单独的Movieclip上,这样可以将png图层叠加在上面您可以填写和绘制。

在场景1中的动作图层中,这是用于更改鼠标的代码:

  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();






$ b每个画笔都有一个与之相关的布尔变量:小,Medium和HugeSelected1,这样,我可以随时告诉代码,其中一个被选中,而另一个不是。

现在运行这个代码,在开始时,没有任何反应,但如果我点击任何画笔,这会弹出输出。

  TypeError:Error #2007:参数子项必须是非空的。 
at flash.display :: DisplayObjectContainer / setChildIndex()
at visibilityToggle / moveCursor()[visibilityToggle :: frame1:42]

似乎特指着

  setChildIndex(cursor_mc,this.numChildren- 1); 

我真的不知道是什么原因导致了这个错误。

我该如何解决这个问题?

解决方案

关于您在第一篇文章中发布的错误 - 当您使用的某个对象为null(即未初始化或已销毁)时会发生此类错误。错误通常泛化为NPE(空指针异常)。发生此类错误时,应检查是否存在所有对象。第二个错误发生,因为你的cursor_mc没有父剪辑(即它没有添加到阶段)或父对象不是你调用的同一个对象setChildIndex。建议您阅读此文档 a>



要解决第二个问题,您可以检查父剪辑是否确实存在。另外,请记住,如果您已经重新分配了cursor_mc的值,则需要将其重新添加到舞台上,也许您希望从舞台上删除上一个剪辑(假设cursor1_mc,cursor2_mc,cursor3_mc不在舞台上)。



  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);

函数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();
}
}


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?

解决方案

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();
    }
}

这篇关于AS3自定义光标点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:00