SSブログ

【Macでゲームを作ろう】ブロック崩しを作っていく第1回 [AppleScript辞書はつくれるか?]

年末年始企画(?) ゲームを作ろう企画第1弾です。
ですが、作り方やコードの書き方をメインにゆっくりやっていきますので、進行の遅さにイライラするかもしれませんがご了承ください。

 

まずは、オブジェクトを動かすためのコントロールの仕方からです。

今回使用する新しいコードは、


|Object|’s frame()'s origin

|Object|’s setFrameOrigin_({x:x, y:y})


になります。

 


まずは、Xcodeを立ち上げて『Cocoa-applescript』の新規プロジェクトを作成しましょう


ss6-0.png


 


そして、ボタンを配置します。


ss6-1.png


 


ボタンをクリックした時に対応するアクション| action1_(sender) |と

ボタンオブジェクトを自身を操作する| button1 |プロパリティのコードを入力します


    property button1 : missing value

    on action1_(sender)

        (* この中は後で入力します *)

    end action1_


 


ss6-2.jpg


 


それぞれ 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})

で数値を再設定しています。

 

では実行してみましょう。

ss6-3.pngss6-4.png


 


いかがでしょう、

ボタンをクリックする度にボタンが右に移動していますよね。


今後これを利用して作っていきます。


 


 


--ソースコード全体は下記から

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

 

 


nice!(1)  コメント(0)  トラックバック(0) 
共通テーマ:パソコン・インターネット

nice! 1

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

トラックバック 0

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。