Actionscript鼠标控件仅限于一个动画片段?

时间:2010-01-28 12:37:57

标签: flash actionscript actionscript-2

在Flash / ActionScript2中,是否可以捕获mousemove,buttonup和down事件,但只能在一个MovieClip中捕获?

目前,我可以通过听众捕捉鼠标等,但仅适用于整个舞台...... 我需要为选择画一个矩形,然后按一个保存按钮。问题是我保存mousedown和up上的坐标,但是当我按下保存按钮时,它会保存按下保存按钮时的坐标...

我已经尝试了mc.onXY,但要么根本不听,要么听整个舞台......

编辑:按要求编码 mc.onMouseDown 应该 mcImageToCrop.onMouseDown 当mcImageToCrop不起作用时,我确实将它改回mc ......

`

    import flash.geom.Rectangle;


// Create a container by calling the setUpContainer functie wich will return a movieclip with a needed propeties set.
var container:MovieClip = setUpContainer();

// Create rectangle named window to create our gradient in it.
var window:Rectangle = new Rectangle(400, 230, 265, 240);

// Set our created window rectangle as a scrollRect property to the container movieclip.
// Rectangle constructor params are x, y, width, height
//container.scrollRect = window;




//this.opaqueBackground = 0xFF0000;//0xCCCCCC;


var mcImageToCrop:MovieClip ;
var mc:MovieClip;
var mcControls:MovieClip ;
//all drawing,positioning and coloring will be set in this function:
function setUpContainer():MovieClip 
{
    mc = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
    mc._x = 0;
    mc._y = 0;
    mc.opaqueBackground = 0xF0F0F0;//0xCCCCCC;



mcImageToCrop = mc.createEmptyMovieClip("mcImageToCrop", mc.getNextHighestDepth());
var mcLoader:MovieClipLoader = new MovieClipLoader();
mcLoader.addListener(this);
mcLoader.loadClip("Test.swf", mcImageToCrop); //load the tower movie clip


mcImageToCrop.onKeyDown=function()
{
    trace("KeyDown");
}

///////////////////////// Button //////////////////////////////////
mcControls=this.createEmptyMovieClip("mcControls", 500);//this.getNextHighestDepth());  // 2=  restrect depth
mcControls._x = 0;
mcControls._y = 480;
//mcControls.opaqueBackground = 0x00FF00;//0xCCCCCC;

mcControls.onPress=function()
{
    trace("Save coord");
}

mcControls.lineStyle(1,0x000000,100);

var startX=0;
var startY=00;
var rwidth=100;
var rheight=30;

mcControls._alpha = 80;
mcControls.beginFill(0x000000);
mcControls.moveTo(startX,startY); // oben links
mcControls.lineTo(startX+rwidth,startY);
mcControls.lineTo(startX+rwidth,startY+rheight);// unten rechts
mcControls.lineTo(startX,startY+rheight);
mcControls.lineTo(startX,startY); // I think this isn't needed anyway
mcControls.endFill();
//public createTextField(instanceName:String, depth:Number, x:Number, y:Number, width:Number, height:Number) : TextField
//  this.createTextField("button_text", 5, x, y, 100, 50);
this.createTextField("button_text", 501, 25, 485, 100, 5);
var bntTxtFmt:TextFormat = new TextFormat();
bntTxtFmt.color=(0xFFFFFF);
bntTxtFmt.bold=true;

button_text.html = true;
button_text.wordWrap = true;
button_text.border = false;
button_text.autoSize = true;
button_text.selectable = false;
button_text.textColor = (0xFFFFFF);
//button_text.backgroundColor=0xD1D6FC;
//button_text.setTextFormat(bntTxtFmt);
button_text.htmlText = "Speichern";
///////////////////////// End Button //////////////////////////////////



this.onLoadInit = function (target_mc:MovieClip)
{
    trace("Onloadinit:target_mc width: " + target_mc._width + " target_mc height: " + target_mc._height);
    trace("Onloadinit Stage width: " + Stage.width + " Stage height: " + Stage.height);
    trace("Onloadinit mcImageToCrop width: " + mcImageToCrop._width + " mcImageToCrop height: " + mcImageToCrop._height);
    /*
    width: 1024 
    height: 768
    Stage: Height X :640 Height Y: 480
    loadinit Stage height: 480 - This height: 768
    */
    var percentW = Stage.width*100 / mcImageToCrop._width;
    var percentH = Stage.height*100 / mcImageToCrop._height;

    mcImageToCrop._xscale = percentW;
    mcImageToCrop._yscale = percentH;



    window.x = 249; // 400
    window.y = 143; // 230
    window.width=163; // 265
    window.height=149; // 240

    var UnscaledX=249*100/percentW;
    var UnscaledY=143*100/percentH;
    var UnscaledW=163*100/percentW;
    var UnscaledH=149*100/percentH;

    container.scrollRect = window;

    // Zurückskalieren des Ausschnitts auf Originalgrösse
    container._xscale = 10000/percentW; // =100* 100* 1/(percentW/100)
    container._yscale = 10000/percentH;

    trace("Onloadinit Scale X :" + mcImageToCrop._xscale + " Scale Y: " +   mcImageToCrop._yscale );
}



//mcLoader.loadClip("Test3.swf", mcControls);
return mc;

enter code here}

///////////////////////////////////////////////////////
// Text field for mouse position
this.createTextField("mouse_info", 999, 5, 5, 250, 80);
mouse_info.html = true;
mouse_info.wordWrap = true;
mouse_info.border = false;
mouse_info.autoSize = true;
mouse_info.selectable = false;
///////////////////////////////////////////////////////




var mouseListener:Object = new Object();


var rectStartX;
var rectStartY;

var rectStopX;
var rectStopY;

var bMouseIsDown:Boolean=false;

mc.onMouseDown = function() 
{
    bMouseIsDown=true;
    rectStartX=_xmouse;
    rectStartY=_ymouse;
    trace("Clicked at x: " + _xmouse + " y: " +_ymouse);
}




mc.onMouseUp = function()
{
    bMouseIsDown=false;
    trace("Released at x: " + _xmouse + " y: " +_ymouse);
    rectStopX=_xmouse;
    rectStopY=_ymouse;
}




mc.onMouseMove = function() 
{
    if(!bMouseIsDown)
    {
        //Line.removeMovieClip();
        return;
    }


    ////////////////////////////////////////////

    rectangles2.removeMovieClip();

    //var rectangles2:MovieClip = createEmptyMovieClip("rectangles2", 1);
    var rectangles2:MovieClip = createEmptyMovieClip("rectangles2", mc.getNextHighestDepth());
    rectangles2.lineStyle(1,0x000000,100);

    var startX=rectStartX;
    var startY=rectStartY;
    var rwidth=_xmouse-rectStartX;
    var rheight=_ymouse-rectStartY;

    rectangles2._alpha = 80;
    rectangles2.beginFill(0xF5F5F5);

    rectangles2.moveTo(startX,startY); // oben links
    rectangles2.lineTo(startX+rwidth,startY);
    rectangles2.lineTo(startX+rwidth,startY+rheight);// unten rechts
    rectangles2.lineTo(startX,startY+rheight);
    rectangles2.lineTo(startX,startY); // I think this isn't needed anyway
    rectangles2.endFill();




    ///////////////////////////////////////////////

var table_str:String = "";


//table_str += "Current content position:  \t"+"<b>x</b>:"+content._x+"\t"+"<b>y</b>:"+content._y+newline;
table_str += "Current mouse position:  \t"+"<b>x</b>:"+_xmouse+"\t"+"<b>y</b>:"+_ymouse+newline;
table_str += "";
mouse_info.htmlText = table_str;
};


// Mouseaction listener
//Mouse.addListener(mouseListener);




Stage.align = "TL";
Stage.scaleMode = "noScale";
//Stage.scaleMode = "exactFit";


// STAGE RESIZE LISTENER
var stageListener:Object = {
    onResize: resizeHandler
};

Stage.addListener( stageListener );


// STAGE RESIZE HANDLER
function resizeHandler():Void
{
    var w:Number = Stage.width;
    var h:Number = Stage.height;

    // mc, mcImageToCrop
    //_root.mc._height = 400;
    //_root.mc._width = 400;

    //trace("Resized to w: "+ w +" and h: " + h);
}


resizeHandler();
`

1 个答案:

答案 0 :(得分:1)

以下是一种可能对您有用的方法:您可以为movieClip定义 onPress onRelease 事件,而不是使用侦听器,如下所示:

import flash.geom.Point;// you'll need this if you want to store your coordinates as Point objects

var startPoint:Point = new Point();
var endPoint:Point = new Point(); 

myClip.onPress= function() {
    startPoint.x=this._xmouse; //gives x-coord relative to myClip...
    startPoint.y=this._ymouse; // ...for coords relative to stage, omit "this."
    trace("start: "+startPoint.toString());
}

myClip.onRelease= function() {
    endPoint.x=this._xmouse;
    endPoint.y=this._ymouse;
    trace("end: "+endPoint.toString());
}

myButton.onRelease= function() {
    trace("saved coordinates: "+startPoint.toString()+endPoint.toString());
}

注意

之间的区别
  • onMouseUp = function(){}按下鼠标按钮时调用。 (的任何地方
  • onPress = function(){}当用户在指针悬停在影片剪辑上时单击鼠标时调用。