Hi folks,
here is the gideros verson of my high-score list manager. You need JSON for it. I got my version from here:
http://www.chipmunkav.com/downloads/otherdownloads.htm
Edit: Updated to version 1.11 which fixes a bug regarding scores with a value of zero.
-- score_gideros.lua
-- Author Michael Hartlef
-- Version 1.11
-- License: MIT
cScoreList = gideros.class(EventDispatcher)
--****************************************************************
function cScoreList:init(entryCount)
--****************************************************************
self.entrycount = entryCount
self.list = {}
local i = 0
for i = entryCount,1,-1 do
local ts = {}
ts.name = "---"
ts.points = 0
table.insert(self.list, ts)
end
end
--****************************************************************
function cScoreList:load(fileName)
--****************************************************************
local hsFile = io.open( fileName, "r" )
if hsFile then
local hsContent = hsFile:read( "*a" )
self.list = Json.Decode(hsContent)
self.entrycount = #self.list
io.close( hsFile )
end
end
--****************************************************************
function cScoreList:save(fileName)
--****************************************************************
local hsFile = io.open( fileName, "w" )
if hsFile then
local hsContent = Json.Encode(self.list)
hsFile:write( hsContent )
io.close( hsFile )
end
end
--****************************************************************
function cScoreList:checkEntry(scorepoints)
--****************************************************************
local i = 0
if scorepoints == 0 then return (self.entrycount+1) end
for i = 1,#self.list,1 do
local ts = self.list[i]
if ts.points < scorepoints then
return i
end
end
return 0
end
--****************************************************************
function cScoreList:storeEntry(index, scorepoints, name)
--****************************************************************
if scorepoints == 0 or index< 1 or index > self.entrycount then return end
local ts = {}
ts.name = name
ts.points = scorepoints
table.insert(self.list, index, ts)
table.remove(self.list,(self.entrycount+1))
end
--****************************************************************
function cScoreList:count()
--****************************************************************
return self.entrycount
end
--****************************************************************
function cScoreList:getName(i)
--****************************************************************
local ts = self.list[i]
return ts.name
end
--****************************************************************
function cScoreList:getPoints(i)
--****************************************************************
local ts = self.list[i]
return ts.points
end |
Here is a code sample where I check the score against the list and then then maybe store it:
HighScore = cScoreList.new(10)
--......
local scoreIndex = HighScore:checkEntry(gameScore)
if scoreIndex <= HighScore:count() then
HighScore:storeEntry(scoreIndex, gameScore, "NameOfPlayer")
end
HighScore:save("scoreList.txt") |
Comments
small note: inheriting from EventDispatcher instead of Sprite is a little bit more efficient.
I changed it to your suggestion.
Will it be available in the gideros community libraries? http://www.giderosmobile.com/forum/discussion/1436/gideros-open-source-community-libraries
Many new codes from community everyday, i hope those dont get lost in sea of forum threads
http://www.nightspade.com
Just pm me with github link to add it
I created cScoreList.lua and use the score checking as above but my project does not create scoreList.txt
And please help me to load top 10 score from scoreList.txt. I saw there's dataSaver.loadValue("scores") but it seem not applicable is this case
Any help would be very appreciated
Ultimate Games on Appstore
Ultimate Games on Google Play
HighScore:save("|D|scoreList.txt")
---------------------------------------