Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Candy Crush Saga Type countdown life system jQuery & Phonegap

I'm developing an app and I'm interested in ways to achive a Candy Crush Saga Type countdown life system. My app is developed in phonegap (so html, css jquery and jquery mobile) and untill now I havn't figured out how to work with an external file as I'm thinking it's needed in this algorithm. I want therefore to have 5 lives and each of them dissapears when the user fail or quit the lvl and regenerates after, lets say, 10 minutes. How can I keep the counter active if the app is closed? Or a date substraction algorithm...

If somebody have knowledge in Phonegap I would be very grateful if he could help me with a jsfiddle wich I'll implement further in my app.

I'm also not sure what to use: localstorage vs DB

I'll give 50 of my bounty for the one who can help me with a jsfiddle example for this issue.

Thank you!

like image 516
Alex Stanese Avatar asked Mar 20 '23 22:03

Alex Stanese


1 Answers

Edit: Sorry this is some pseudo code based off PHP - but you don't need crazy algorithms or anything

I don't write phone apps, but I know what you're trying to do and you're thinking about it too hard. You don't need to run the counter while the app is closed. You can save a timestamp from when the game is over, and reference it the next time the app is opened.

start_game();

if($lives > 0){ //run game
    if($death === true){
        $lives = $lives - 1;
        $timer_value = date(Y-m-d h:i:s);
        end_game($timer_value); //save starting time locally in "end_game" function
    }

} else {
    //buy more lives
}

Now let's say the user closes it and opens up. You can reference the current time versus the saved time. For every 10 minutes past then, add a life.

$old_time = strtotime($timer_value); //635393400
$cur_time = strtotime(date(Y-m-d h:i:s)); //635394600

if( $cur_time - $old time > 600 ) { $lives = $lives + 1; }
if( $cur_time - $old time > 1200 ) { $lives = $lives + 2; }
if( $cur_time - $old time > 1800 ) { $lives = $lives + 3; }
if( $cur_time - $old time > 2400 ) { $lives = $lives + 4; }
if( $cur_time - $old time > 3000 ) { $lives = $lives + 5; }
if( $lives > 5 ) { $lives = 5; }

That's some awful code lol, but you gut the idea. You don't need to run a timer in the background (you can't really with the app being closed, without doing some sort of server checks online, which is basically the same thing, but hosting all that personal life records in the cloud instead of on the phone.

like image 167
Xhynk Avatar answered Apr 09 '23 03:04

Xhynk