funfunfun38
バウンシング・ボール
バウンシング・ボールを作ってみるです。この発展形としてはブロック崩しとか
テニスゲームとかいろいろ作れそうだね。
おいらの場合は「粒子遊び(Play with Particle)」の第一歩ってかんじかにゃ。
■ボールとケース
今回はボールのクラス(Particle)とケース(Case)のクラスの2つのクラスを作るです。
ボールをケースのサブモーフにします。
■クラスカテゴリを作る
「ParticleTest」というクラスカテゴリを新たに作ります。
「Browser」左上のカテゴリペインで右クリック(Opt+クリック)すると
メニューが出てくるので「項目を追加・・・」を選びます。
出てきたダイアログにカテゴリの名前を付けるです。
ローマ字表記で頭文字はは大文字でね。「了解」のボタンをクリック!
ほい!クラスカテゴリの登録おしまいっす!!
このなかにクラスを作っちゃいます。
■ボールのクラスを作る
● Paticleクラス
「Particle」クラスの定義ね!親は「RectangleMorph」にするです。
移動量のインスタンス変数「addX」、「addY」も定義します。
--------------------------------------------
RectangleMorph subclass: #Particle
instanceVariableNames: 'addX addY '
classVariableNames: ''
poolDictionaries: ''
category: 'ParticleTest'
--------------------------------------------
アクセプト。
Alt + s ( Cmd + s )
■メソッドを追加します。
● initializeメソッド
「initialize」メソッドを追加します。
ボールのサイズ 10@10
ボールの色 ラベンダー色
枠の線は無し
移動量の初期値
x方向 3
y方向 3
今回はソースコードにコメントを入れたです。
--------------------------------------------
initialize
"ボールの見た目を設定"
super initialize.
self extent: 10@10.
self color: (Color r: 0.7 g: 0.7 b: 0.8).
self borderWidth: 0.
"移動量の初期設定"
addX _ 3.
addY _ 3.
--------------------------------------------
アクセプト。
Alt + s ( Cmd + s )
● bouncingメソッド
ボールの移動メソッドです。
現在の自分の位置を調べて、これに各移動量を加えます。
自分の上下左右を調べて、もし自分がオーナーの外にはみ出ていたならその方向の移動量を
「negated」します。<「+」なら「−」に、「−」なら「+」にってことね。
--------------------------------------------
bouncing: aBounds
| pos px py |
"位置を計算"
pos _ self position.
px _ pos x + addX.
py _ pos y + addY.
"外に出ないようにする"
px > (aBounds right - self width) ifTrue: [
px _ aBounds right - self width.
addX _ addX negated].
px aBounds left ifTrue: [
px _ aBounds left.
addX _ addX negated].
py > (aBounds bottom - self height) ifTrue: [
py _ aBounds bottom - self height.
addY _ addY negated].
py aBounds top ifTrue: [
py _ aBounds top.
addY _ addY negated].
"位置を変える"
self position: px @ py.
--------------------------------------------
アクセプト。
Alt + s ( Cmd + s )
試しにインスタンスを出してみる
--------------------------------------------
Paticle new openInWorld
--------------------------------------------
Alt + d ( Cmd + d )
もちろんまだ動かないよ〜ん。ゴミ箱に捨てちゃってください。
▲PREV ------ ● TOP ● ------ NEXT▼
■ MEMO ■
Links to this Page
- FunFunFunMorphic last edited on 25 March 2004 at 5:25:44 am by tk0005-202x210x196x109.ap-TK.usen.ad.jp
- funfunfun37 last edited on 18 June 2007 at 5:27:42 am by localhost.localdomain
- funfunfun39 last edited on 18 June 2007 at 5:25:01 am by localhost.localdomain