AS3:我如何定义边界一拖就能对象? [英] AS3: How can I define boundaries for a drag-able object?
问题描述
我在做闪光灯的图像探宝,但我不能换我的头,围绕如何限制滑块到 area_mc
。主要表现在的mouseMoveHandler
。是否有人可以给我一些指点?
进口com.greensock *。
进口com.greensock.easing *。
功能的init():无效{
area_mc.sliderbar_mc.buttonMode = TRUE;
area_mc.sliderbar_mc.addEventListener(的MouseEvent.MOUSE_DOWN,moveSliderbar);
stage.addEventListener(侦听MouseEvent.MOUSE_UP,stopSliderbar);
area_mc.mask_mc.alpha = 0;
area_mc.after_mc.mask = area_mc.mask_mc;
TweenLite.to(area_mc.sliderbar_mc,3,{X:stage.stageWidth / 2,易于:Elastic.easeOut});
TweenLite.to(area_mc.mask_mc,3,{X:stage.stageWidth / 2,易于:Elastic.easeOut});
}
功能moveSliderbar(事件:MouseEvent)方法:无效{
stage.addEventListener(的MouseEvent.MOUSE_MOVE,的mouseMoveHandler);
}
功能stopSliderbar(事件:MouseEvent)方法:无效{
stage.removeEventListener(的MouseEvent.MOUSE_MOVE,的mouseMoveHandler);
}
功能的mouseMoveHandler(事件:MouseEvent)方法:无效{
area_mc.sliderbar_mc.x = area_mc.mouseX;
VAR左:数= area_mc.x - (area_mc.width / 2);
无功权:数= area_mc.x +(area_mc.width / 2);
如果(area_mc.sliderbar_mc.x>右){
area_mc.sliderbar_mc.x =权利;
}
否则,如果(area_mc.sliderbar_mc.x<左){
area_mc.sliderbar_mc.x =左;
}
area_mc.mask_mc.x = area_mc.sliderbar_mc.x;
}
在里面();
根据我的除preting你的问题是:我怎么能放弃一拖能项目的边界呢?
。 的startDrag()
有一个接受参数矩形
( flash.geom。矩形
),这将作为在那里你可以拖动东西的边界。
如:
VAR RECT:长方形=新的Rectangle(0,0,100,100);
的startDrag(假的,RECT);
下面是如何,你拖我会做一个对象的限制:
包
{
进口的flash.display.MovieClip;
进口flash.geom.Rectangle;
进口flash.events.MouseEvent;
公共类DragableItem扩展影片剪辑
{
//瓦尔
私人VAR _boundaries:矩形;
/ **
*构造函数
* /
公共职能DragableItem()
{
//定义边界
//左,上,右,下
_boundaries =新的矩形(30,30,200,200);
的addEventListener(的MouseEvent.MOUSE_DOWN,_mouseDown);
}
/ **
* MOUSE_DOWN
* /
私有函数_mouseDown(E:MouseEvent)方法:无效
{
的startDrag(假,_boundaries);
的addEventListener(侦听MouseEvent.MOUSE_UP,_stopDrag);
的addEventListener(的MouseEvent.MOUSE_OUT,_stopDrag);
}
/ **
* MOUSE_UP
* /
私有函数_stopDrag(E:MouseEvent)方法:无效
{
调用stopDrag();
removeEventListener(侦听MouseEvent.MOUSE_UP,_stopDrag);
removeEventListener(的MouseEvent.MOUSE_OUT,_stopDrag);
}
}
}
快速测试:
VAR DRG:DragableItem =新DragableItem();
drg.graphics.beginFill(0);
drg.graphics.drawRect(0,0,60,60);
drg.graphics.endFill();
的addChild(DRG);
I'm making an image revealer in flash, but I can't wrap my head around how to constrain the slider bar to the area_mc
. Mainly in the mouseMoveHandler
. Can someone please give me some pointers?
import com.greensock.*;
import com.greensock.easing.*;
function init():void {
area_mc.sliderbar_mc.buttonMode = true;
area_mc.sliderbar_mc.addEventListener(MouseEvent.MOUSE_DOWN,moveSliderbar);
stage.addEventListener(MouseEvent.MOUSE_UP,stopSliderbar);
area_mc.mask_mc.alpha = 0;
area_mc.after_mc.mask = area_mc.mask_mc;
TweenLite.to(area_mc.sliderbar_mc,3,{x:stage.stageWidth/2,ease:Elastic.easeOut});
TweenLite.to(area_mc.mask_mc,3,{x:stage.stageWidth/2,ease:Elastic.easeOut});
}
function moveSliderbar(event:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
}
function stopSliderbar(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
}
function mouseMoveHandler(event:MouseEvent):void {
area_mc.sliderbar_mc.x = area_mc.mouseX;
var left:Number = area_mc.x - (area_mc.width / 2);
var right:Number = area_mc.x + (area_mc.width / 2);
if (area_mc.sliderbar_mc.x > right) {
area_mc.sliderbar_mc.x = right;
}
else if(area_mc.sliderbar_mc.x < left){
area_mc.sliderbar_mc.x = left;
}
area_mc.mask_mc.x = area_mc.sliderbar_mc.x;
}
init();
Based on me interpreting your question as: "how can I give a drag-able item boundaries?".
startDrag()
has a parameter that accepts a Rectangle
(flash.geom.Rectangle
) which will act as a boundary for where you can drag something.
eg.
var rect:Rectangle = new Rectangle(0,0,100,100);
startDrag(false, rect);
Here's how I would do an object that you drag and limit:
package
{
import flash.display.MovieClip;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
public class DragableItem extends MovieClip
{
// vars
private var _boundaries:Rectangle;
/**
* Constructor
*/
public function DragableItem()
{
// define boundaries
// left, top, right, bottom
_boundaries = new Rectangle(30,30,200,200);
addEventListener(MouseEvent.MOUSE_DOWN, _mouseDown);
}
/**
* MOUSE_DOWN
*/
private function _mouseDown(e:MouseEvent):void
{
startDrag(false, _boundaries);
addEventListener(MouseEvent.MOUSE_UP, _stopDrag);
addEventListener(MouseEvent.MOUSE_OUT, _stopDrag);
}
/**
* MOUSE_UP
*/
private function _stopDrag(e:MouseEvent):void
{
stopDrag();
removeEventListener(MouseEvent.MOUSE_UP, _stopDrag);
removeEventListener(MouseEvent.MOUSE_OUT, _stopDrag);
}
}
}
Quick test:
var drg:DragableItem = new DragableItem();
drg.graphics.beginFill(0);
drg.graphics.drawRect(0,0,60,60);
drg.graphics.endFill();
addChild(drg);
这篇关于AS3:我如何定义边界一拖就能对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!