我有一段代码可以在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
}
答案 0 :(得分:1)
解决此问题的最简单方法是使用鼠标单击的Point
和x
坐标创建y
个对象,然后将它们推送到数组。
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位比特,例如)通过使用按位运算。
答案 1 :(得分:0)
我认为在代码中看起来比解释更容易。并不是说你不应该阅读使用鼠标点,但是为了概述,下面的代码应该给你一个很好的开始使用点,给出你上面的内容。有关详细信息,请参阅评论。
// 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.
}
}