在鼠标单击时将 x 和 y 线保存为变量
Posted
技术标签:
【中文标题】在鼠标单击时将 x 和 y 线保存为变量【英文标题】:Saving x and y cords as variables on mouse click 【发布时间】:2012-01-07 16:08:08 【问题描述】:我编写了一段代码,可以在 mouseDown 函数上创建圆圈。我正在尝试保存圆圈的 x 和 y 线,但无法找到解决方法。我需要从 mouseDown 中保存多个绳索实例。
是否可以在数组中执行此操作?
P.S 我不是在寻找任何人来发布代码或任何东西。请只是建议。或者建议将不胜感激:D 我使用的是 AS3,而且我对它还很陌生。
var posOne:Number;
//var posTwo:Number;
//var posThree:Number;
//var posFour:Number;
//var posFive:Number;
import flash.events.MouseEvent;
stage.addEventListener(MouseEvent.MOUSE_DOWN,startDoodle);
stage.addEventListener(MouseEvent.MOUSE_UP,stopDoodle);
function startDoodle(e:MouseEvent):void
stage.addEventListener(MouseEvent.MOUSE_DOWN,makeTarget);
function stopDoodle(e:MouseEvent):void
stage.removeEventListener(MouseEvent.MOUSE_UP,makeTarget);
import flash.events.MouseEvent;
function makeTarget(e:MouseEvent):void
var ellipse:Ellipse = new Ellipse (15,15, 0x00ff00);
addChild (ellipse);
ellipse.x = mouseX;
ellipse.y = mouseY;
//posOne == ellipse.x && ellipse.y
【问题讨论】:
这取决于您到目前为止如何设置代码,但一个简单的选择是生成一个类似于您的变量的字符串,并在运行时将值连接到它。然后由您决定是否从跟踪/输出中复制“代码”或使用setClipboard() 方法复制字符串。 我已经添加了到目前为止的代码。感谢您的回复:D posOne/Two/等。是数字变量,所以我假设您只想存储一个位置分量(x 或 y),但不能同时存储两者?另外,假设你想保存 5 个值,你想要 5 个最近的鼠标位置是否安全?如果是这样,当您在数组中添加新值时,您应该有一个数组来存储值并将旧值移动 1。 目前我正在尝试添加一个计数器来计算鼠标点击次数,这取决于点击次数,它将保存 mouseX 和 mouseY 的变量。实际上,我试图同时保存两者,它们应该是 int 吗?我很快就会尝试这个数组,是的,最近的五次点击就是我需要的。 【参考方案1】:解决此问题的最简单方法是使用鼠标单击的x
和y
坐标创建Point
对象并将它们推送到数组。
function onMouseClick(event:MouseEvent):void
var mousePoint:Point = new Point(stage.mouseX, stage.mouseY);
mouseClicks.push(mousePoint);
如果您需要存储这些类型的坐标的负载,并且您担心性能或内存,您可以使用包含某些分隔符的特定格式的字符串保存坐标字符串表示,例如85|56$104|77$...
,这样您会知道,每组x
和y
值都由一个符号分隔,而集合中的x
和y
值由其他分隔符分隔。明智地存储此数据存储器的最佳方法是将输入限制为 16 位整数,然后将两个值存储为 32 位整数(例如,x
值存储在前 16 位,y
值存储到后 16 位) 使用按位运算。
【讨论】:
可能会删除保存值并将它们连接到字符串 - 实际上会比int
或 Number
值占用更多的内存。您可以将 int
值推送到数组中,并将 array.shift
其中两个推入数组,并将第一个视为x
,将第二个视为y
。【参考方案2】:
我认为实际上在代码中看到比解释更容易。并不是说您不应该阅读有关使用鼠标点的内容,但是对于概述,鉴于您上面的内容,下面的代码应该可以为您提供一个很好的使用点的开始。详情请看cmets。
// It helps to keep all the imports at hte top of your file, for easy code reading.
import flash.events.MouseEvent;
import flash.geom.Point;
// It helps to keep all class-level variables at the top of the file.
// Make an array rather than many individual variables. That way you can use a for-loop to access the items.
// (The brackets "[]" are equivalent to new Array().)
var localPosArray:Array = [];
var globalPosArray:Array = [];
var ellipseArr:Array = [];
// can use these as temporary variables:
var localPoint:Point;
var globalPoint:Point;
// stores currently active ellipse index:
var curEllipseIdx:int;
// stores currently active point index:
var curPtIdx:int;
// store click count:
// (You could use an array instead, but probably not necessary.)
var countTotal:int = 0;
// Probably not needed:
// var posOne:Number;
//var posTwo:Number;
//var posThree:Number;
//var posFour:Number;
//var posFive:Number;
stage.addEventListener(MouseEvent.MOUSE_DOWN, startDoodle);
stage.addEventListener(MouseEvent.MOUSE_MOVE, sizeDoodle);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDoodle);
function startDoodle(ev:MouseEvent):void
// count clicks (mouse downs):
countTotal++;
// to find the coordinates on the local object -- this is usually different from the global coordinates.
curPtIdx = localPosArray.length;
localPosArray[curPtIdx] = new Point(ev.localX, ev.localY);
// alternately, to overwrite a current point:
// curPtIdx = 0;
// localPosArray[curPtIdx] = new Point(ev.localX, ev.localY); // the old value will be garbage collected, if there are no other references.
// to convert from local to global coordinates:
// curPtIdx = 0;
// globalPoint = localToGlobal(localPosArray[curPtIdx]);
// alternately, to find the coordinates on the global object (the stage):
curPtIdx = globalPosArray.length;
globalPosArray[globalPosArray.length] = new Point(ev.stageX, ev.stageY);
// alternately, to overwrite a current point:
// curPtIdx = 0;
// globalPosArray[curPtIdx] = new Point(ev.stageX, ev.stageY); // the old value will be garbage collected, if there are no other references.
// to convert from local to global coordinates:
// curPtIdx = 0;
// localPoint = globalToLocal(globalPosArray[curPtIdx]);
//You do not need to stop listening for mouse *down*, since that can never happen when the mouse is down.
// stage.addEventListener(MouseEvent.MOUSE_DOWN, makeTarget);
function stopDoodle(e:MouseEvent):void
//You do not need to stop listening for mouse *up*, since that can never happen when the mouse is up.
//stage.removeEventListener(MouseEvent.MOUSE_UP, makeTarget);
function makeTarget(e:MouseEvent):void
curPtIdx = 0;
curEllipseIdx = ellipseArr.length;
ellipseArr[curEllipseIdx] = new Ellipse(localPosArray[curPtIdx].x, localPosArray[curPtIdx].x, 0x00ff00);
addChild(ellipseArr[curEllipseIdx]);
// These lines are probably not necessary:
//ellipse.x = mouseX;
//ellipse.y = mouseY;
// What is this for?
//posOne == ellipse.x && ellipse.y
function sizeDoodle(ev:MouseEvent):void
if (ellipseArr && ellipseArr[curEllipseIdx])
// size the ellipse by the distance from the initial point:
// however you might do that.
【讨论】:
以上是关于在鼠标单击时将 x 和 y 线保存为变量的主要内容,如果未能解决你的问题,请参考以下文章
用PostMessage函数向窗体发送鼠标单击消息,单击的X坐标=100 ,y坐标=200.该窗体的句柄为hand。代码怎么写