2010-03-11 129 views

回答

1

Flash版本

var s:Sprite = new Sprite(); 
s.x = 20; 
s.graphics.beginFill(0xFF0000); 
s.graphics.drawRect(0,0,20,20); 
addChild(s); 

stage.addEventListener(MouseEvent.MOUSE_MOVE,moveSprite); 

function moveSprite(e:MouseEvent):void 
{ 
    s.y = e.localY; 
} 

柔性版

<mx:Canvas width="100" height="100"> 
      <mx:mouseMove> 
        <![CDATA[ 
         s.y = event.localY; 
        ]]> 
       </mx:mouseMove> 
      <mx:Canvas id="s" backgroundColor="#ff0000" width="20" height="20"/> 
     </mx:Canvas> 

每一种可以粘贴在和对你说的话。它会创建一个与鼠标垂直相同但水平固定的20x20红色框。您的鼠标必须位于包含的画布内的Flex版本。

+0

太好了,谢谢! – 2010-03-12 04:52:40

1
addEventListener(MouseEvent.CLICK, clickHandler); 
function clickHandler(e:MouseEvent):void{ 
    mySprite.y += amount; 
} 
+0

对不起,我问到鼠标点击,而我实际上需要鼠标移动。点击不会跟踪鼠标移动。 – 2010-03-11 13:43:48

0

好吧,拖动是更复杂一点。您需要为拖动的边界定义一个矩形。如果你只想沿一个轴拖动,那么你可以使矩形的宽度为0.在这个例子中,我已经将滚动的数量限制在不同的数字上,并且可以在下面进行修改。

import flash.events.MouseEvent; 
import flash.geom.Rectangle; 

mySprite.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 

function mouseDownHandler(event:MouseEvent):void{ 
    stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); 
    var scrollUpAmount:int = 10; 
    var scrollDownAmount:int = 200; 
    var boundsRect:Rectangle = new Rectangle(mySprite.x,mySprite.y-scrollUpAmount,0,mySprite.y+scrollDownAmount); 
    mySprite.startDrag(false, boundsRect); 
} 

function mouseUpHandler(event:MouseEvent):void{ 
    stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); 
    mySprite.stopDrag(); 
} 
+0

我如何限制只有垂直的运动?谢谢 – 2010-03-11 14:21:08

+0

这个例子仅限于垂直拖动,正如我上面所解释的。 如果你只想沿一个轴拖动(例如垂直),那么你可以使边界矩形的宽度为0. – danjp 2010-03-12 09:39:03

相关问题