2011-03-14 37 views
0

這裏是編碼,我發現創建一個盒子,放大鼠標點。 我需要的是縮放然後跟着鼠標。 拼命需要幫助PLZ。謝謝as3我有編碼縮放,但我想要縮放跟隨鼠標

import fl.transitions.Tween; 
import fl.transitions.TweenEvent; 
import fl.transitions.easing.*; 
import fl.motion.MatrixTransformer; 

const TWEEN_IN:String = "tweenIn"; 
const TWEEN_OUT:String = "tweenOut"; 
var tweenDirection:String; 

var internalPoint:Point; 
var externalPoint:Point; 
var tw:Tween; 

var square:Sprite = new Sprite(); 
square.graphics.beginFill(0xFF0000); 
square.graphics.drawRect(0, 0, 100, 100); 

square.x = stage.stageWidth/2 - square.width/2; 
square.y = stage.stageHeight/2 - square.height/2; 

addChild(square); 

square.addEventListener(MouseEvent.CLICK, zoomIn); 

function zoomIn($e:MouseEvent):void 
{ 
    square.removeEventListener(MouseEvent.CLICK, zoomIn); 

    internalPoint = new Point(square.mouseX, square.mouseY); 
    externalPoint = new Point(stage.mouseX, stage.mouseY); 

    tweenDirection = TWEEN_IN; 

    tw = new Tween(null, "", Elastic.easeOut, square.scaleX, 4, 1, true); 
    tw.addEventListener(TweenEvent.MOTION_CHANGE, _syncScale); 
    tw.addEventListener(TweenEvent.MOTION_FINISH, _cleanTween); 
} 

function _syncScale($e:TweenEvent):void 
{ 
    square.scaleX = square.scaleY = tw.position; 

    var matrix:Matrix = square.transform.matrix; 

    MatrixTransformer.matchInternalPointWithExternal(matrix, internalPoint, externalPoint); 

    square.transform.matrix = matrix; 
} 

function _cleanTween($e:TweenEvent):void 
{ 
    tw.removeEventListener(TweenEvent.MOTION_CHANGE, _syncScale); 
    tw.removeEventListener(TweenEvent.MOTION_FINISH, _cleanTween); 

    tw = null; 

    if(tweenDirection == TWEEN_IN) 
     stage.addEventListener(MouseEvent.CLICK, zoomOut); 
    else if(tweenDirection == TWEEN_OUT) 
     square.addEventListener(MouseEvent.CLICK, zoomIn); 
} 

function zoomOut($e:MouseEvent):void 
{ 
    stage.removeEventListener(MouseEvent.CLICK, zoomOut); 

    externalPoint = square.localToGlobal(internalPoint); 
    internalPoint = square.globalToLocal(externalPoint); 


    tweenDirection = TWEEN_OUT; 

    tw = new Tween(null, "", Strong.easeOut, square.scaleX, 1, 1, true); 
    tw.addEventListener(TweenEvent.MOTION_CHANGE, _syncScale); 
    tw.addEventListener(TweenEvent.MOTION_FINISH, _cleanTween); 
} 
+0

當補間縮放完成時,您需要爲ENTER_FRAME添加en事件偵聽器,並在縮放補間開始時將其刪除。在ENTER_FRAME處理程序中,您將根據需要更新位置。 – 2011-03-14 16:57:06

+0

嗨喬治感謝您的答覆,我複製了這個oringinal代碼,所以沒有充分理解它,究竟我在Enter_frame函數中包含什麼信息? – Naomi 2011-03-14 18:09:58

+0

這將是最好的理解代碼,否則你會卡住每一個小的變化。通過理解這一點,你將會減少頭痛並獲得更多的收益。 – 2011-03-14 21:30:32

回答

0

這樣的事情?

import fl.transitions.Tween; 
import fl.transitions.TweenEvent; 
import fl.transitions.easing.*; 
import fl.motion.MatrixTransformer; 

const TWEEN_IN:String = "tweenIn"; 
const TWEEN_OUT:String = "tweenOut"; 
var tweenDirection:String;//variable to keep track of the tween type(zoom in/zoom out) 

var internalPoint:Point; 
var externalPoint:Point;//two points used for localToGlobal and globalToLocal coordinates conversion 
var tw:Tween; 

