[Stage3D] StarlingのTouchとTouchEvent (3) ~touchGroupプロパティ~

Flash Player 11 から採用された Stage3D を学習するよ。 :bouzu:
Starling Framework を試しちゃうんだからね。 :girl:

starling_touch3

TouchクラスTouchEventクラスの挙動を少し整理してみたよ。 :boy:

Starling1.5から、DisplayObjectContainerクラスにtouchGroupプロパティが追加されてたんだって! これで、mouseChildren = false みたいなことができるようになるよ! :boy:
ただし、ボタン(Sprite)に、ヒットエリアっぽいQuadを配置してあるけど、意味がなくなって、button2の影部分も反応しちゃうよ! 😮

:caution: 要 Flash Player 11.8 以上

This movie requires Flash Player 11.8.0

Main.as
package {

  import flash.display.Sprite;
  import flash.display.StageScaleMode;
  import flash.display.StageAlign;

  import starling.core.Starling;

  [SWF(backgroundColor="#FFFFFF", width="600", height="500", frameRate="60")]

  public class Main extends Sprite {
    // プロパティ
    private var starling:Starling;

    // コンストラクタ
    public function Main() {
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;
      init();
    }

    // メソッド
    private function init():void {
      starling = new Starling(MainView, stage, null, null);
      starling.enableErrorChecking = true;
      starling.start();
    }

  }

}



import starling.core.Starling;
import starling.display.Sprite;
import starling.display.Quad;
import starling.display.Image;
import starling.events.Event;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;


internal class MainView extends Sprite {
  [Embed(source="assets/button.png")]
  private var ButtonImage:Class;
  // プロパティ
  private var back:Quad;
  private var button1:Image;
  private var button2:Sprite;

  // コンストラクタ
  public function MainView() {
    addEventListener(Event.ADDED_TO_STAGE, init);
    addEventListener(Event.REMOVED_FROM_STAGE, remove);
  }

  // メソッド
  private function init(evt:Event):void {
    removeEventListener(Event.ADDED_TO_STAGE, init);
    back = new Quad(600, 500, 0x000000);
    addChild(back);

    back.setVertexColor(0, color1);
    back.setVertexColor(1, color1);
    back.setVertexColor(2, color2);
    back.setVertexColor(3, color2);

    stage.addEventListener(TouchEvent.TOUCH, touch);
    button1 = Image.fromBitmap(new ButtonImage());
    addChild(button1);

    button1.x = 180 - button1.width/2;
    button1.y = 260 - button1.height/2;
    button1.addEventListener(TouchEvent.TOUCH, touch1);
    button2 = new Sprite();
    addChild(button2);

    var area:Quad = new Quad(200, 100, 0x000000);
    area.x = 9;
    area.y = 7;
    area.alpha = 0;
    button2.addChild(area);

    var image:Image = Image.fromBitmap(new ButtonImage());
    button2.addChild(image);

    //image.touchable = false;
    button2.x = 420 - button2.width/2;
    button2.y = 260 - button2.height/2;
    button2.touchGroup = true;
    button2.addEventListener(TouchEvent.TOUCH, touch2);
  }
  private function remove(evt:Event):void {
    removeEventListener(Event.REMOVED_FROM_STAGE, remove);
  }
  override public function dispose():void {
    removeEventListener(Event.ADDED_TO_STAGE, init);
    removeEventListener(Event.REMOVED_FROM_STAGE, remove);
    super.dispose();
  }
  private function touch(evt:TouchEvent):void {
    var touch:Touch = evt.getTouch(stage);
    if (touch) {
      trace(touch.phase, touch.globalX, touch.globalY, touch.target, evt.target, evt.currentTarget);
    }
  }
  private function touch1(evt:TouchEvent):void {
    var touch:Touch = evt.getTouch(stage);
    if (touch) {
      trace(touch.phase, touch.globalX, touch.globalY, touch.target, evt.target, evt.currentTarget);
    }
  }
  private function touch2(evt:TouchEvent):void {
    var touch:Touch = evt.getTouch(stage);
    if (touch) {
      trace(touch.phase, touch.globalX, touch.globalY, touch.target, evt.target, evt.currentTarget);
    }
  }

}