バウンドチャイム(なんやそれ?)

バウンドチャイム


ベルはドラッグ可能です。いろんな位置に動かしてみて下さい。
容量をできるだけ小さくするために背景などを削除したへろへろな作品です(笑)

有名なページTAKAGISMの中のLOGOBALLという作品をヒントに制作してみました。
ただバウンドするだけではおもしろくないので、音でちょっと遊んでみました。
さて、問題のスクリプトの方ですが、一番やっかいなのはバウンド処理です。このバウンドの軌跡を書くためには様々な数学的知識を使わなくてはいけません。
タネをあかしますと、これは厳密な意味での落下運動ではありません。サインカーブを擬似的にバウンド処理しているだけです。

昔高校でやった数学を思い出していただきたいのですが、このバウンドの軌跡はサインカーブの絶対値……つまりX軸で折り曲げているだけです。細かな設定などは試行錯誤なのですが、

set the locV of sprite mySpNum to abs(the bound of me*sin((the myT of me/kagen )/pi()))*(-1)+280

という部分で処理しています。absとは絶対値の事。Directorの扱う座標系は左上が原点なので、シフトしてから絶対値処理をかけています。
the bound of meとは跳ね返りの割合を擬似的に処理するための変数です。この変数は10から150までの間を振動するように設定してあります。
the myT of meとはステップ数です。……ええと、プロパティなどの扱いがめんどうなので、全スクリプトを公開します。

--myH=子オブジェクトの水平座標
--myV=子オブジェクトの垂直座標
--myColor=子オブジェクトの描画色
--myT=子オブジェクトのステップ数
--mySpNum=子オブジェクトのスプライト番号
--mydirH=子オブジェクトが水平方向正か負かのフラグ
--bound=子オブジェクトの跳ね返る高さの限度
--hMove=子オブジェクトの水平方向の移動量
--boundFlag=子オブジェクトが跳ね返り収束に向かっているかどうかのフラグ

property myH,myV,myColor,myT,mySpNum,mydirH,bound,hMove,boundFlag

on new me,num
--初期設定とか
 set the myH of me to random(250)+20
 set the myV of me to random(250)+20
 set the myColor of me to random(256)-1
 set the myT of me to random(300)
 set the mySpNum of me to num
 set the myDirH of me to 1
 set the boundFlag of me to 1
 set the bound of me to (random(7)+3)*15
 set the hMove of me to random(5)+15
 return me
end

on move me
 --kagenとはプロパティでなく、グローバル変数です
 --on startMovieを参照して下さい

 global kagen
 --回転させるためにmyTを5で割り、キャストを変更しています
 --modを使うと、フィルムループよりも安定して動作します

 set the castNum of sprite mySpNum to the myT of me mod 5 + 11
 --ここから5行は音を出すための部分で、あまり関係ありません
 if sprite mySpNum intersects 10 then puppetSound 3 "5"
 if sprite mySpNum intersects 11 then puppetSound 4 "4"
 if sprite mySpNum intersects 12 then puppetSound 5 "3"
 if sprite mySpNum intersects 13 then puppetSound 6 "2"
 if sprite mySpNum intersects 14 then puppetSound 7 "1"
 --子オブジェクトが画面端にいったら折り返すようにフラグを変更します
 if the myH of me <1 or the myH of me >300 then
  set the mydirH of me to the myDirH of me *(-1)
 end if
 --水平方向は徐々に動きが遅くなるようにします
 set the hMove of me to the hMove of me -0.1
 --0になったらまずいので、そのときは再設定します
 if the hMove of me <0 then set the hMove of me to random(5) +15
 --ステップ数を増やします
 set the myT of me to the myT of me + 1
 --バウンドの大きさを設定します
 --200と10の間をいったりきたりします

 if the bound of me <10 then
  set the boundFlag of me to 1
 else if the bound of me >200 then
  set the boundFlag of me to -1
 end if
 set the bound of me to the bound of me +3*the boundFlag of me
 --ここがサインカーブを描画する大事な部分です
 set the locV of sprite mySpNum to abs(the bound of me*sin((the myT of me/kagen )/pi()))*(-1)+280
 --水平方向の移動も忘れずに行います
 set the locH of sprite mySpNum to the myH of me + the hMove of me*(the myDirH of me)
 set the myH of me to the locH of sprite mySpNum
 --色をつけます
 set the foreColor of sprite mySpNum to the myColor of me
end

on creatBalls
 --5つのボールの名前をグローバル宣言します
 --名前は内容と全く関係ありませんので……

 global watasi,kodomo,mago,himago,yasyamago
 set watasi to new (script"oya",1)
 set kodomo to new(script"oya",2)
 set mago to new(script"oya",3)
 set himago to new(script"oya",4)
 set yasyamago to new(script"oya",5)
end

on exitFrame
 --子オブジェクトを動かします
 global watasi,kodomo,mago,himago,yashamago
 move watasi
 move kodomo
 move mago
 move himago
 move yasyamago
 go to the frame
end

on startMovie
 --いろんな値の設定です
 global kagen
 repeat with m=1 to 5
  puppetsprite m,TRUE
 end repeat
 set kagen to 3.7
 put kagen into field "smooth"
end

かなりいい加減なスクリプトですが、もっと洗練して記述することもできるようです。腕に覚えのある方は、一度試してみては如何でしょうか。
© Akira SAno 1996
一応こんなページでも著作権は佐野彰に存在します
リンクフリーですが、メールでご連絡頂けたら嬉しいです。
96/11/23更新 homeホームに戻る