Actionscript Mouse control on ONE movieclip only?
In Flash/ActionScript2, is it possible to capture the mousemove, buttonup and down event but only within one MovieClip?
At present, i can capture mousemove etc. via a listener, but only for the entire stage... I need to draw a rectangle for selecten, and then press a save button. The problem is i save the coordinates on mousedown and up, but when I press on the save button, it saves the coordinates of when you pressed on the save button...
I've tried mc.onXY, but that either listened not at all or to the entire stage...
Edit: Code as requested mc.onMouseDown should be mcImageToCrop.onMouseDown I did change it back to mc when mcImageToCrop didn't work...
`
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 LI开发者_StackOverflow中文版STENER
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();
`
Here's one way that might work for you: instead of using a listener, you can define onPress and onRelease events for your movieClip, like this:
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());
}
Note the difference between
- onMouseUp = function() {} Invoked when the mouse button is pressed. (anywhere)
- onPress = function() {} Invoked when the user clicks the mouse while the pointer is over a movie clip.
精彩评论