Arrays 单击鼠标将x和y跳线保存为变量
我有一段代码,在mouseDown函数上创建圆。我试图保存圆圈的x和y线,但无法找到解决方法。我需要从mouseDown保存多个跳线实例 是否可以在阵列中执行此操作 另外,我不想找任何人发布代码或任何东西。请给我一些建议。或建议将不胜感激:D我使用AS3,我对它相当陌生Arrays 单击鼠标将x和y跳线保存为变量,arrays,actionscript-3,variables,save,Arrays,Actionscript 3,Variables,Save,我有一段代码,在mouseDown函数上创建圆。我试图保存圆圈的x和y线,但无法找到解决方法。我需要从mouseDown保存多个跳线实例 是否可以在阵列中执行此操作 另外,我不想找任何人发布代码或任何东西。请给我一些建议。或建议将不胜感激:D我使用AS3,我对它相当陌生 var posOne:Number; //var posTwo:Number; //var posThree:Number; //var posFour:Number; //var posFive:Number; impo
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
}
实现这一点的最简单方法是使用鼠标单击的
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位)。我认为在代码中查看实际上比解释更容易。这并不是说你不应该阅读鼠标点的使用,但是作为一个概述,下面的代码应该为你提供一个使用鼠标点的良好开端。详情请参阅评论
// 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.
}
}
这取决于到目前为止代码设置的方式,但一个简单的选择是生成一个类似于变量的字符串,并在运行时将值连接到它。然后由您决定是否从traces/output复制“code”或使用方法复制字符串。感谢您的回复:DposOne/Two/etc.是数字变量,所以我假设您只想存储一个位置组件(x或y),而不是同时存储两个位置组件?另外,由于您想要保存5个值,所以您想要5个最新的鼠标位置,这是安全的吗?如果是这样,您应该有一个数组来存储值,并在数组中添加新值时将旧值移位1。目前,我正在尝试添加一个计数器来计算鼠标点击次数,具体取决于点击次数,它将保存mouseX和mouseY的变量。我试图同时保存这两个,实际上,它们应该改为int吗?我将很快尝试数组,是的,我需要的是最近的五次单击。可能会划掉保存值并将它们连接到字符串-实际上会比
int
或Number
值占用更多内存。您可以将int
值推入数组,然后array。将其中两个值移位,将第一个值视为x
,将第二个值视为y
。