1

Alright, so me and my buddy is creating a game and a website, he is working on the game, and i am working on the site. The game is built using the ImpactJS engine, and everything works fine and so, but the problem we have ecountered is how we should save the highscore. Currently we got a website built, where you can log in and then play, and our idea is that after you finish the level (or when you die) you get a score, then that score gets send to the database along with the current user thats logged in. We got the function that would do that, but the problem is that we dont know how we should send the score from the javascript file, to the actuall database.

So this is how things are looking right now: First we actually got the highscore function in the main.js

Main.js

draw: function() {
        this.parent();
        this.background.draw(0,0);
        var x = ig.system.width/2;
        var y = ig.system.height/2 - 20;
        this.gameOver.draw(x - (this.gameOver.width * .5), y - 30);
        var score = (this.stats.kills * 100) - (this.stats.deaths * 50);
        this.instructText.draw('Total Kills: '+this.stats.kills, x, y+30,
        ig.Font.ALIGN.CENTER);
        this.instructText.draw('Total Deaths: '+this.stats.deaths, x, y+40,
        ig.Font.ALIGN.CENTER);
        this.instructText.draw('Score: '+score, x, y+50, ig.Font.ALIGN.CENTER);
        this.instructText.draw('Press Spacebar To Continue.', x, ig.system.height -
        10, ig.Font.ALIGN.CENTER);
        }

We understand that its the +score that we have to insert into the database.

Then we got the DB class, thats going to insert the data into the database (and by the way, im working object oriented since this is a school project and we have to do it this way)

DB.php

public function insert($table, $fields = array()) {     
    $keys = array_keys($fields);
    $values = '';
    $x = 1;

    foreach($fields as $field) {
        $values .= '?';
        if($x < count($fields)) {
            $values .= ', ';
        }
        $x++;
    }

    $sql = "INSERT INTO {$table} (`" . implode('`,`', $keys) . "`) VALUES ({$values})";

    if(!$this->query($sql, $fields)->error()) {
        return true;
    }

    return false;
}

And then lastly we got the highscore class:

Highscore.php

public function create($fields = array()) {
    if(!$this->_db->insert('highscores', $fields)) {
        throw new Exception('There was a problem saving your score. Sorry!');
    }
}

Yeah, so basicaly we dont know how we should get the +score from the external javascript file, and send it into to php... We did do some research and found out that maybe we should use Ajax for this? Anyhow, anybody can lend a hand?

user3383540
  • 159
  • 1
  • 1
  • 5
  • Yes, you should use ajax to send the high score to Highscore.php. You need to decide when the high score is sent over (right now, the `draw` function looks like it's running frequently, but you probably just need to send the high score between levels or at game over), and you need a dedicated event handler (separate javascript function) that is triggered when you want the high score to be sent and send it the username and score. While it's great to see your code rather than no code, please try to avoid posting chunks of code that are irrelevant to your problem. – Anthony Mar 23 '14 at 14:50
  • 1
    Okay thanks! Just wanted to make sure that people understood my problem. Going to go futher into ajax then! Thanks again! – user3383540 Mar 23 '14 at 14:56

0 Answers0