1

我在 Adob​​e Animate 中使用 ActionScript 3 来缩放和平移绘图。每当单击控制面板中的中心按钮时,图像不会出现在中心,它会出现在下方..

MovieClip 中的照片如何不以舞台为中心的示例

此外,平移和缩放也不流畅。我正在使用的代码是创建图像的视频剪辑,并且有一个控制面板来控制缩放和平移。请让我知道使其顺利和可行所需的更改。

AS3代码如下:

import flash.events.MouseEvent;
import flash.events.Event;

var moveSpeed: Number = 5;
var sizeScale: Number = 0.04;
var clickMore: Boolean = false;
var clickLess: Boolean = false;
var clickLeft: Boolean = false;
var clickRight: Boolean = false;
var clickUp: Boolean = false;
var clickDown: Boolean = false;
var clickCenter: Boolean = false;

// --- 点击放大更多:

btMore.addEventListener(MouseEvent.MOUSE_DOWN, moreZoom);
function moreZoom(event: MouseEvent): void {
 clickMore = true;
}
btMore.addEventListener(MouseEvent.MOUSE_UP, stopMoreZoom);
function stopMoreZoom(event: MouseEvent): void {
 clickMore = false;
}

// --- 点击缩小:

btLess.addEventListener(MouseEvent.MOUSE_DOWN, lessZoom);
function lessZoom(event: MouseEvent): void {
 clickLess = true;
}
btLess.addEventListener(MouseEvent.MOUSE_UP, stopLessZoom);
function stopLessZoom(event: MouseEvent): void {
 clickLess = false;
}

// --- 单击左移:

btLeft.addEventListener(MouseEvent.MOUSE_DOWN, leftMove);
function leftMove(event: MouseEvent): void {
 clickLeft = true;
}
btLeft.addEventListener(MouseEvent.MOUSE_UP, stopLeftMove);
function stopLeftMove(event: MouseEvent): void {
 clickLeft = false;
}

// --- 单击向右移动:

btRight.addEventListener(MouseEvent.MOUSE_DOWN, rightMove);
function rightMove(event: MouseEvent): void {
 clickRight = true;
}
btRight.addEventListener(MouseEvent.MOUSE_UP, stopRightMove);
function stopRightMove(event: MouseEvent): void {
 clickRight = false;
}

// --- 单击上移:

btUp.addEventListener(MouseEvent.MOUSE_DOWN, upMove);
function upMove(event: MouseEvent): void {
 clickUp = true;
}
btUp.addEventListener(MouseEvent.MOUSE_UP, stopUpMove);
function stopUpMove(event: MouseEvent): void {
 clickUp = false;
}

// --- 点击下移:

btDown.addEventListener(MouseEvent.MOUSE_DOWN, downMove);
function downMove(event: MouseEvent): void {
 clickDown = true;
}
btDown.addEventListener(MouseEvent.MOUSE_UP, stopDownMove);
function stopDownMove(event: MouseEvent): void {
 clickDown = false;
}

// --- 点击移动中心:

btCenter.addEventListener(MouseEvent.MOUSE_DOWN, centerMove);
function centerMove(event: MouseEvent): void {
 clickCenter = true;
}
btCenter.addEventListener(MouseEvent.MOUSE_UP, stopCenterMove);
function stopCenterMove(event: MouseEvent): void {
 clickCenter = false;
}

// --- 单击缩放轮:

stage.addEventListener(MouseEvent.MOUSE_WHEEL, handleMouseWheel);
function handleMouseWheel(event: MouseEvent): void {
 if (myMCimg.scaleX >= 1) {
 myMCimg.scaleX += (event.delta * sizeScale);
 myMCimg.scaleY += (event.delta * sizeScale);
 } else {
 myMCimg.scaleX = 1;
 myMCimg.scaleY = 1;
 }
}

// --- 动作:

addEventListener(Event.ENTER_FRAME, enterFrame);
function enterFrame(event: Event): void {
 if (clickMore == true) {
 myMCimg.scaleX += sizeScale;
 myMCimg.scaleY += sizeScale;
 }
 if ((clickLess == true) && (myMCimg.scaleX >= 1)) {
 myMCimg.scaleX -= sizeScale;
 myMCimg.scaleY -= sizeScale;
 }
 if (clickLeft == true) {
 myMCimg.x -= moveSpeed;
 }
 if (clickRight == true) {
 myMCimg.x += moveSpeed;
 }
 if (clickUp == true) {
 myMCimg.y -= moveSpeed;
 }
 if (clickDown == true) {
 myMCimg.y += moveSpeed;
 }
 if (clickCenter == true) {

// --- 集中:

 myMCimg.scaleX = 1;
 myMCimg.scaleY = 1;
 myMCimg.x = stage.stageWidth / 2;
 myMCimg.y = stage.stageHeight / 2;
 }
}

