在动作脚本3中协调功能鼠标按下

时间:2015-12-28 13:25:32

标签: actionscript-3

我将按照光标制作我的锤子(影片剪辑),每次单击鼠标/鼠标时,锤子都会向下暴露光标并返回原始位置。但我只能在光标下制作锤子(影片剪辑)。我该怎么办?

hammer.startDrag(true);

enter image description here

2 个答案:

答案 0 :(得分:0)

你可以做的就是用锤子使用一个动画片段。在这个动画片段中你有你的时间轴,在时间轴的第一个键上你用“向上”位置敲击锤子,并在同一个键上输入以下代码

stop();

然后在下一个键上你用锤子创建一个新的框架,你还要添加:

stop();

这将停止第一个键上的帧读取器,我们要做的是在舞台上实例化动画片段并使其跟随鼠标。

//Instance the hammer
var hammer:Hammer = new Hammer(); //Hammer is the name of you class on the movieclip in your flash library

stage.addChild(hammer); //instance on the stage

//Add an event listener on the stage wich is trigger when the mouse move
stage.addEventListener(MouseEvent.MOUSE_MOVE, functionName);
    function functionName2(evt:MouseEvent){
       hammer.x = stage.mouseX; //If you want you can add an offset (ex : stage.mouseX - 5; )
       hammer.y = stage.mouseY; 
    }

现在锤子应该移动并跟随鼠标:

//On mouse button down
hammer.addEventListener(MouseEvent.MOUSE_DOWN, functionName2);
function functionName2(evt:MouseEvent){
   evt.target.goToAndStop(2); //2 is the number of the key wich contains the hammer with down position
}

//On mouse button up
hammer.addEventListener(MouseEvent.MOUSE_UP, functionName3);
function functionName3(evt:MouseEvent){
   evt.target.goToAndStop(1); //1 is the number of the key wich contains the hammer with up position
}

这项技术的优势在于,如果你对Flash动画感到友好,你可以在带有“goToAndPlay(x)”的键之间创建一个小动画,这不仅仅是“向上/向下”,你可以添加一些乐趣。 / p> 祝你好运!

答案 1 :(得分:0)

我收到你的文件后,这里是新代码:

Mouse.hide();   //Hide the mouse cursor

//The stage listen the mouse movement
stage.addEventListener(MouseEvent.MOUSE_MOVE, functionA);
function functionA(evt:MouseEvent){
    //On mouse movement, the hammer position will change
    hammer.x = mouseX; //If you want you can add an offset (ex : stage.mouseX - 5; )
    hammer.y = mouseY;
    //That's exactly the same as hammer.startDrag(true); but with something more cool, it's you can add an offset for X/Y axes.
}

//The stage is listening on mouse button down
stage.addEventListener(MouseEvent.MOUSE_DOWN, functionName2);
function functionName2(evt:MouseEvent){
 //On mouse button down, we enter the movieclip "hammer" and on the timeline we go to and play (gotoAndPlay :D) the label "HammerDown"
 //Now if you double click on the hammer on the stage, you will enter the movieclip and you will find a layer call "Labels"
 //I called it like that, the name is not important. In Flash you can add a name to a key frame or a group of keys. Maybe you don't see
 //any advantage for now but trust me, that's much more upgradable like that. So in my first answer on Stackoverflow I wrote to you
 //to type : gotoAndPlay( 1 ), the number was the key's number, but this time I've change it to "HammerDown" wich is the name of my label
 //on the layer labels inside hammer's movieclip.
 //So at the end what happen is : When the stage receive a mouse down event, the script enter "hammer" movieclip and gotoAndPlay the animation
 //start from the first key of "HammerDown" label. That's what the line below means.
 hammer.gotoAndPlay( "HammerDown" ); //2 is the number of the key wich contains the hammer with down position

 //To give a label's name to a key or a group of key, just click on a keyframe, go to Propriety -> Label -> Name and that's it!

}

//On mouse button up
stage.addEventListener(MouseEvent.MOUSE_UP, functionName3);
function functionName3(evt:MouseEvent){
    //On mouse up the script enter hammer's movieclip and gotoAndPlay the animation inside "hammer" movieclip, wich start from "HammerUp".
    //The trick is that the animation will be stop because at the same time, the script read the stop(); and so the animation is stop inside "Hammer".
   hammer.gotoAndPlay("HammerUp"); //1 is the number of the key wich contains the hammer with up position
}

这是我的编辑文件: File