【Macでゲームを作ろう】ブロック崩しを作っていく第7回 [AppleScript辞書はつくれるか?]
property NSCursor : class "NSCursor"
NSCursor's hide()
script AppDelegate
property parent : class "NSObject"
property NSTimer : class "NSTimer"
global TimerObj
global flg
property NSEvent : current application's class "NSEvent"
property view1 : missing value
property image1 : missing value
property image2 : missing value
property textLabel1 : missing value
property Button1 : missing value
global viewX,viewY, tamaX,tamaY, moveX,moveY
global tamaW,tamaH, tamaSizeX,tamaSizeY
global flg
global padX,padY, padW, padH, padSizeX,padSizeY
global mouse0
global tamaReset
property NSCursor : class "NSCursor"
on action1_(sender)
if flg is false then
(* NSViewの範囲 *)
set origin1 to view1's frame()'s |size|
set {viewX,viewY} to {origin1's width, origin1's height}
(* tamaの情報をリセット *)
set {moveX,moveY} to {5,5}
set {tamaX,tamaY} to tamaReset
set BollPosition to {x:tamaX, y:tamaY}
image1's setFrameOrigin_(BollPosition)
(* Padの情報取得 *)
set {padSizeX, padSizeY} to {100, 24}
set {padSizeX, padSizeY} to {(padSizeX / 2), (padSizeY / 2)}
set origin3 to image2's frame()'s origin
set {padX,padY} to {origin3's x, origin3's y}
set size3 to image2's frame()'s |size|
set {padW, padH} to {size3's width, size3's height}
set {padW, padH} to {(padW / 2), (padH / 2)}
(* 現在のマウスの位置を取得 *)
set {mouseX,mouseY} to my mousePosition()
set mouse0 to mouseX
my timerStart()
set flg to true
button1's setAlphaValue_(0.0)
textLabel1's setStringValue_("")
NSCursor's hide()
end if
end action1_
on timerStart()
try
set TimerObj to NSTimer's scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(0.01, me, "mainLoop", missing value, true)
on error
set TimerObj to missing value
end try
end timerStart
on mainLoop()
my moveBall()
my movePad()
end mainLoop
on abs(n)
return (n ^ 2) ^ 0.5
end abs
on moveBall()
set {tamaX,tamaY} to {tamaX + moveX, tamaY + moveY}
set {Xposition1, Yposition1} to {(tamaX + tamaW - tamaSizeX), (tamaY + tamaH - tamaSizeY)}
set {Xposition2, Yposition2} to {(tamaX + tamaW + tamaSizeX), (tamaY + tamaH + tamaSizeY)}
if (Xposition1 < 0) or (Xposition2 > viewX) then set moveX to 0 - moveX
if Yposition2 > viewY then set moveY to 0 - moveY
if Yposition1 < 0 then
tell TimerObj to invalidate()
textLabel1's setStringValue_("Game Over !")
set flg to false
button1's setAlphaValue_(1.0)
NSCursor's unhide()
end if
set decisionX to my abs((padX + padSizeX) - (tamaX + tamaW))
set decisionY to my abs((padY + padSizeY) - (tamaY + tamaH - tamaSizeY))
if (decisionX < padSizeX) and (decisionY < padSizeY) then set moveY to my abs(moveY)
set BollPosition to {x:tamaX, y:tamaY}
image1's setFrameOrigin_(BollPosition)
end moveBall
on movePad()
set {mouseX,mouseY} to my mousePosition()
set padX to padX + (mouseX - mouse0)
set padLeft to padX + padW - padSizeX
set padRight to padX + padW + padSizeX
if padLeft < 0 then set padX to padSizeX - padW
if padRight > viewX then set padX to viewX - padW - padSizeX
set mouse0 to mouseX
set padPosition to {x:padX, y:padY}
image2's setFrameOrigin_(padPosition)
end movePad
on mousePosition()
set mouseLocate to NSEvent's mouseLocation()
set mouseX to (mouseLocate's x) as integer
set mouseY to (mouseLocate's y) as integer
return {mouseX,mouseY}
end mousePosition
-- IBOutlets
property theWindow : missing value
on awakeFromNib()
textLabel1's setStringValue_("")
end awakeFromNib
on applicationWillFinishLaunching_(aNotification)
set flg to false
(* tamaの情報取得 *)
set {tamaSizeX, tamaSizeY} to {16, 16}
set {tamaSizeX, tamaSizeY} to {(tamaSizeX / 2), (tamaSizeY / 2)}
set origin2 to image1's frame()'s origin
set {tamaX,tamaY} to {origin2's x, origin2's y}
set size2 to image1's frame()'s |size|
set {tamaW, tamaH} to {size2's width, size2's height}
set {tamaW, tamaH} to {(tamaW / 2), (tamaH / 2)}
(* tamaの位置の初期値を保存 *)
set tamaReset to {tamaX,tamaY}
end applicationWillFinishLaunching_
on applicationShouldTerminateAfterLastWindowClosed_(sender)
return true
end applicationShouldTerminateAfterLastWindowClosed_
on applicationShouldTerminate_(sender)
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
コメント 0