サンプル 10:ボタンによって動くMovieClip


ソースは以下になります。

 
     1	#!/usr/local/bin/ruby
     2	
     3	require 'ming'
     4	
     5	# ボタンとActionScriptの連携の練習をしてみましょう。
     6	
     7	def makeShapeWithBitmap(filename)
     8	  b=SWFBitmap.new(filename);
     9	  width=b.getWidth;
    10	  height=b.getHeight;
    11	
    12	  s=SWFShape.new;
    13	  f = s.addFill(b, SWFFILL_CLIPPED_BITMAP);
    14	  s.setRightFill(f);
    15	  s.movePenTo(0,0);
    16	  s.drawLineTo(width,0);
    17	  s.drawLineTo(width,height);
    18	  s.drawLineTo(0,height);
    19	  s.drawLineTo(0,0);
    20	
    21	  return s;
    22	end
    23	
    24	# Movieを作る
    25	m=SWFMovie.new;
    26	m.setDimension(400,400);
    27	m.setRate(10);
    28	
    29	# ボタンを作る
    30	su = makeShapeWithBitmap("button_u.dbl");
    31	sd = makeShapeWithBitmap("button_d.dbl");
    32	so = makeShapeWithBitmap("button_o.dbl");
    33	sh = makeShapeWithBitmap("button_h.dbl");
    34	
    35	b1=SWFButton.new;
    36	b1.setUp(su);
    37	b1.setDown(sd);
    38	b1.setOver(so);
    39	b1.setHit(sh);
    40	
    41	b2=SWFButton.new;
    42	b2.setUp(su);
    43	b2.setDown(sd);
    44	b2.setOver(so);
    45	b2.setHit(sh);
    46	
    47	b3=SWFButton.new;
    48	b3.setUp(su);
    49	b3.setDown(sd);
    50	b3.setOver(so);
    51	b3.setHit(sh);
    52	
    53	# MovieClip 1を作る
    54	
    55	mc1=SWFMovieClip.new;
    56	sh1=SWFShape.new;
    57	sh1.setLine(2, 0xc0,0x20,0xff);
    58	sh1.movePenTo(-50,-50);
    59	sh1.drawLine(100,0);
    60	sh1.drawLine(0,100);
    61	sh1.drawLine(-100,0);
    62	sh1.drawLine(0,-100);
    63	sh1.drawLine(100,100);
    64	sh1.movePenTo(50,-50);
    65	sh1.drawLine(-100,100);
    66	di =mc1.add(sh1);
    67	for i in (0 .. 90)
    68	  di.rotate(4);
    69	  mc1.nextFrame;
    70	end
    71	
    72	# MovieClip 2を作る
    73	
    74	mc2=SWFMovieClip.new;
    75	for i in (1 .. 60)
    76	  s = SWFShape.new;
    77	  s.setLine(2, 0, 0xff, 0xb0);
    78	  s.movePenTo(-i*2,-i*2);
    79	  s.drawLine(i*4,0);
    80	  s.drawLine(0,i*4);
    81	  s.drawLine(-i*4,0);
    82	  s.drawLine(0,-i*4);
    83	  di = mc2.add(s);
    84	  mc2.nextFrame;
    85	  mc2.remove(di);
    86	end
    87	
    88	# MovieClip 3を作る
    89	
    90	mc3=SWFMovieClip.new;
    91	sh1=SWFShape.new;
    92	sh1.setLine(4,0xff,0,0xff,80);
    93	sh1.movePenTo(0,-60);
    94	sh1.drawLine(-50,80);
    95	sh1.drawLine(100,0);
    96	sh1.drawLine(-50,-80);
    97	
    98	sh2=SWFShape.new;
    99	sh2.setLine(4,0,0xff,0,80);
   100	sh2.movePenTo(0,60);
   101	sh2.drawLine(-50,-80);
   102	sh2.drawLine(100,0);
   103	sh2.drawLine(-50,80);
   104	
   105	i1 = mc3.add(sh1);
   106	i2 = mc3.add(sh2);
   107	for i in (0 .. 90);
   108	  i1.rotate(4);
   109	  i2.rotate(-4);
   110	  mc3.nextFrame;
   111	end
   112	
   113	
   114	# ボタンのアクションを記述
   115	
   116	b1.addAction(SWFAction.new("this.gotoAndPlay('play_mc1');"),
   117				   SWFBUTTON_MOUSEDOWN);
   118	b1.addAction(SWFAction.new("this.gotoAndPlay('play_mc1');"),
   119				   b1.SWFBUTTON_KEYPRESS('1'));
   120	
   121	b2.addAction(SWFAction.new("this.gotoAndPlay('play_mc2');"),
   122				   SWFBUTTON_MOUSEDOWN);
   123	b2.addAction(SWFAction.new("this.gotoAndPlay('play_mc2');"),
   124				   b2.SWFBUTTON_KEYPRESS('2'));
   125	
   126	b3.addAction(SWFAction.new("this.gotoAndPlay('play_mc3');"),
   127				   SWFBUTTON_MOUSEDOWN);
   128	b3.addAction(SWFAction.new("this.gotoAndPlay('play_mc3');"),
   129				   b3.SWFBUTTON_KEYPRESS('3'));
   130	
   131	# ボタン用のテキストを作成
   132	f = SWFFont.new("OmegaSerif88591-B.fdb");
   133	t1 = SWFText.new;
   134	t2 = SWFText.new;
   135	t3 = SWFText.new;
   136	
   137	t1.setFont(f);
   138	t1.setColor(0, 0, 0);
   139	t1.setHeight(20);
   140	t2.setFont(f);
   141	t2.setColor(0, 0, 0);
   142	t2.setHeight(20);
   143	t3.setFont(f);
   144	t3.setColor(0, 0, 0);
   145	t3.setHeight(20);
   146	
   147	t1.addString("Movie1");
   148	t2.addString("Movie2");
   149	t3.addString("Movie3");
   150	
   151	# ボタンを追加して配置
   152	
   153	i1=m.add(b1);
   154	i2=m.add(b2);
   155	i3=m.add(b3);
   156	
   157	i1.moveTo(30,300);
   158	i2.moveTo(160,300);
   159	i3.moveTo(290,300);
   160	
   161	# ボタン用テキストを追加して配置
   162	
   163	it1=m.add(t1);
   164	it2=m.add(t2);
   165	it3=m.add(t3);
   166	
   167	it1.moveTo(40,350);
   168	it2.moveTo(170,350);
   169	it3.moveTo(300,350);
   170	
   171	# MovieClipを配置
   172	
   173	imc1 = m.add(mc1);
   174	imc1.moveTo(100,100);
   175	imc1.setName("mc1");
   176	
   177	imc2 = m.add(mc2);
   178	imc2.moveTo(200,100);
   179	imc2.setName("mc2");
   180	
   181	imc3 = m.add(mc3);
   182	imc3.moveTo(300,100);
   183	imc3.setName("mc3");
   184	
   185	# ActionScriptを記述しつつFrameを進める
   186	
   187	m.add(SWFAction.new("_root.mc1.stop();
   188	_root.mc2.stop();
   189	_root.mc3.stop();"));
   190	
   191	m.nextFrame;
   192	
   193	# ここが言うなればメインループ
   194	m.labelFrame("main_loop");
   195	m.nextFrame;
   196	m.add(SWFAction.new("this.gotoAndPlay(_currentFrame-1);"));
   197	m.nextFrame;
   198	
   199	# MovieClip 1を再生する
   200	m.labelFrame("play_mc1");
   201	m.add(SWFAction.new("_root.mc1.play();"));
   202	
   203	for i in (0 .. 90);
   204	  m.nextFrame;
   205	end
   206	
   207	m.add(SWFAction.new("_root.mc1.stop();this.gotoAndPlay('main_loop');"));
   208	m.nextFrame;
   209	
   210	# MovieClip 2を再生する
   211	m.labelFrame("play_mc2");
   212	m.add(SWFAction.new("_root.mc2.play();"));
   213	
   214	for i in (0 .. 60);
   215	  m.nextFrame;
   216	end
   217	
   218	m.add(SWFAction.new("_root.mc2.stop();this.gotoAndPlay('main_loop');"));
   219	m.nextFrame;
   220	
   221	# MovieClip 3を再生する
   222	m.labelFrame("play_mc3");
   223	m.add(SWFAction.new("_root.mc3.play();"));
   224	
   225	for i in (0 .. 90);
   226	  m.nextFrame;
   227	end
   228	
   229	m.add(SWFAction.new("_root.mc3.stop();this.gotoAndPlay('main_loop');"));
   230	m.nextFrame;
   231	
   232	m.save('example10.swf');


説明は[TBD]

ボタンのサンプルの画像データ。
Masahiko KIMOTO <kimoto@ohnolab.org>
Last modified: Sun Jan 18 15:01:20 JST 2004