In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

方向キーで移動する

動きがあるかをswitchで一発判定できるようにしています。
Get Adobe Flash player
by lla 18 Jun 2012
    Embed
/**
 * Copyright lla ( http://wonderfl.net/user/lla )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/rTyv
 */

package {
    //import flash.display.Graphics;
    //import flash.display.Shape;
    //import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.text.TextField;
    
    public class Test extends Sprite {
      public static var tf : TextField;
      public function Test(){
        stage.frameRate = 10;
        tf = new TextField();
        tf.border = true;
        addChild(tf);
        
        
        var obj = new Sprite();
            var g = obj.graphics;
            g.lineStyle(0);
            g.beginFill(0);
            g.drawRect(0,0,40,40);
            g.endFill();
            addChild(obj);
            obj.x = obj.y = 100;

        var obj2 = new Sprite();
            var g = obj2.graphics;
            g.lineStyle(0);
            g.beginFill(0);
            g.drawRect(40,40,20,20);
            g.endFill();
            addChild(obj2);

        var d = new Dpad(obj, 10);
        var d2 = new Dpad(obj2, 10);
        d2.inverse();
        
      }
    }
}

import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.DisplayObject;
class Dpad {  

  static var x  = 2; //0b10
  static var  y = 1; //0b01
  static var xy = 3; //0b11

  public static var direction : int;
  public static var neg_direction : int;

  public var obj: DisplayObject;
  
  public var straightSpeed : Number;
  public var diagonalSpeed : Number;

  public function Dpad(obj, speed : Number) {
    this.obj = obj;
    direction = 0;
    neg_direction = 0;
    straightSpeed = speed;
    diagonalSpeed = speed * (Math.sqrt(2) / 2.0);
    
    if(obj.stage != null) {
        addEvents();
    } else {
        obj.addEventListener(Event.ADDED_TO_STAGE, function(e){ addEvents();});
    }
  }

  public function inverse(): void {
    straightSpeed = -straightSpeed;
    diagonalSpeed = -diagonalSpeed;
  } 

  public function xmotion(): Boolean {
    return !((direction & x) == 0);
  }
  public function ymotion(): Boolean {
    return !((direction & y) == 0);
  }

  public function move(event): void {
    ///Test.tf.text = String(direction ^ neg_direction);
    switch(direction ^ neg_direction) {
      case 0: return;
      case x: obj.x += (!xmotion()) ? -straightSpeed : straightSpeed; break;
      case y: obj.y += (!ymotion()) ? -straightSpeed : straightSpeed; break;
      case xy:
        obj.x += (!xmotion()) ? -diagonalSpeed : diagonalSpeed;
        obj.y += (!ymotion()) ? -diagonalSpeed : diagonalSpeed;
        break;
    }
    
  }
  //
  ////////////////////////////

    
  ///////////////////////////////
  //
  
  public function addEvents(): void {
    obj.addEventListener(flash.events.Event.ENTER_FRAME, move);
    
    obj.stage.addEventListener(flash.events.KeyboardEvent.KEY_DOWN, moveStart);
    obj.stage.addEventListener(flash.events.KeyboardEvent.KEY_UP,  moveStop);
    //stage.addEventListener(flash.events.Event.DEACTIVATE, moveStop);
  }

  public static function moveStop(event): void {
    switch(event.keyCode) 
    { 
      case Keyboard.LEFT: neg_direction &= ~x; break;
      case Keyboard.DOWN: direction &= ~y; break;
      case Keyboard.UP: neg_direction &= ~y; break;
      case Keyboard.RIGHT: direction &= ~x; break;
    }
  }
    
  //keyRepeat offしたら軽くなるかも
  // flash.ui.Keyboard.LEFT
  public static function moveStart(event : flash.events.KeyboardEvent): void{ 
    switch(event.keyCode) 
    { 
      // 37 40 38 39 : left down up right
      case Keyboard.LEFT: neg_direction |= x; break;
      case Keyboard.DOWN: direction |= y; break;
      case Keyboard.UP: neg_direction |= y; break;
      case Keyboard.RIGHT: direction |= x; break;
    }
  }
  //
  /////////////////////////////
  
}