Actionscript 3 AS3在“中单击”;分区“;一幅画
我已经搜索过了,根本找不到我需要的东西(如果它存在的话)Actionscript 3 AS3在“中单击”;分区“;一幅画,actionscript-3,flash,mxml,Actionscript 3,Flash,Mxml,我已经搜索过了,根本找不到我需要的东西(如果它存在的话) 窗户上会有一张大图 图片将被划分为多个区域(如地图上分隔各州的边界线) 当某人在区域内单击时,我将引发相应的事件 我使用AS3和MXML创建了一个数据库程序。除了最后一步,一切都很顺利。我无法理解用户在点击或触摸时是如何在图片的特定区域内的 我已经阅读并试图提出一种方法,肯定有(希望如此)一种比我提出的那些胡说八道更简单的方法 谢谢 VL您是用flash professional CS6绘制的吗?如果是这样的话,为什么你不能把图片作为一个
VL您是用flash professional CS6绘制的吗?如果是这样的话,为什么你不能把图片作为一个符号,然后自己分割线条,把这些分割的区域变成图片符号的子符号呢。您可以将各个州的符号保留在正确的位置,以便它们与整体情况保持一致 第一个想法是通过代码生成此图片符号的实例,然后循环遍历该图片的所有子项,并向每个子项添加一个单击事件
var picture:Picture=new Picture();
for(var i:int=0; i<picture.numChildren-1; i++){
picture.getChildAt(i).addEventListener(MouseEvent.CLICK, mouseEventHandler);
}
var图片:图片=新图片();
对于(var i:int=0;i最简单的解决方案是为MouseEvent添加一个事件侦听器。单击图片并在处理程序中检查图片的属性mouseX和mouseY。在XML或类似文件中定义每个区域的边界,并对照当前mouseX/Y检查哪个区域被单击。我的图片是一个窗口的屏幕截图我设计的。想象一张瓷砖的图片,3x3。我有九个区域。我想点击任何区域并引发一个不同的事件。我将突出显示我的“区域”在Photoshop中,用户可以很容易地看到它们。但是,如果用户在左上角区域单击,我怎么知道他在那里单击了呢?我忘了提到。我使用FlashBuilder是因为我正在创建一个AIR程序。@Xiler至少,请诚实地告诉我,因为这是我在编辑前几个小时建议的方法。我没有在我提交我的编辑之前,我会一直关注你的答案,与你不同的是,我会花时间清楚地回答并提供示例代码。也许这就是我花了两个多小时的原因。西勒,我感谢你的代码,我觉得这很有帮助。当然,我感谢你的两个回复。坦率地说,我担心这可能是答案。那是因为有90多个区域另一个问题:确定分区的x,y的最佳方法是在我点击分区的四个角时将显示的x,y加载到我的pix中吗?恐怕我不理解这个问题。请你再详细说明一下好吗?谢谢!好的,现在我明白了。你可以自动通过加载图片、在图片中单击四次、为每次单击检索mouseX和mouseY,并使用区域名称存储四个点,可以简化创建区域的过程。简单如饼图;)
//This is all for a constat region list (like a window, or floor tiles, not things irregular)
import flash.display.Sprite;
var regionsX:int = 3; //Your number of windows across the row
var regionsY:int = 3; // across the column
var regions:Array = new Array(); // an array to hold the values that you will get from where the user clicks
// All of this used a 2D array method
for(var x:int = 0; x < regionX; x++) {
regions[regionsX] = new Array();
for(var y:int = 0; y < regionY; y++) {
regions[regionsX][regionsY] = "region(".concat(x).concat(",").concat(y);
// Here you make this equal to anything you want to get a value of,
//once the correct region is found (I just have a string version here for an example)
}
}
... // other stuff..
var picture:Picture = new Picture(); // your window picture
var regionWidth:Number = picture.width / regionsX; // Gets each region's width
var regionHeight:Number = picture.height / regionsY; // Get each regoin's height
...
picture.addEventListener(MouseEvent.CLICK, mouseEventListener); // add a click listener to the picture
function mouseEventListener(event:MouseEvent):void{
var mouseX:Number = picture.globalToLocal(event.stageX); // gets where the user clicked, and then converts it
//to the picture's cordinate space. ( 50,100 acording to the stage, could be (25,200) to the picture)
var mouseY:Number = picture.globalToLocal(event.stageY); // same for the Y
var regionIntX:Number = Math.floor(mouseX / regionWidth); // Dives the point by each region's width, and then
// converts it to a while integer. (For instance, if a region's width is 100 and you click at 288, then if you do the
// math, you clicked in the 3rd region, but it returns 2... why? (becaue the array counter starts at 0, so 0 is the 1st
// region, 1 is the second and so on...
var regionIntY:Number = Math.floor(mouseY / regionHeight); // Same for Y
var yourValue:String = regions[regionIntX][regionIntY]; // This returns that you initialy put into your 2d array
// by using the regionIntX and regionIntY for the array values. You have to decide what is stored in this array...
}