Showing stars in front a level

Hi again ok I have a question. how would I show stars in front of the menu level select screen meaning in each level you get three stars, depending on how much stars you get example

1 star = 1 star collected
2 star = 3 stars collected
3 star = 7 stars collected

That I mean about stars displaying in front the level select and also how would I save it when they quit the game, so when they come back the stars are there again.

Help is appreciated [import]uid: 17058 topic_id: 19952 reply_id: 319952[/import]

Sorry for being impatient but I would really like some help [import]uid: 17058 topic_id: 19952 reply_id: 77680[/import]

You need to use Ice. Check it out in share your code, heres an example that will help you. All you need to do is copy paste it.

Link to ice: http://developer.anscamobile.com/code/ice
*Copy paste code for Ice, make sure you have ice in you folder before copy pasting this.
Also make sure you use director class and have each piece of code in the right section. Hope this helps!*

[code]
– main.lua
function changeScene (e)
if(e.phase == “ended”) then
director:changeScene(e.target.scene)
end
end

local director = require(“director”);
local mainGroup = display.newGroup();

mainGroup:insert(director.directorView);
director:changeScene(“ice2”);


–Ice2.lua:

module(…, package.seeall)

function new()

local ice2Group = display.newGroup();

require( “ice” )

local scores = ice:loadBox( “scores” )

local high = scores:retrieve(“best”)
print(high)

local gameOver = false

local physics = require(“physics”)
physics.start()
physics.setGravity(0,5)
local ballGroup = display.newGroup()
local score = 0

local score_txt =display.newText("Score: ",0,0,nil,30)
score_txt.x = 100; score_txt.y = 50
score_txt:setTextColor(255,0,0)
score_txt.text = "Score: "… score
point = 0
local scoreText = display.newText("Points: ",0,0,nil,30)
scoreText.x = 100
scoreText.y = 100
scoreText:setTextColor(255,0,0)
scoreText.text = “Points:”… point

local function spawn_ball()
if gameOver == false then
local ball = display.newCircle(ballGroup,0,0,30)
ball.x = math.random(50,300)
ball.y = -50
physics.addBody(ball, {radius = 30})
ball.name = “ball”
end
end

timer.performWithDelay(500, spawn_ball, 0)

local basket = display.newRect(0,0,50,70)
basket.x = display.contentWidth/2
basket.y = display.contentHeight - 80
physics.addBody(basket, “static”)

local function drag(event)
if event.phase == “began” then
event.target.isFocus = true
elseif event.phase == “moved” and event.target.isFocus then
event.target.x = event.x
event.target.y = event.y
end
return true
end

basket:addEventListener(“touch”, drag)

local function show_highScore()
gameOver = not gameOver

scores:storeIfHigher( “best”, score )
scores:save()
local highText = display.newText("", 0,0,nil,30)
highText.x = display.contentWidth/2
highText.y = display.contentHeight/2
if score > high then
highText.text = "HighScore: "… scores:retrieve(“best”)
scoreText.text = "HighScore: "… scores:retrieve(“best”)
else
highText.text = “NO NEW HIGH SCORE!”
end
end

local function show_text()
local text = display.newText("+1", 0,0, nil, 30)

score = score + 1

if score > scores:retrieve(“best”) then
print(“new high score”)
end

score_txt.text = "Score: "… score
text.alpha = 1
text.x = basket.x
text.y = basket.y - 100
transition.to(text, {time=1000,y = text.y - 50, alpha = 1, onComplete = function() display.remove(text) end})
end

local function onCollision(event)
if event.phase == “began” and event.other.name == “ball” then
display.remove(event.other)
show_text()
end
end

basket:addEventListener(“collision”, onCollision)

timer.performWithDelay(50000, show_highScore, 1)

Tap = display.newText( “Tap”, 20, 200, “Helvetica”, 20 )
Tap.x = 30
Tap.y = 450

function ther5(event)
director:changeScene( “Ice3”, “fade” )
end
Tap:addEventListener(“tap”, ther5);

