AS3: defining hit area

2019-02-16 19:55发布

I have a movieclip which contains a bitmap and I wan't to increase the hit area. I understand I can add a transparent shape behind it but this is to be compiled through air for ios and I don't want to cause unnecessary redraws.

Is there a way to define a rectangle as the hit area or another solution perhaps?

2条回答
我命由我不由天
2楼-- · 2019-02-16 20:25

There is a special hitArea field for that purposes.

  const MOUSE_ZONE_SIZE:Number = 10;
  const hitArea:Sprite = new Sprite()
  hitArea.graphics.beginFill( 0xFFFFFF );
  hitArea.graphics.drawRect( -MOUSE_ZONE_SIZE, -MOUSE_ZONE_SIZE, MOUSE_ZONE_SIZE* 2, MOUSE_ZONE_SIZE* 2 );
  hitArea.mouseEnabled = false;
  hitArea.visible = false;
  hitArea.x = bitmap.x
  hitArea.y = bitmap.y
  bitmap.hitArea = hitArea;
  addChild( bitmap );
  addChild( hitArea );

Unfortunately even if you override hitTest* function of InteractiveObject they will not be used for mouse-events dispatching :( If somebody knows how to force Flash to use overridden methods - I'd like to know it too.

查看更多
Animai°情兽
3楼-- · 2019-02-16 20:43

You could also create a button with Bitmap inside it then define the hitArea. It essentially does the same thing that Ilya did in code. However when you add the new instance of the button to the stage you will be able to apply MouseEvents to it.

Pretty pictures :)

Create the button symbol

Create a new key frame in the hitArea

Draw the hit area

package{
import flash.display.MovieClip;
import flash.events.MouseEvent;

public class main extends MovieClip
{
    public function main()
    {
        var btn:button = new button();
        btn.addEventListener(MouseEvent.CLICK, clicked);
        this.addChild(btn);
    }

    private function clicked(e:MouseEvent):void{
        trace("Clicked");
    }

}
}

The problem is this will increase the amount of memory and power to do on the iOS so it's really a horse a piece... :(

查看更多
登录 后发表回答