var square:Sprite = new Sprite();//create the square sprite, and draw a red rectangle 
square.graphics.beginFill(0xFF0000); 
square.graphics.drawRect(0, 0, 100, 100); 
square.graphics.endFill(); 

square.x = stage.stageWidth/2 - square.width/2;//centre the square 
square.y = stage.stageHeight/2 - square.height/2; 

addChild(square);//add it to the display list 

square.addEventListener(MouseEvent.CLICK, zoomIn); 

function zoomIn($e:MouseEvent):void 
{ 
    square.removeEventListener(MouseEvent.CLICK, zoomIn); 
    square.removeEventListener(Event.ENTER_FRAME, pan); 
    //update the internal/local(mouse position relative to the square) and external/global(mouse position relative to the stage) 
    internalPoint = new Point(square.mouseX, square.mouseY); 
    externalPoint = new Point(stage.mouseX, stage.mouseY); 

    tweenDirection = TWEEN_IN;//keep track of the tween type 
    //create a 'dummy' tween and use the 'change' handler to update the square 
    tw = new Tween(null, "", Elastic.easeOut, square.scaleX, 4, 1, true); 
    tw.addEventListener(TweenEvent.MOTION_CHANGE, _syncScale); 
    tw.addEventListener(TweenEvent.MOTION_FINISH, _cleanTween); 
} 

function _syncScale($e:TweenEvent):void 
{ 
    //update the scale 
    square.scaleX = square.scaleY = tw.position; 
    //get a reference to the square's transformation matrix 
    var matrix:Matrix = square.transform.matrix; 
    //use the MatrixTransformer utility to 'automagically' adjust translation with the scale 
    MatrixTransformer.matchInternalPointWithExternal(matrix, internalPoint, externalPoint); 
    //apply the updated transform matrix to the square 
    square.transform.matrix = matrix; 
} 

function _cleanTween($e:TweenEvent):void 
{ 
    //tween is complete, cleanul 
    tw.removeEventListener(TweenEvent.MOTION_CHANGE, _syncScale); 
    tw.removeEventListener(TweenEvent.MOTION_FINISH, _cleanTween); 

    tw = null; 

    if(tweenDirection == TWEEN_IN){ 
     stage.addEventListener(MouseEvent.CLICK, zoomOut); 
     square.addEventListener(Event.ENTER_FRAME, pan); 
    }else if(tweenDirection == TWEEN_OUT){ 
     square.addEventListener(MouseEvent.CLICK, zoomIn); 
    } 
} 

function zoomOut($e:MouseEvent):void 
{ 
    stage.removeEventListener(MouseEvent.CLICK, zoomOut); 
    square.removeEventListener(Event.ENTER_FRAME, pan); 

    externalPoint = square.localToGlobal(internalPoint); 
    internalPoint = square.globalToLocal(externalPoint); 

    tweenDirection = TWEEN_OUT; 

    tw = new Tween(null, "", Strong.easeOut, square.scaleX, 1, 1, true); 
    tw.addEventListener(TweenEvent.MOTION_CHANGE, _syncScale); 
    tw.addEventListener(TweenEvent.MOTION_FINISH, _cleanTween); 
} 
function pan($e:Event):void { 
    //move about the centre of the stage, should keep the scale in mind for 'bounds' 
    square.x = stage.stageWidth/2-mouseX; 
    square.y = stage.stageHeight/2-mouseY; 
} 
+0

我明白代碼我只是不明白什麼部分設置放大的位置。 – Naomi 2011-03-14 21:35:20

+0

我加了這個functionstage.addEventListener(MouseEvent.MOUSE_MOVE,moving); 函數移動(e:MouseEvent):void { internalPoint.y = mouseY; internalPoint.x = mouseX; externalPoint.x = mouseX; externalPoint.y = mouseY; } – Naomi 2011-03-14 21:36:02

+0

對不起,鍵盤瘋了。我在zoomIn函數的最後添加了這個函數stage.addEventListener(MouseEvent.MOUSE_MOVE,moving); 函數移動(e:MouseEvent):void { internalPoint.y = mouseY; internalPoint.x = mouseX; externalPoint.x = mouseX; externalPoint.y = mouseY; } 並且這使得放大的拍攝按照鼠標,但只有幾秒鐘,然後停止。只需要一個項目,因爲在tomorow – Naomi 2011-03-14 21:37:42