return ice2Group;

end


– Ice3.lua

module(…, package.seeall)

function new()

local ice2Group = display.newGroup();
– main.lua
require(“ice”)
myData = nil

– load previously saved data
– EDIT: if no data has been saved, it will create the myData.ice where data will be saved
local scores = ice:loadBox( “scores” )

local score = scores:retrieve( “best” )
local scoreText = display.newText( "Score: "…score, 20, 0,nil, 20)
scoreText:setTextColor(255,255,255)

local function show_highScore()
local highText = display.newText("", 0,0,nil,30)
highText.x = display.contentWidth/2
highText.y = display.contentHeight/2
if score > 40 then
highText.text = "HighScore: "… scores:retrieve(“best”)
else
highText.text = “NO NEW HIGH SCORE!”
end
end

timer.performWithDelay(2000, show_highScore)

Tap = display.newText( “Tap”, 20, 170, “Helvetica”, 30 )
Tap.x = 100
Tap.y = 400

function ther5(event)
director:changeScene( “ice2”, “fade” )
end
Tap:addEventListener(“tap”, ther5);

return ice2Group;

end
[import]uid: 23689 topic_id: 19952 reply_id: 77686[/import]

@micheal Assadi yeah you got expect there something missing. Ok you got the example of when score < 40. can I use that instead to display the numbers of stars in front of the level select. Also how would I add this the overall score do this

score = score1 + score2 + score3 + .....

I know there more to that what would it be [import]uid: 17058 topic_id: 19952 reply_id: 77693[/import]

I’m not understanding what you mean. :slight_smile: [import]uid: 66985 topic_id: 19952 reply_id: 77707[/import]

You can also look at ego if you want something really simple - it isn’t as powerful as Ice but would suit your needs; http://techority.com/2011/12/28/ego-easy-saving-and-loading-in-your-corona-apps/

Peach :slight_smile: [import]uid: 52491 topic_id: 19952 reply_id: 77709[/import]

@peach how do I use ego? [import]uid: 17058 topic_id: 19952 reply_id: 77719[/import]

@peach can you explain a little on how to use ego I’m not understating how to save? [import]uid: 17058 topic_id: 19952 reply_id: 77720[/import]

Hey, I tweeted at you as well.

[lua]ego.saveFile(“fileName.txt”, value)[/lua]

I realize you were trying to save something that wasn’t a value - “star” - was that an object or something? [import]uid: 52491 topic_id: 19952 reply_id: 77970[/import]

Oh I see you mention you didn’t require ego - do;

[lua]require “ego”[/lua] before trying to save or load. [import]uid: 52491 topic_id: 19952 reply_id: 77971[/import]

@peach pellen I did require “ego” and my star is an object so how do I make it to save having it to be an object [import]uid: 17058 topic_id: 19952 reply_id: 77976[/import]

Ice can be easy too. There is an example on the Ice page, showing you how to save and load data. Graham Ranson posted a demo. I mean there is an example if you would like to check it out, so that you understand it better. I don’t know any thing about ego, just giving you the information I know. As i said before download ice, then you can just copy past what he gave as a demo and you can dissect it from there.

Link: http://developer.anscamobile.com/code/ice

But whatever Peach says it’s always helpful :wink:
[import]uid: 23689 topic_id: 19952 reply_id: 77995[/import]

You can’t simply save an object - but I think you want to save how many starts the person scored, right? So you’d save like;

ego.saveFile(“level1stars.txt”, 3)

That would save the value 3.

As Michael says Ice is great, although far more complex.

Michael - Ego is something I wrote for very simple saving and loading, for those who were still a little overwhelmed by Ice - which I think is awesome and regularly recommend :slight_smile:

Peach [import]uid: 52491 topic_id: 19952 reply_id: 78008[/import]

That does sound easy. :slight_smile: [import]uid: 23689 topic_id: 19952 reply_id: 78009[/import]