2011-12-17 18 views
1

我有許多10x10像素的正方形網格,只有一個像素線分開。當用戶滑過精靈時,它會增長,動畫大小約爲其大小的4倍 - 大約爲40x40像素。當鼠標移動到精靈之上時,精靈會保持很大的狀態,但是如果用戶想要翻轉鄰居精靈,它會被這個精靈和它的較大尺寸遮住。如何在RollOver上縮小雪碧的hitArea

是否可以縮小命中區域的大小,以便即使精靈長大,鼠標仍然可以訪問或觸發下面的鄰近精靈 - 向右,左,上,下?我想過使用Mouse_move,但不知道是否會引發太多事件。

我閱讀以下帖子。但是,我的hitArea需要與原始10x10像素精靈的大小相同或略大。我沒有成功。

不知道這是否重要:我在Flex項目中使用ActionsScript類。主要的類文件是mxml。

Routing Mouse Events through a Sprite in Actionscript 3

AS3: defining hit area

public function onRollOver(event:MouseEvent):void 
{ 
    //do a bunch of stuff 
    timerUp.start(); 
    timerUp.addEventListener(TimerEvent.TIMER, growSquare);  
} 

private function growSquare(e:TimerEvent):void 
{ 
    var maxSize:Number = 40;  
    if (currSprite.width < maxSize) { 
     currSprite.width++; 
     currSprite.height++; 
     if (currSprite.width > maxSize) { 
     currSprite.width = maxSize; 
     currSprite.height = maxSize; 
     } 
     e.updateAfterEvent(); 
     } else { 
      timerUp.stop(); 
     } 
    } 


    /////////Hit Area Code - From Square Object ////////////////// 

     var vHeight:Number = 10; 
     var vWidth:Number = 10; 
     var square:Sprite = new Sprite(); 
     square.graphics.beginFill(0x000000); 
     square.graphics.drawRect(0, 0, vWidth, vHeight); 
     square.graphics.drawRect(-vWidth/2, -vHeight/2, vWidth, vHeight); 
     square.graphics.endFill(); 

     // try to decrease the hit area, or keep it at the original size 
     const hitArea:Sprite = new Sprite() 
     hitArea.graphics.beginFill(0xFFFFFF); 
     hitArea.graphics.drawRect(-vWidth/2, -vHeight/2, vWidth, vHeight); 
     hitArea.mouseEnabled = false; 
     hitArea.visible = false; 
     hitArea.x = square.x 
     hitArea.y = square.y 
     square.hitArea = hitArea; 
     addChild(square); 
     addChild(hitArea); 
+0

另外,currentSprite是翻轉過的。它已被移動到使用setChildIndex顯示列表的頂部... – cin13 2011-12-17 07:43:43

回答

2

你可以你的精靈分爲兩個subsprites:一個用於災區和一個用於繪圖。類似的東西:

public class GridBox extends Sprite 
{ 
    public var hitAreaSprite:Sprite; 
    public var drawingSprite:Sprite; 

    public function GridBox() 
    { 
     hitAreaSprite = new Sprite(); 
     // Draw a 10x10 pixel box in hitAreaSprite, add event listeners, etc. 

     drawingSprite = new Sprite(); 
     // Disable mouse input for drawingSprite here. 

     addChild(hitAreaSprite); 
     addChild(drawingSprite); 
    } 
} 

... 

var yourSprite:GridBox = new GridBox(); 

然後讓yourSprite.hitAreaSprite是10×10像素,並添加鼠標監聽事件給它。 yourSprite.drawingSprite必須包含所有的圖形元素和圖紙。將其mouseChildrenmouseEnabled屬性設置爲false。

然後,當精靈應該增長時,不要直接更改yourSprite.width,而是更改yourSprite.drawingSprite.width。由於這個子精靈沒有啓用鼠標,並且hitAreaSprite沒有移動,下面的精靈仍然會接收鼠標事件。

+0

這聽起來很有希望。但僅僅爲了澄清,「YourSprite」是另一個對象或類,而不是實際的Sprite類。 「YourSprite」對象裏有兩個精靈,對嗎?我將嘗試此操作並很快就會發表評論。 – cin13 2011-12-17 17:38:26