libGDX:使用高分的首选项

时间:2017-10-19 22:58:15

标签: java android libgdx sharedpreferences

我知道我之前已经问过这个问题了,但我没有得到更多答案......我想,一旦我在游戏中丢失,我的HighScore就会显示出来。这是代码:

add_filter('post_gallery', 'ct_post_gallery', 10, 2);
function ct_post_gallery($output, $attr) {
    global $post;

    if (isset($attr['orderby'])) {
        $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
        if (!$attr['orderby'])
            unset($attr['orderby']);
    }

    extract(shortcode_atts(array(
        'order' => 'ASC',
        'orderby' => 'menu_order ID',
        'id' => $post->ID,
        'itemtag' => 'dl',
        'icontag' => 'dt',
        'captiontag' => 'dd',
        'columns' => 3,
        'size' => 'thumbnail',
        'include' => '',
        'exclude' => ''
    ), $attr));

    $id = intval($id);
    if ('RAND' == $order) $orderby = 'none';

    if (!empty($include)) {
        $include = preg_replace('/[^0-9,]+/', '', $include);
        $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));

        $attachments = array();
        foreach ($_attachments as $key => $val) {
            $attachments[$val->ID] = $_attachments[$key];
        }
    }

    if (empty($attachments)) return '';

    // Here's your actual output, you may customize it to your need
    $output = "<div id=\"icmimarlikreferans\">\n";
    $output .= "<ul id=\"icmimarlikreferansimg\">\n";

    // Now you loop through each attachment
    foreach ($attachments as $id => $attachment) {
        // Fetch the thumbnail (or full image, it's up to you)
//      $img = wp_get_attachment_image_src($id, 'medium');
//      $img = wp_get_attachment_image_src($id, 'my-custom-image-size');
        $img = wp_get_attachment_image_src($id, 'full');

        $output .= "<li class=\"item\">\n";
        $output .= "<img src=\"{$img[0]}\" width=\"{$img[1]}\" height=\"{$img[2]}\" alt=\"\" />\n";
        $output .= "</li>\n";
    }

    $output .= "</ul>\n";
    $output .= "</div>\n";

    return $output;
}

但如果我运行我的应用程序,则只显示此错误:

 protected Preferences HighScore () {

    if (score > highscore) { 
        prefs.putInteger("highscore", score); 

        this.highscore = prefs.getInteger("highscore", 0);

        prefs.flush(); 
    }
   return prefs;
}

我在网上看了但找不到任何解决方案......

感谢您的回答! :)

1 个答案:

答案 0 :(得分:0)

首先你得到了这个对象:

Preferences preferences = Gdx.app.getPreferences("My preferences");

然后,当你在游戏中失败时,你就会获得价值。我通常会检查我是否在使用布尔值的render方法中丢失了。在这种情况下,您将您的高分与您当前的分数进行比较:

if(IsGameFinished)
{
    int highscore = preferences.getInteger("High score",0);
    if(highscore>=yourCurrentScore)
    {
          // display highscore
    }
    else
    {
          // display yourCurrentScore
         preferences.putInteger("High score", yourCurrentScore);
         preferences.flush();
    }
}

此外,在您的代码中存在错误:

protected Preferences HighScore () {

if (score > highscore) { 
    prefs.putInteger("highscore", score); 
    prefs.flush(); // YOU SHOULD FLUSH BEFORE!
    this.highscore = prefs.getInteger("highscore", 0);
}
return prefs;
}

而且,为什么要退回偏好?将你的高分作为int返回应该会更好。