如果您打开信息面板(窗口 > 信息),您会注意到,当您将鼠标光标向左移动时,x 值会减小,而当您将光标向右移动时,x 值会增加。
您可以在这里应用相同的概念。您将需要一个变量来跟踪您的旧 x 鼠标位置和一个变量来跟踪您的新 x 鼠标位置。
如果您的新鼠标位置大于旧鼠标位置,您可以假设鼠标向右移动并且您将向前移动一帧。如果您的新鼠标位置小于旧鼠标位置,您可以假设您的鼠标向左移动并且您将向后移动一帧。您还必须考虑在 MovieClip 的最后一帧上“前进”并在第一帧上“后退”。
这是您可以解决此问题的一种方法:
//Create a reference to store the previous x mouse position
var old_mouseX = 0;
//Add an event listener
this.Silver.addEventListener("pressmove", mouseMovementHandler);
//Mouse movement handler
function mouseMovementHandler(event){
//Get a reference to the target
var currentTarget = event.currentTarget;
//Get a reference to the current x mouse position
var current_mouseX = stage.mouseX;
//Check for mouse movement to the left
if(current_mouseX < old_mouseX){
//Check if we're within the total frames of the MovieClip
if(currentTarget.currentFrame - 1 >= 0 ){
currentTarget.gotoAndStop(currentTarget.currentFrame - 1);
}
//If not, restart on the last frame of the MovieClip
else{
currentTarget.gotoAndStop(currentTarget.totalFrames - 1);
}
}
//Check for mouse movement to the right
else if(current_mouseX > old_mouseX){
//Check if we're within the total frames of the MovieClip
if(currentTarget.currentFrame + 1 <= currentTarget.totalFrames - 1){
currentTarget.gotoAndStop(currentTarget.currentFrame + 1);
}
//If not, restart at frame 0
else{
currentTarget.gotoAndStop(0);
}
}
//Update the old mouse position
old_mouseX = current_mouseX;
}