【Macでゲームを作ろう】ブロック崩しを作っていく第1回 [AppleScript辞書はつくれるか?]
まずは、オブジェクトを動かすためのコントロールの仕方からです。
今回使用する新しいコードは、
|Object|’s frame()'s origin
|Object|’s setFrameOrigin_({x:x, y:y})
になります。
まずは、Xcodeを立ち上げて『Cocoa-applescript』の新規プロジェクトを作成しましょう
そして、ボタンを配置します。
ボタンをクリックした時に対応するアクション| action1_(sender) |と
ボタンオブジェクトを自身を操作する| button1 |プロパリティのコードを入力します
property button1 : missing value
on action1_(sender)
(* この中は後で入力します *)
end action1_
それぞれ Delegate と接続します。
今回はボタンをクリックしたら10ずつ右に移動するコードを入力します。
on action1_(sender)
set origin1 to button1's frame()'s origin
set {x, y} to {(origin1's x), (origin1's y)}
set x to x + 10
button1's setFrameOrigin_({x:x, y:y})
end action1_
先ほど接続したボタンがクリックされたアクションが発生した場合に| action1_ |ハンドラが実行されます。
set origin1 to button1's frame()'s origin
ボタンのフレームの情報を取得し、orginで配置したViewの原点を基本に、ボタンの減点位置の座標をを取得しています。取得したデータは
{x: |横方向の位置|, y: |縦方向の位置|}
レコードクラスで受け取れます。
set {x, y} to {(origin1's x), (origin1's y)}
set x to x + 10
でそれぞれの数値を取得し、xだけ10ふやし、
setFrameOrigin_({x:x, y:y})
で数値を再設定しています。
では実行してみましょう。
いかがでしょう、
ボタンをクリックする度にボタンが右に移動していますよね。
今後これを利用して作っていきます。
script AppDelegate
property parent : class "NSObject"
property button1 : missing value
on action1_(sender)
set origin1 to button1's frame()'s origin
set {x, y} to {(origin1's x), (origin1's y)}
set x to x + 10
button1's setFrameOrigin_({x:x, y:y})
end action1_
-- IBOutlets
property theWindow : missing value
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
end applicationWillFinishLaunching_
on applicationShouldTerminateAfterLastWindowClosed_(sender)
return true
end applicationShouldTerminateAfterLastWindowClosed_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
コメント 0