AS3单击图片的“区域”

时间:2013-10-24 17:44:12

标签: actionscript-3 flash mxml

我已经搜索过,根本无法找到我需要的东西(如果存在)。

  1. 窗口会有很大的图片。

  2. 图片将分为多个区域(例如在地图上分隔状态的边框线)。

  3. 当某人在区域内点击时,我会举起相应的活动。

  4. 我使用AS3和MXML来创建数据库程序。除了最后一步,一切都很好。当他点击或接触时,我无法弄清楚用户是如何在图片的特定区域内进行的。

    我已经读过并尝试提出一种方法,并且必须(希望如此)比我想出的混乱的废话更容易。

    由于

    VL

2 个答案:

答案 0 :(得分:0)

你是用flash专业CS6画的吗?如果是这样,那么为什么你不能只将图片作为符号然后自行划分线条并将这些划分的区域划分为图片符号的子元素。您可以将各个州的符号保持在正确的位置,以便它们能够忠实于整体画面。

首先想到的是通过代码制作这个图片符号的实例,然后遍历该图片的所有子项,并为每个图片添加一个点击事件。

var picture:Picture=new Picture();
for(var i:int=0; i<picture.numChildren-1; i++){
 picture.getChildAt(i).addEventListener(MouseEvent.CLICK, mouseEventHandler);
}

如果我遗漏了某些内容,请发表评论,否则这不起作用。

修改

好吧,如果您知道图像的尺寸,可以将其宽度和高度除以3(行数和列数),这是您的区域尺寸。然后,您可以将鼠标的点击点相对于图片的左上角,然后将其宽度除以区域,并将其高度除以区域高度,然后获得其整数楼层值,您可以得到它所在的区域。编码如下:

    //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...
}

答案 1 :(得分:0)

最简单的解决方案是将MouseEvent.CLICK的事件侦听器添加到图片中,并在处理程序中检查图片的mouseX和mouseY属性。在XML或类似物中定义每个区域的边界,并检查当前的mouseX / Y以查看已单击的区域。