[Stage3D] StarlingでTouchButtonクラス (2) ~touchGroupプロパティ~

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

starling_touchbutton2

TouchButtonクラスを作ってみたよ。 :boy:

Starling1.5から、DisplayObjectContainerクラスにtouchGroupプロパティが追加されてたんだって! これで、mouseChildren = false みたいなことができるようになるよ! :boy:
ただし、ヒットエリアっぽいQuadは機能しなくなるから、削除しちゃったよ! 😮

: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;

import starling.controls.TouchButton;
import starling.events.ButtonEvent;


internal class MainView extends Sprite {
  // プロパティ
  private var back:Quad;
  private var button:TouchButton;

  // コンストラクタ
  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);
    button = new TouchButton();
    addChild(button);

    button.id = "button";
    button.x = 300;
    button.y = 260;
    button.label = "button";
    button.addEventListener(TouchEvent.TOUCH, touch);
    button.addEventListener(ButtonEvent.PRESS, press);
    button.addEventListener(ButtonEvent.RELEASE, release);
    button.addEventListener(ButtonEvent.RELEASE_OUTSIDE, releaseOutside);

  }
  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 press(evt:ButtonEvent):void {
    trace("press");
  }
  private function release(evt:ButtonEvent):void {
    trace("release");
  }
  private function releaseOutside(evt:ButtonEvent):void {
    trace("releaseOutside");
  }
  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);
    }
  }

}

TouchButton.as (starling.controls.TouchButton.as)
package starling.controls {

  import flash.geom.Point;
  import starling.display.Sprite;
  import starling.display.Quad;
  import starling.display.Image;
  import starling.text.TextField;
  import starling.utils.HAlign;
  import starling.utils.VAlign;
  import starling.events.Touch;
  import starling.events.TouchEvent;
  import starling.events.TouchPhase;

  import starling.events.ButtonEvent;

  public class TouchButton extends Sprite {
    [Embed(source="assets/button_up.png")]
    private var ButtonUp:Class;
    [Embed(source="assets/button_down.png")]
    private var ButtonDown:Class;
    // プロパティ
    public var id:Object;
    private var upBtn:Image;
    private var downBtn:Image;
    private static var bw:uint = 218;
    private static var bh:uint = 118;
    private var _pressed:Boolean = false;
    private var txt:TextField;
    private static var fontType:String = "Myriad Apple Bold";

    // コンストラクタ
    public function TouchButton(n:String = "") {
      init();
      label = n;
    }

    // メソッド
    private function init():void {
      upBtn = Image.fromBitmap(new ButtonUp());
      addChild(upBtn);

      upBtn.x = - bw/2;
      upBtn.y = - bh/2;
      //upBtn.touchable = false;
      downBtn = Image.fromBitmap(new ButtonDown());
      addChild(downBtn);

      downBtn.x = - bw/2;
      downBtn.y = - bh/2;
      //downBtn.touchable = false;
      txt = new TextField(200, 20, "", fontType, 20, 0x000000);
      addChild(txt);

      txt.x = - 100;
      txt.y = - 14;
      txt.hAlign = HAlign.CENTER;
      txt.vAlign = VAlign.TOP;
      //txt.touchable = false;
      touchGroup = true;
      pressed = false;
      addEventListener(TouchEvent.TOUCH, touch);
    }
    private function touch(evt:TouchEvent):void {
      var touch:Touch = evt.getTouch(this);
      if (touch) {
        var point:Point = touch.getLocation(this);
        switch (touch.phase) {
          case TouchPhase.BEGAN :
            pressed = true;
            dispatchEvent(new ButtonEvent(ButtonEvent.PRESS));
            break;
          case TouchPhase.HOVER :
            break;
          case TouchPhase.MOVED :
            if (hitTest(point, true)) {
              pressed = true;
            } else {
              pressed = false;
            }
            break;
          case TouchPhase.ENDED :
            pressed = false;
            if (hitTest(point, true)) {
              dispatchEvent(new ButtonEvent(ButtonEvent.RELEASE));
            } else {
              dispatchEvent(new ButtonEvent(ButtonEvent.RELEASE_OUTSIDE));
            }
            break;
          case TouchPhase.STATIONARY :
            break;
          default :
            break;
        }
      }
    }
    public function get label():String {
      return txt.text;
    }
    public function set label(value:String):void {
      txt.text = value;
    }
    public function set textColor(value:uint):void {
      txt.color = value;
    }
    public function get pressed():Boolean {
      return _pressed;
    }
    public function set pressed(value:Boolean):void {
      _pressed = value;
      upBtn.visible = !value;
      downBtn.visible = value;
      if (_pressed) {
        txt.y = - 12;
      } else {
        txt.y = - 14;  
      }
    }

  }

}

ButtonEvent.as (starling.events.ButtonEvent.as)
package starling.events {

  import starling.events.Event;

  public class ButtonEvent extends Event {
    // プロパティ
    public static const PRESS:String = "press";
    public static const RELEASE:String = "release";
    public static const RELEASE_OUTSIDE:String = "release_outside";

    // コンストラクタ
    public function ButtonEvent(type:String, bubbles:Boolean = true) {
      super(type, bubbles, null);
    }

    // メソッド

  }

}