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

TRAIN Simulation

なんというか 出身がモロバレする
http://www.minimalcomps.com/documentation/com/bit101/components/Component.html
http://www40.atwiki.jp/spellbound/pages/97.html
↑参考
/**
 * Copyright YAZUMA ( http://wonderfl.net/user/YAZUMA )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/p1md
 */

//なんというか 出身がモロバレする
//http://www.minimalcomps.com/documentation/com/bit101/components/Component.html
//http://www40.atwiki.jp/spellbound/pages/97.html
//↑参考
package {
    import flash.ui.Mouse;
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;
   import fl.controls.*;
    import flash.utils.Timer;
    import mx.collections.ArrayCollection;
    import com.bit101.components.Window;
    import com.bit101.components.InputText;
    import com.bit101.components.PushButton;
    import com.bit101.components.Meter;
    import com.bit101.components.HSlider;
    import com.bit101.components.VSlider;
    import com.bit101.components.HUISlider;
    import com.bit101.components.VUISlider;
    import com.bit101.components.Text;
    import com.bit101.components.RadioButton;
    import com.bit101.components.Panel;
    import com.bit101.components.NumericStepper;

 
    [SWF(backgroundColor=0xcccccc,frameRate=60)]
    
    public class sp extends Sprite {
        public  var Accele:int = 0;
        private var Brake:int = 0;
        public  var Anotch:Array = [0, 2, 5, 10];
        private var Bnotch:Array = [0, 2, 5, 10, 20, 30];
        public  var weight:Number = 20000;    //重量 t
        public  var num:int = 1;    //車両数 
        public  var motor:int = 1;
        public  var speed:Number = 0;
        public  var master_location:Array = [19.6]; //京都-新田辺 data km
        public  var location:Array = [0.9, 2.7, 2.4, 0.5, 7.1, 6.0];
        public  var station_name:Array = ["京都", "東寺", "竹田", "丹波橋", "桃山御陵前", "大久保" , "新田辺"];
        public  var now_location:Array = [0.9]; //区間 data
        public  var pickup_location:Array = [0.9];
        //Sysyem
        public  var timer:Timer = new Timer(100);
        public  var speedy_timer:Timer = new Timer(10);
        public  var s1:Sprite = new Sprite;
        public  var s2:Sprite = new Sprite;
        public  var s3:Sprite = new Sprite;
        public  var s4:Sprite = new Sprite;
        public  var Aslider:HUISlider;
        public  var Bslider:HUISlider;
        public  var meter:Meter;
        public  var master:LineChart;
        public  var section:LineChart;
        public  var pickup:LineChart;
        public  var window1:Window;
        public  var window2:Window;
        public  var window3:Window;
        private var panel:Panel;
        private var panel2:Panel;
        private var Pbutton1:RadioButton;
        private var Pbutton2:RadioButton;
        private var Pbutton3:RadioButton;
        public  var mode:Boolean = false;
        private var mode_num:int = 0;
        private var count:int = 0;
        private var wait_count:int = 0;
        private var forward_10:Number = 0;
        private var name1:TextField = new TextField();
        private var name2:TextField = new TextField();
        private var task:TextField = new TextField();
        private var TASK:int = 0; // 0.waiting 1.running 2.over 3.goal
        private var waiting_time:int = -1;
        private var person:int = 0;
        private var station_count:int = 0;
        private var stepper2:NumericStepper;
        public function sp():void{
           initition();
        }
        private function initition():void{
            //Accele Brake Slider
            Aslider = new HUISlider(s1, 230, 390, "Accele", onChange);
            Aslider.maximum = 3;
            Aslider.scaleY = 1.2;
            Aslider.width = 250;
            Bslider = new HUISlider(s1, 230, 410, "Brake ", onChange2);
            Bslider.maximum = 5;
            Bslider.scaleY = 1.2;
            Bslider.width = 250;
            addChild(s1);
            //Speed Meter
            meter = new Meter(s1, 20, 350, "Speed km/h");
            meter.maximum = 150;
            //LineChart //from KYOTO to ShinTANABE
            master = new LineChart(400, 200, master_location, 0xeaf4fc);
            master.x = 40;
            master.y = 10;
            s1.addChild(master);
            var tf_start:TextField = new TextField();
            tf_start.defaultTextFormat = new TextFormat("メイリオ", 11, 0x68be8d, true);
            tf_start.text = "京都";
            tf_start.x = 5;
            var tf_end:TextField = new TextField();
            tf_end.defaultTextFormat = new TextFormat("メイリオ", 11, 0x68be8d, true);
            tf_end.text = "新田辺";
            tf_end.y = 200;
            s1.addChild(tf_start);
            s1.addChild(tf_end);
            //Window 5second timer
            window1 = new Window(s2, 290, 300, "Accele and Brake");
            window1.width = 120;
            window1.height = 80;
            window1.color = 0x93ca76;
            var window_tf1:TextField = new TextField();
            window_tf1.defaultTextFormat = new TextFormat("Arial", 9, 0x555555, true);
            window_tf1.text = "  Please change Accele \n    or  Brake bar's value.";
            window_tf1.y = 5;
            window_tf1.width = 200;
            window1.content.addChild(window_tf1);
            var button:PushButton = new PushButton(window1.content, 10, 35, "OK");
            button.addEventListener(MouseEvent.CLICK, clickButton);
            s2.addChild(window1);
            addChild(s2);
            //Window2
            window2 = new Window(null, 191, 230, "Section Chart");
            window2.width = 250;
            window2.height = 155;
            window2.hasMinimizeButton = true;
            window2.minimized = true;
            section = new LineChart(200, 120, now_location, 0xeebbcb);
            s3.addChild(section);
            s3.x = 40;
            s3.y = 10;
            name1.defaultTextFormat = new TextFormat("メイリオ", 9, 0x555555, true);
            name1.text = station_name[station_count];
            name1.y = 5;
            name2.defaultTextFormat = new TextFormat("メイリオ", 9, 0x555555, true);
            name2.text = station_name[station_count + 1];
            name2.y = 120;
            window2.content.addChild(name1);
            window2.content.addChild(name2);
            window2.content.addChild(s3);
            addChild(window2);
            //Window3
            window3 = new Window(null, 40, 230, "PickUp Chart");
            window3.width = 150;
            window3.height = 120;
            window3.hasMinimizeButton = true;
            window3.minimized = true;
            pickup = new LineChart(150, 100, pickup_location, 0xf5e56b);
            s4.addChild(pickup);
            window3.content.addChild(s4);
            addChild(window3);
            //Mode Button
            panel = new Panel(s1, 20, 270);
            panel.height = 70;
            panel.width = 120;
            Pbutton1 = new RadioButton(panel.content, 10, 10, "Normal mode", true, onSelect);
            Pbutton2 = new RadioButton(panel.content, 10, 30, "Real time simulation", false, onSelect);
            Pbutton3 = new RadioButton(panel.content, 10, 50, "High speed simulation", false, onSelect);
            //Train valiue change
            panel2 = new Panel(s1, 140, 270);
            panel2.height = 70;
            panel2.width  = 140;
            var stepper1:NumericStepper = new NumericStepper(panel2, 50, 15, ChangeTRAIN);
            stepper1.value = 1;
            stepper1.minimum = 1;
            stepper1.maximum = 8;
            stepper2 = new NumericStepper(panel2, 50, 40, ChangeTRAIN2);
            stepper2.value = 1;
            stepper2.minimum = 1;
            stepper2.maximum = 1;
            var window_tf2:TextField = new TextField();
            window_tf2.defaultTextFormat = new TextFormat("Arial", 9, 0x555555, true);
            window_tf2.text = "Train\n number\n\n Motor\n number";
            window_tf2.y = 6;
            window_tf2.x = 5;
            window_tf2.width = 200;
            panel2.content.addChild(window_tf2);
            //task viewer
            task.defaultTextFormat = new TextFormat("メイリオ", 9, 0x555555, true);
            task.x = 230;
            task.y = 435;
            task.width = 300;
            task.antiAliasType = "ADVANCED";
            task.text = "Task : Waiting";
            addChild(task);
            //timer
            timer.addEventListener(TimerEvent.TIMER, Location);
            speedy_timer.addEventListener(TimerEvent.TIMER, Location);
            timer.start();
        }
        private function ChangeTRAIN(event:Event):void
        {
            var stepper:NumericStepper = event.currentTarget as NumericStepper;
            num = stepper.value;
            stepper2.maximum = num;
            person = Math.floor(Math.random()*(150 - 0 + 1));
            person *= num;
            waiting_time = Math.floor(Math.abs(person - 0)*0.01+10);
            task.text = "Task : Waiting "+person+"/"+num*150+" waiting_time : "+waiting_time;
        }
        private function ChangeTRAIN2(event:Event):void
        {
            var stepper:NumericStepper = event.currentTarget as NumericStepper;
            motor = stepper.value;
        }
        private function onSelect(event:Event):void{
            var button:RadioButton = event.currentTarget as RadioButton;
            if(button.label == "Real time simulation"){
                mode = true;
                if(speedy_timer.running == true)speedy_timer.stop();
                if(mode_num == 0)removeChild(s2);
                mode_num = 1;
                timer.start();
            }else if(button.label == "Normal mode"){
                mode = false;
                mode_num = 0;
            }else if(button.label == "High speed simulation"){
                mode = true;
                if(timer.running == true)timer.stop();
                if(mode_num == 0)removeChild(s2);
                mode_num = 2;
                speedy_timer.start();
            }
        }

        private function clickButton(event:MouseEvent):void{
            removeChild(s2);
            timer.start();
        }
        private function onChange(e:Event):void{
            Accele = int(Aslider.value);
        }
        private function onChange2(e:Event):void{
            Brake = int(Bslider.value);
        }
        //Main System
        private function Location(e:TimerEvent):void{
            var accele:Number = Anotch[Accele] * 1000;
            var brake:Number = Bnotch[Brake] * 1000;
            if(speed <= 0.1)brake = 0;
            var m:Number = weight * num + person * 60;
            var rrc:Number = m * 9.8 * 0.001;    //転がり抵抗
            if(speed < 0.1)rrc = 0;
            var f:Number = accele * motor - brake * motor- rrc;
            var a:Number = f / m;
            var s:Number = a/1000;
            if(timer.running == true && mode == true)s /= 10;
            if(speedy_timer.running == true)s /= 10;
            speed += s * 3600;
      
            if(speed > 100)speed = 100;
            if(speed < 0.1 || TASK == 0)speed =0;
            var forward:Number = speed / 3600 ; //km/h 5second.
            
            //system update
            if(waiting_time == 0)TASK = 1;
            if(TASK == 1){          
            //走行      
                waiting_time = -1;
                if(mode == true){
                    forward /= 10;
                    forward_10 += forward;
                }
                if(master_location[master_location.length - 1] <= 0){
                    timer.stop();
                    speedy_timer.stop();                
                }
                if(master_location[master_location.length - 1] < 0.01){
                    timer.stop();
                    speedy_timer.stop();
                }
                if(count == 10 || mode == false){
                    if(mode == true)forward = forward_10;
                    forward_10 = 0;
                    master_location.push(master_location[master_location.length - 1] - forward);
                    master.Change_Data(master_location);  
                    now_location.push(now_location[now_location.length - 1] - forward);
                    section.Change_Data(now_location);
                    pickup_location.push(now_location[now_location.length - 1]);
                    if(pickup_location.length >= 10)pickup_location.shift();
                    pickup.Change_Data(pickup_location);
                    task.text = "Task : Running "+person+"/"+num*150;
                    if(now_location[now_location.length - 1] < 0){
                        timer.stop();
                        speedy_timer.stop();
                        task.text = "Task : Over  Failed.";
                        //initition();
                    }
                    if(now_location[now_location.length - 1] < 0.01 && speed < 5 && station_count <= 6){
                        //駅に到着
                        if(station_count == 5){
                            timer.stop();
                            speedy_timer.stop();
                            task.text = "Task : Goal! Now, ShinTANABE.";
                        }else{
                            station_count++;
                            master_location[master_location.length - 1] -= now_location[now_location.length - 1];
                            name1.text = station_name[station_count];
                            name2.text = station_name[station_count + 1];
                            now_location = [location[station_count]];
                            TASK = 0;
                            speed = 0;
                        }
                    }
                    count = 0;          
                }
                meter.value = speed;    
                count++;
                //next 5 second
                if(mode == false){
                    timer.stop();
                    speedy_timer.stop();
                    addChild(s2);
                }
            }
            if(TASK == 0 && waiting_time == -1){
                //乗車設定
                var tmp:int = person;
                person = Math.floor(Math.random()*(150 - 0 + 1));
                person *= num;
                waiting_time = Math.floor(Math.abs(person - 0)*0.01+10);
                task.text = "Task : Waiting "+person+"/"+num*150+" waiting_time : "+waiting_time;
            }
            if(TASK == 0){
                //駅停止中
                task.text = "Task : Waiting "+person+"/"+num*150+" waiting_time : "+waiting_time;
                if(wait_count == 10 || mode == false){
                    waiting_time--;
                    master_location.push(master_location[master_location.length - 1] - forward);
                    master.Change_Data(master_location);  
                    wait_count = 0;          
                }
                wait_count++;
                if(mode == false){
                    timer.stop();
                    speedy_timer.stop();
                    addChild(s2);
                }
            }
       }
    }
}
import flash.text.TextFormat;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
class LineChart extends Sprite {
  private var max_tf : TextField = new TextField();
  private var min_tf : TextField = new TextField();
  private var time_tf : TextField = new TextField();
  private var tf:TextFormat = new TextFormat("Arial", 15, 0xeb6101, true);
  private var now_location_tf : TextField = new TextField();
  private var data : Array = [];
  private var Chart_width:Number;
  private var Chart_height:Number;
  private var max:Number = 0;
  private var min:Number = 0;
  private var s1:Sprite = new Sprite;
  private var s2:Sprite = new Sprite;
  function LineChart(_width:Number, _height:Number, _data:Array, _Color:int){
    this.data = _data;
    Chart_width = _width;
    Chart_height = _height;
    min_tf.y = _height - (_height / 15);
    time_tf.x = Chart_width - 10;
    time_tf.y = Chart_height;
    time_tf.textColor = 0x9d5b8b;
    now_location_tf.x = 10;
    now_location_tf.y = Chart_height - (Chart_height / 10) - 5;
    now_location_tf.width = 200;
    now_location_tf.textColor = 0x5654a2;
    //addChild(max_tf);
    //addChild(min_tf);
    addChild(time_tf);
    addChild(now_location_tf);
    max_tf.width = 300;
    graphics.lineStyle(0,0x777777);
    graphics.beginFill(_Color);
    graphics.drawRect(0,0,_width,_height);
    graphics.endFill();
    Draw();
  }
  private function MaxMin():void{
      for(var i:int = 0;i < data.length;i++){
          if(max < data[i])max = data[i];
          if(min > data[i])min = data[i];
      }
      max_tf.text = "" + int(max);
      min_tf.text = "" + int(min);
      max_tf.x = 0 - (max_tf.length * 9);
      min_tf.x = 0 - (min_tf.length * 9);
  }
  private function Draw():void{
      MaxMin();
      for(var i:int = 0; i < data.length - 1; i++){
          var x:Number = Chart_width / (data.length - 1) * i;
          var next_x:Number = Chart_width / (data.length - 1) * (i+1);
          var y:Number = Chart_height / (max - min) * data[i];
          var next_y:Number = Chart_height / (max - min) * data[i+1];
          s1.graphics.lineStyle(2, 0x69b076);        
          s1.graphics.moveTo(x, Chart_height - y);
          s1.graphics.lineTo(next_x, Chart_height - next_y);
          addChild(s1);
          time_tf.text = "" + (data.length-1);
          now_location_tf.text = "location:" + Math.round(data[i+1] * 1000) / 1000 + " km";
          if(data[i+1]<1){
              now_location_tf.text = "location:" + Math.round(data[i+1] * 1000) + " m";
              now_location_tf.setTextFormat(tf, 9, now_location_tf.length);
          }
      }
  }
  public function Change_Data(_data:Array):void{
      data = _data;
      s1.graphics.clear();
      s2.graphics.clear();
      max = 0;
      min = 0;
      Draw();
  }

}