Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

User name input after reaching highscore
Started by michaelthomasdanaher Apr 08 2019 08:19 AM

- - - - -
No replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

michaelthomasdanaher

[GLOBAL: userInfoPane.html]
michaelthomasdanaher
  • Observer

  • 1 posts
  • Corona SDK

I'm having trouble working out how to use the userput features. In the game there is a highscore menu where I want the user to be able to type in 3 characters next to their highscore which represents their name.

This is the code I have so far which stores 10 highscores and sorts them highest to lowest.

local json = require("json")

local scoresTable ={}

local filePath= system.pathForFile("scores.json", system.DocumentsDirectory)

local function loadScores()

	local file = io.open(filePath, "r")

	if file then
		local contents = file:read("*a")
		io.close(file)
		scoresTable = json.decode(contents)
	end
	if(scoresTable == nil or #scoresTable == 0) then
		scoresTable = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
	end
end

local function saveScores()

	for i = #scoresTable, 11, -1 do
		table.remove(scoresTable, i)
	end
	local file = io.open(filePath, "w")

	if file then
		file:write(json.encode(scoresTable))
		io.close(file)
	end
end

local function gotoMenu()
	composer.gotoScene("menu", {time = 800, effect = "crossFade"})
end


-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------

-- create()
function scene:create( event )

	local sceneGroup = self.view
	-- Code here runs when the scene is first created but has not yet appeared on screen

	-- Load the previous scores
	loadScores()

	-- Insert the saved score from the last game into the table, then reset to 0
	table.insert(scoresTable, composer.getVariable("finalScore"))
	composer.setVariable("finalScore", 0)

	-- Sort the table entries from highest to lowest
	local function compare(a,b)
		return a > b
	end
	table.sort(scoresTable, compare)

	-- Save the scores
	saveScores()



[topic_controls]
[/topic_controls]