반응형
오늘은 화면에 점수 표시하는 샘플코드를 공부할 겁니다.
이 파일이 전체 소스 파일입니다.
웹 싸이트는 http://techority.com/2011/01/26/how-to-add-a-score-to-your-iphone-app/ 입니다.
이 싸이트로 가면 유튜브 동영상도 있습니다.
이 소스를 보면서 제 식으로 공부 해 보겠습니다.
이 소스를 실행하면 위 화면처럼 나옵니다.
화면을 Tapping 하면 1 포인트씩 숫자가 올라갑니다.
우선 main.lua부터 보겠습니다.
--[[
Related files are score.lua, 1 through 9.png, scorebg.png and space.png
--]]
display.setStatusBar( display.HiddenStatusBar )
local background = display.newImage ("background.png")
score = require ("score")
local border = 5
local scoreInfo = score.getInfo()
score.init({
x = 40,
y = 5}
)
score.setScore(0)
local function addtoit (event)
if event.phase == "ended" then
score.setScore (score.getScore()+1)
end
end
background:addEventListener("touch", addtoit)
처음에 아이폰의 StatusBar를 없앱니다. (원래 소스에는 없는데 그냥 제가 추가 했습니다. 이게 보기에 더 편하더라구요.)
두번째는 백그라운드 이미지를 display 합니다.
세번째는 score.lua 소스를 require (implement) 합니다.
그 다음은 border를 5로 설정합니다.(이 border는 뭐 하는건지 모르겠습니다. 그냥 쓸데없이 있는거 같네요.)
그 다음엔 아까 require했던 score.lua에 있는 getInfo() 함수를 scoreInfo 변수에 담습니다. 이 getInfo() 함수는 이따 score.lua 파일을 살펴 볼 때 어떤 기능인지 공부하겠습니다.
그 다음은 score.lua에 있는 init() 함수에 x,y 파라미터를 넣어서 실행시킵니다.
그리고 setScore()함수에 파라미터 값을 0으로 해서 보냅니다.
로컬 함수 addtoit(event) 함수를 만듭니다.
그 내용은 이벤트가 끝나면 score.lua의 setScore함수에 이전 점수 +1 을 한 파라미터 값을 넣어서 부릅니다.
맨 마지막으로 background 에 touch 리스너를 달구요. 이 이벤트가 일어났을 경우 addtoit() 함수를 실행시킵니다.
지금까지 main.lua의 코드를 분석해 봤습니다.
이제 score.lua 파일을 분석해 보겠습니다.
-- A sample score keeping display
-- This updates a display for a numeric score
-- Example usage:
-- Place the score at 50,50
-- score.init( { x = 50, y = 50 } )
-- Update the score to current value + 10:
-- score.setScore( score.getScore() + 10 )
module(..., package.seeall)
-- Init images. This creates a map of characters to the names of their corresponding images.
local numbers = {
[string.byte("0")] = "0.png",
[string.byte("1")] = "1.png",
[string.byte("2")] = "2.png",
[string.byte("3")] = "3.png",
[string.byte("4")] = "4.png",
[string.byte("5")] = "5.png",
[string.byte("6")] = "6.png",
[string.byte("7")] = "7.png",
[string.byte("8")] = "8.png",
[string.byte("9")] = "9.png",
[string.byte(" ")] = "space.png",
}
-- score components
local theScoreGroup = display.newGroup()
local theBackground = display.newImage( "scorebg.png" )
local theBackgroundBorder = 10
theScoreGroup:insert( theBackground )
local numbersGroup = display.newGroup()
theScoreGroup:insert( numbersGroup )
-- the current score
local theScore = 0
-- the location of the score image
-- initialize the score
-- params.x <= X location of the score
-- params.y <= Y location of the score
function init( params )
theScoreGroup.x = params.x
theScoreGroup.y = params.y
setScore( 0 )
end
-- retrieve score panel info
-- result.x <= current panel x
-- result.y <= current panel y
-- result.xmax <= current panel x max
-- result.ymax <= current panel y max
-- result.contentWidth <= panel width
-- result.contentHeight <= panel height
-- result.score <= current score
function getInfo()
return {
x = theScoreGroup.x,
y = theScoreGroup.y,
xmax = theScoreGroup.x + theScoreGroup.contentWidth,
ymax = theScoreGroup.y + theScoreGroup.contentHeight,
contentWidth = theScoreGroup.contentWidth,
contentHeight = theScoreGroup.contentHeight,
score = theScore
}
end
-- update display of the current score.
-- this is called by setScore, so normally this should not be called
function update()
-- remove old numerals
theScoreGroup:remove(2)
local numbersGroup = display.newGroup()
theScoreGroup:insert( numbersGroup )
-- go through the score, right to left
local scoreStr = tostring( theScore )
local scoreLen = string.len( scoreStr )
local i = scoreLen
-- starting location is on the right. notice the digits will be centered on the background
local x = theScoreGroup.contentWidth - theBackgroundBorder
local y = theScoreGroup.contentHeight / 2
while i > 0 do
-- fetch the digit
local c = string.byte( scoreStr, i )
local digitPath = numbers[c]
local characterImage = display.newImage( digitPath )
-- put it in the score group
numbersGroup:insert( characterImage )
-- place the digit
characterImage.x = x - characterImage.width / 2
characterImage.y = y
x = x - characterImage.width
--
i = i - 1
end
end
-- get current score
function getScore()
return theScore
end
-- set score to value
-- score <= score value
function setScore( score )
theScore = score
update()
end
score.lua 파일을 보겠습니다.
score.lua 파일 처음에 module(..., package.seeall) 를 넣습니다. 이러면 이 파일은 모듈로 사용될 수 있습니다.
이것을 했기 때문에 main.lua에서 require("score") 를 할 수 있고 이것을 변수 score에 담아서 score.(score.lua에 있는 함수) 방법으로 score.lua 내의 함수를 main.lua에서 사용할 수 있습니다.
그 다음 numbers 라는 테이블 변수에 0~9 의 이미지 파일을 담습니다.
그 다음은 display.newGroup()으로 객체들을 담을 그룹을 만듭니다.
그리고 score를 표시할 백그라운드 이미지를 넣고 theBackgroundBorder 변수에 10을 담습니다.
아까 만들었던 theScoreGroup에 백그라운드 이미지(theBackground)를 insert 합니다.
보시면 아까 theScoreGroup 말고 또다른 Group인 numberGroup을 만듭니다.
그리고 이 numberGrooup 자체를 처음의 theScoreGroup에 insert 합니다.
다음 줄에서 theScore 변수를 0으로 선언합니다. 처음 0점부터 시작하게 됩니다.
이제 함수가 나오네요. 아까 main.lua에서 불렀던 init(params) 함수가 나옵니다.
여기서는 theScoreGroup.x와 y를 전달받은 파라미터 값으로 넣고 setScore()함수에 0을 던져 줍니다.
아까 main에서 x=40, y=5 으로 보냈기 때문에 그 위치에 theScoreGroup의 객체들이 display 될 겁니다.
다음은 getInfo() 함수입니다. main함수에서는 이 함수에서 세팅된 값을 scoreInfo 변수에 담았었습니다.
getInfo() 함수에서는 7개의 값을 return합니다.
Lua (Corona) 에서는 복수개의 값을 return 할 수 있습니다.
theScoreGrooup,x , y 값과 ymax,ymax, contentWidth,contentHeight, score 등의 값을 return합니다.
다음은 update() 함수입니다.
처음엔 이전 score를 없앱니다.
그 다음은 numbersGroup이라는 그룹을 만들고 이를 통째로 theScoreGroup에 넣습니다.
다음줄은 theScore를 String 값으로 변환해서 scoreStr 변수에 담습니다.
Score가 한자리 수 인지 두자리 수인지 세자리 수인지 에 대한 정보를 담는 변수도 있네요.
scoreLen변수에 scoreStr 의 length를 담습니다.
local x, y는 점수가 표시되는 위치 입니다. 한자리 수일때 두자리 수일때 표시되는 위치가 조금씩 다르겠죠?
그 다음 while 문에서 어떤 점수 이미지를 어디에 뿌려줄지 계산하는 로직이 있습니다.
이미지를 출력하고 그 이미지를 numbersGroup에 담습니다.
그리고 나서 이미지의 x,y 좌표를 설정하구요.
이 while 문은 scoreLen 만큼 돌아갈 겁니다.
다음 함수는 getScore() 함수인데 여기서는 theScore를 리턴해 줍니다.
그리고 setScore() 함수에서는 theScore 함수에 score 값을 넣어 준 후 update() 함수를 실행시킵니다.
여기 까지 하면 scoring 샘플이 모두 완성된거구요.
이걸 실행 한 후 background를 누르면 점수가 1씩 증가하게 될 겁니다.
이 소스를 가지고 있으면 나중에 게임 개발할 때 아주 유용하게 사용할 수 있을 겁니다.
그럼.........
이 파일이 전체 소스 파일입니다.
웹 싸이트는 http://techority.com/2011/01/26/how-to-add-a-score-to-your-iphone-app/ 입니다.
이 싸이트로 가면 유튜브 동영상도 있습니다.
이 소스를 보면서 제 식으로 공부 해 보겠습니다.
이 소스를 실행하면 위 화면처럼 나옵니다.
화면을 Tapping 하면 1 포인트씩 숫자가 올라갑니다.
우선 main.lua부터 보겠습니다.
--[[
Related files are score.lua, 1 through 9.png, scorebg.png and space.png
--]]
display.setStatusBar( display.HiddenStatusBar )
local background = display.newImage ("background.png")
score = require ("score")
local border = 5
local scoreInfo = score.getInfo()
score.init({
x = 40,
y = 5}
)
score.setScore(0)
local function addtoit (event)
if event.phase == "ended" then
score.setScore (score.getScore()+1)
end
end
background:addEventListener("touch", addtoit)
처음에 아이폰의 StatusBar를 없앱니다. (원래 소스에는 없는데 그냥 제가 추가 했습니다. 이게 보기에 더 편하더라구요.)
두번째는 백그라운드 이미지를 display 합니다.
세번째는 score.lua 소스를 require (implement) 합니다.
그 다음은 border를 5로 설정합니다.(이 border는 뭐 하는건지 모르겠습니다. 그냥 쓸데없이 있는거 같네요.)
그 다음엔 아까 require했던 score.lua에 있는 getInfo() 함수를 scoreInfo 변수에 담습니다. 이 getInfo() 함수는 이따 score.lua 파일을 살펴 볼 때 어떤 기능인지 공부하겠습니다.
그 다음은 score.lua에 있는 init() 함수에 x,y 파라미터를 넣어서 실행시킵니다.
그리고 setScore()함수에 파라미터 값을 0으로 해서 보냅니다.
로컬 함수 addtoit(event) 함수를 만듭니다.
그 내용은 이벤트가 끝나면 score.lua의 setScore함수에 이전 점수 +1 을 한 파라미터 값을 넣어서 부릅니다.
맨 마지막으로 background 에 touch 리스너를 달구요. 이 이벤트가 일어났을 경우 addtoit() 함수를 실행시킵니다.
지금까지 main.lua의 코드를 분석해 봤습니다.
이제 score.lua 파일을 분석해 보겠습니다.
-- A sample score keeping display
-- This updates a display for a numeric score
-- Example usage:
-- Place the score at 50,50
-- score.init( { x = 50, y = 50 } )
-- Update the score to current value + 10:
-- score.setScore( score.getScore() + 10 )
module(..., package.seeall)
-- Init images. This creates a map of characters to the names of their corresponding images.
local numbers = {
[string.byte("0")] = "0.png",
[string.byte("1")] = "1.png",
[string.byte("2")] = "2.png",
[string.byte("3")] = "3.png",
[string.byte("4")] = "4.png",
[string.byte("5")] = "5.png",
[string.byte("6")] = "6.png",
[string.byte("7")] = "7.png",
[string.byte("8")] = "8.png",
[string.byte("9")] = "9.png",
[string.byte(" ")] = "space.png",
}
-- score components
local theScoreGroup = display.newGroup()
local theBackground = display.newImage( "scorebg.png" )
local theBackgroundBorder = 10
theScoreGroup:insert( theBackground )
local numbersGroup = display.newGroup()
theScoreGroup:insert( numbersGroup )
-- the current score
local theScore = 0
-- the location of the score image
-- initialize the score
-- params.x <= X location of the score
-- params.y <= Y location of the score
function init( params )
theScoreGroup.x = params.x
theScoreGroup.y = params.y
setScore( 0 )
end
-- retrieve score panel info
-- result.x <= current panel x
-- result.y <= current panel y
-- result.xmax <= current panel x max
-- result.ymax <= current panel y max
-- result.contentWidth <= panel width
-- result.contentHeight <= panel height
-- result.score <= current score
function getInfo()
return {
x = theScoreGroup.x,
y = theScoreGroup.y,
xmax = theScoreGroup.x + theScoreGroup.contentWidth,
ymax = theScoreGroup.y + theScoreGroup.contentHeight,
contentWidth = theScoreGroup.contentWidth,
contentHeight = theScoreGroup.contentHeight,
score = theScore
}
end
-- update display of the current score.
-- this is called by setScore, so normally this should not be called
function update()
-- remove old numerals
theScoreGroup:remove(2)
local numbersGroup = display.newGroup()
theScoreGroup:insert( numbersGroup )
-- go through the score, right to left
local scoreStr = tostring( theScore )
local scoreLen = string.len( scoreStr )
local i = scoreLen
-- starting location is on the right. notice the digits will be centered on the background
local x = theScoreGroup.contentWidth - theBackgroundBorder
local y = theScoreGroup.contentHeight / 2
while i > 0 do
-- fetch the digit
local c = string.byte( scoreStr, i )
local digitPath = numbers[c]
local characterImage = display.newImage( digitPath )
-- put it in the score group
numbersGroup:insert( characterImage )
-- place the digit
characterImage.x = x - characterImage.width / 2
characterImage.y = y
x = x - characterImage.width
--
i = i - 1
end
end
-- get current score
function getScore()
return theScore
end
-- set score to value
-- score <= score value
function setScore( score )
theScore = score
update()
end
score.lua 파일을 보겠습니다.
score.lua 파일 처음에 module(..., package.seeall) 를 넣습니다. 이러면 이 파일은 모듈로 사용될 수 있습니다.
이것을 했기 때문에 main.lua에서 require("score") 를 할 수 있고 이것을 변수 score에 담아서 score.(score.lua에 있는 함수) 방법으로 score.lua 내의 함수를 main.lua에서 사용할 수 있습니다.
그 다음 numbers 라는 테이블 변수에 0~9 의 이미지 파일을 담습니다.
그 다음은 display.newGroup()으로 객체들을 담을 그룹을 만듭니다.
그리고 score를 표시할 백그라운드 이미지를 넣고 theBackgroundBorder 변수에 10을 담습니다.
아까 만들었던 theScoreGroup에 백그라운드 이미지(theBackground)를 insert 합니다.
보시면 아까 theScoreGroup 말고 또다른 Group인 numberGroup을 만듭니다.
그리고 이 numberGrooup 자체를 처음의 theScoreGroup에 insert 합니다.
다음 줄에서 theScore 변수를 0으로 선언합니다. 처음 0점부터 시작하게 됩니다.
이제 함수가 나오네요. 아까 main.lua에서 불렀던 init(params) 함수가 나옵니다.
여기서는 theScoreGroup.x와 y를 전달받은 파라미터 값으로 넣고 setScore()함수에 0을 던져 줍니다.
아까 main에서 x=40, y=5 으로 보냈기 때문에 그 위치에 theScoreGroup의 객체들이 display 될 겁니다.
다음은 getInfo() 함수입니다. main함수에서는 이 함수에서 세팅된 값을 scoreInfo 변수에 담았었습니다.
getInfo() 함수에서는 7개의 값을 return합니다.
Lua (Corona) 에서는 복수개의 값을 return 할 수 있습니다.
theScoreGrooup,x , y 값과 ymax,ymax, contentWidth,contentHeight, score 등의 값을 return합니다.
다음은 update() 함수입니다.
처음엔 이전 score를 없앱니다.
그 다음은 numbersGroup이라는 그룹을 만들고 이를 통째로 theScoreGroup에 넣습니다.
다음줄은 theScore를 String 값으로 변환해서 scoreStr 변수에 담습니다.
Score가 한자리 수 인지 두자리 수인지 세자리 수인지 에 대한 정보를 담는 변수도 있네요.
scoreLen변수에 scoreStr 의 length를 담습니다.
local x, y는 점수가 표시되는 위치 입니다. 한자리 수일때 두자리 수일때 표시되는 위치가 조금씩 다르겠죠?
그 다음 while 문에서 어떤 점수 이미지를 어디에 뿌려줄지 계산하는 로직이 있습니다.
이미지를 출력하고 그 이미지를 numbersGroup에 담습니다.
그리고 나서 이미지의 x,y 좌표를 설정하구요.
이 while 문은 scoreLen 만큼 돌아갈 겁니다.
다음 함수는 getScore() 함수인데 여기서는 theScore를 리턴해 줍니다.
그리고 setScore() 함수에서는 theScore 함수에 score 값을 넣어 준 후 update() 함수를 실행시킵니다.
여기 까지 하면 scoring 샘플이 모두 완성된거구요.
이걸 실행 한 후 background를 누르면 점수가 1씩 증가하게 될 겁니다.
이 소스를 가지고 있으면 나중에 게임 개발할 때 아주 유용하게 사용할 수 있을 겁니다.
그럼.........
반응형
'Corona SDK > Corona SDK TIPs' 카테고리의 다른 글
Sprite Sheets 이용해서 애니메이션 구현하기 (0) | 2011.10.28 |
---|---|
Time Bar 만들기 (0) | 2011.10.27 |
백 그라운드 이미지 scroll down 시키기 (0) | 2011.10.26 |
코로나에서 Table View (List View) 표현하기 두번째 시간 (15) | 2011.10.14 |
코로나에서 Table View (List View) 표현하기... (0) | 2011.10.14 |
함수 (function) 공부 (1) | 2011.10.03 |
Corona SDK 앱에 광고 달기 (2) | 2011.09.23 |
앱스토어에 업로드 하기 (0) | 2011.09.20 |
빌드하기 (아이폰) - 2 (0) | 2011.09.20 |
빌드하기 (아이폰) -1 (0) | 2011.09.20 |