// --- 单击键方向键盘:

stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKeys);
function pressKeys(event: KeyboardEvent): void {

 if (event.keyCode == Keyboard.LEFT) {
 myMCimg.x -= moveSpeed;
 }
 if (event.keyCode == Keyboard.RIGHT) {
 myMCimg.x += moveSpeed;
 }
 if (event.keyCode == Keyboard.UP) {
 myMCimg.y -= moveSpeed;
 }
 if (event.keyCode == Keyboard.DOWN) {
 myMCimg.y += moveSpeed;
 }
 if (event.keyCode == Keyboard.ENTER) {

// --- 集中:

 myMCimg.scaleX = 1;
 myMCimg.scaleY = 1;
 myMCimg.x = stage.stageWidth / 2;
 myMCimg.y = stage.stageHeight / 2;
 }

// --- 点击拖动:

 myMCimg.addEventListener(MouseEvent.MOUSE_DOWN, dragMove);
 function dragMove(event: MouseEvent): void {
 myMCimg.startDrag();
}
 myMCimg.addEventListener(MouseEvent.MOUSE_UP, stopDragMove);
 function stopDragMove(event: MouseEvent): void {
 stopDrag();
} 
}
4

1 回答 1

0

(1) 集中:

要按宽度(水平)集中,请尝试减去图片的半宽,例如:

myMCimg.x = (stage.stageWidth / 2) - (myMCimg.width / 2);

按高度(垂直)集中的相同逻辑。

(2) 缩放:

至于平滑缩放,我没有测试过你的代码,所以不确定视觉问题“不平滑”到底是什么意思。也许这是你的输出设置?如果您尝试以下 3 个硬件加速选项:无、直接和 GPU,在您的发布设置(按Ctrl+Shift+F12)中,“平滑度”是否会提高?

最后,我只能建议您尝试以下类似的方法作为替代方案(也许有帮助):

要测试代码...

  • 在舞台上创建 2 个不同的颜色框。

  • 将每个框转换为 MovieClip(而不是按钮)。

  • 给每个盒子一个实例名称(例如:zoom_inzoom_out)。

  • 将图像添加/转换为具有实例名称的某些 MovieClip pic

第一个示例将通过鼠标悬停更新缩放...

zoom_in.addEventListener(MouseEvent.MOUSE_OVER, process_zoom_rollover);
zoom_in.addEventListener(MouseEvent.MOUSE_OUT, process_zoom_rollout);

zoom_out.addEventListener(MouseEvent.MOUSE_OVER, process_zoom_rollover);
zoom_out.addEventListener(MouseEvent.MOUSE_OUT, process_zoom_rollout);

//# mouse pointer is placed over either zoom-in or zoom-out icon..
function process_zoom_rollover (event: MouseEvent): void 
{ 
    //trace("event.currentTarget : " + event.currentTarget.name );
    event.currentTarget.addEventListener(Event.ENTER_FRAME, animate_Zoom);
}

//# mouse pointer is now move away from over any zoom icon.
function process_zoom_rollout (event: MouseEvent): void 
{ 
    event.currentTarget.removeEventListener(Event.ENTER_FRAME, animate_Zoom);
}

function animate_Zoom (event:MouseEvent) : void 
{
    //# scale by PERCENTAGE (width or height / 100), then multiply by some SPEED amount
    if (event.currentTarget.name == "zoom_in") //increase width and height
    { pic.width += ((pic.width /100 ) * 1.75); pic.height += ((pic.height /100 ) * 1.75);  }

    if (event.currentTarget.name == "zoom_out") //decrease width and height
    { pic.width -= ((pic.width /100 ) * 1.75); pic.height -= ((pic.height /100 ) * 1.75);  }
}

第二个示例将通过鼠标单击更新缩放...

zoom_in.addEventListener(MouseEvent.CLICK, process_zoom_click);
zoom_out.addEventListener(MouseEvent.CLICK, process_zoom_click);

//# mouse pointer is placed over either zoom-in or zoom-out icon.
function process_zoom_click (event:MouseEvent) : void 
{ 
    //trace("event.currentTarget : " + event.currentTarget.name );
    if (event.currentTarget.name == "zoom_in") //increase width and height
    { pic.width += ((pic.width /100 ) * 1.75); pic.height += ((pic.height /100 ) * 1.75);  }

    if (event.currentTarget.name == "zoom_out") //decrease width and height
    { pic.width -= ((pic.width /100 ) * 1.75); pic.height -= ((pic.height /100 ) * 1.75);  }
}
于 2018-08-11T16:01:24.313 回答