2014-05-21 71 views
2

我想知道如果我有一個數組推動是位圖的內容,我怎麼得到點擊時特定圖像的索引。我試圖使用indexOf但沒有運氣,我的代碼如下。 感謝您的時間!如何從位圖數據(從數組)獲取位圖圖像的索引?

代碼:

//First Part is where i add the URLRequest and add the image into contentHolder then onto Stage 
function loadImage():void { 
    for(var i:int = 5; i < somedata.length; i++){ 
     if(somedata[i]){ 
      var loader:Loader = new Loader(); 
      loader.load(new URLRequest("http://www.rentaid.info/rent/"+somedata[i])); 
      loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded); 

     } 
    } 
} 


function onImageLoaded(e:Event):void { 
    loadedArray.push(e.target.content as Bitmap); 

    for(var i:int = 0; i < loadedArray.length; i++){ 
     var currentY1:int = 200; 

     e.currentTarget.loader.content.height =200; 
     e.currentTarget.loader.content.y += currentY1; 
     currentY1 += e.currentTarget.loader.content.height +300; 
     _contentHolder.mouseChildren = false; // ignore children mouseEvents 
     _contentHolder.mouseEnabled = true;  // enable mouse on the object - normally set to true by default 
     _contentHolder.useHandCursor = true;  // add hand cursor on mouse over 
     _contentHolder.buttonMode = true; 

     _contentHolder.addChild(loadedArray[i]); 
     addChild(_contentHolder); 
     _contentHolder.addEventListener(MouseEvent.CLICK, gotoscene); 
    } 
} 

// then the part where i try to get the index 
function gotoscene(e:MouseEvent):void { 
    var index:Number; 
    index = loadedArray.indexOf(e.target); 
    trace(index); 
} 

編輯:

var viewport:Viewport = new Viewport(); 

    viewport.y = 0; 

    viewport.addChild(_contentHolder); 
+0

你能格式化你的代碼?如何調用gotoscene?刺入黑暗中,將e.target改爲e.currentTarget。 –

+0

嗨,對不起,我編輯了代碼,包括如何在代碼中調用gotoscene,當content_Holder被點擊時,它是一個mouseEvent。我試過e.currentTarget,沒有運氣,它顯示索引爲-1。 – Benyaman

+0

位圖不接受鼠標點擊,因此您接收到的是一個Sprite(或者甚至可能是一個Loader)。所以你應該得到內容,然後查詢你的數組。 – Vesper

回答

2

你的第一個問題很簡單的答案:

var image:Bitmap = new Bitmap(); 
var images:Array = new Array(image); 

for (var i:uint = 0; i < images.length; i++) { 
    // images[i].bitmapData is the original image in your array 
    // image.bitmapData is searched one 
    if (images[i].bitmapData == image.bitmapData) { 
     // found 
    } 
} 

但你的問題是比這更大。我看到你繼續四處遊蕩..

你應該爲每個孩子添加監聽器,而不是內容持有者。我通常不使用Loaders,而是獲取他們的Bitmap,並將它們包裝在Sprites或其他東西中,我將它們添加到場景中。您應該將此Sprite或您的Loader存儲到該數組中,而不是位圖。然後將偵聽器添加到它們中的每一個(Sprite或Loader,而不是Bitmap)並獲取目標。根據您存儲在數組中的東西,你可以很容易地得到它爲:

function gotoscene(e:MouseEvent):void { 
    var index:uint = loadedArray(indexOf(e.target)); 
} 

但是存儲一個特定類型實際上是點擊是很重要的。不要考慮位圖 - 它只是一個圖形表示,並且在代碼中沒有太多工作。

**編輯: 好吧我加入的代碼,你需要而是要了解你在做什麼是非常重要的,而不是僅僅依靠別人的答案:)

function onImageLoaded(e:Event):void { 
    var bitmap:Bitmap = e.target.content as Bitmap; // get the Bitmap 

    var image:Sprite = new Sprite(); 
    image.addChild(bitmap); // wrap it inside new Sprite 

    // add listener to Sprite! 
    image.addEventListener(MouseEvent.CLICK, gotoscene); 

    // gets url of current image (http://website.com/images/image1.jpg) 
    var url:String = e.target.loaderURL; 

    // get only the number from that url by replacing or by some other way 
    // this removes the first part and results in "1.jpg" 
    var name:String = url.replace("http://website.com/images/image", ""); 
    // this removes the extension and results in number only - 1, 2, 3 
    // it's important to change this depending on your naming convention 
    name = name.replace(".jpg", ""); 
    image.name = "button" + name; // results in "button1", "button2", "button3" 

    // store object, name, or whatever (not really needed in your case, but commonly used) 
    loadedArray.push(image.name); 

    image.x = counter * 100; // position so you can see them, at 100, 200, 300, etc. 
    _contentHolder.addChild(image); // add newly created Sprite to content 
} 

function gotoscene(e:MouseEvent):void { 
    var name:String = e.target.name; 

    // strips down "button" from "button1", and only the number remains, 
    // which is 1, 2, 3, etc. the number of the scene :) 
    var scene:uint = name.replace("button", ""); 

    // you're the man now :) 
} 
+0

好的代碼!我唯一擔心的是,比較bitmapData可能會有一些性能問題。如果對於項目來說似乎合理,則可以使用並行/二維數組來爲索引「索引」字符串。再說一遍,這裏完全可能不適用。只是一個想法。 Upvote都一樣。 – CodeMouse92

+1

@ JasonMc92當然你是對的:) LoaderInfo具有loaderURL屬性,可以在加載器完成時通過event.target獲取。這意味着數組只能存儲圖像的URL,並且可以稍後進行搜索。問題出在我們想要檢查用戶點擊哪個圖像時。當然,還有一個更好的選擇 - 將位圖封裝到Sprite中,並將其保存在數組中,或者保存實例名稱。反正有很多選項可以優化,我的代碼只是一個示例:) –

+0

Hm ,很好的解決方法。事實上,另一個「至少有10個好方法可以做到這一點」的類型。 – CodeMouse92