ippa/chingu

EnterName and HighScore GameStates not working as expected

RyanPadveen opened this issue · 2 comments

I get a black screen. Am I using these GameStates correctly?

require 'rubygems' rescue nil
$LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
require 'chingu'
include Gosu
include Chingu

class Game < Chingu::Window
  def initialize
    super
  end
  def setup
    switch_game_state(MainState)
  end
end

class MainState < Chingu::GameState
  def initialize
    super
    $name = "Player"
    $points = 500
    $high_score_list = Chingu::HighScoreList.load(:size => 10)
    $window.push_game_state(GameStates::EnterName.new(:callback => method(:got_name)))
  end

  def got_name(name)
    $name = name
    $newscore = $high_score_list.add({name: $name, score:   $points})
    puts "Got name: #{name}"
    pop_game_state
    $window.switch_game_state(HighScores)
  end
end

class HighScores < Chingu::GameState
  def initialize
    super
    create_text
  end

  def create_text
    $high_score_list.each_with_index do |high_score, index|
      y = index * 25 + 100
      Text.create(high_score[:name], :x => 200, :y => y, :size => 20)
      Text.create(high_score[:score], :x => 400, :y => y, :size => 20)
    end
  end
end

Game.new.show
ippa commented

does example16_online_high_scores.rb or example13_high_scores work for you?

@ippa example13_high_scores.rb works perfectly. example16_online_high_scores.rb mostly works, but it crashes sometimes when I press R.

/Users/ryanpadveen/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/ryanpadveen/RubymineProjects/ChinguHighScore/example16.rb
RequestFailed: couldn't add high score
/Users/ryanpadveen/.rvm/gems/ruby-1.9.3-p194@ucode/gems/chingu-0.8.1/lib/chingu/online_high_score_list.rb:81:in `add': undefined method `[]' for nil:NilClass (NoMethodError)
    from /Users/ryanpadveen/RubymineProjects/ChinguHighScore/example16.rb:53:in `add_random'
    from /Users/ryanpadveen/.rvm/gems/ruby-1.9.3-p194@ucode/gems/chingu-0.8.1/lib/chingu/helpers/input_dispatcher.rb:83:in `[]'
    from /Users/ryanpadveen/.rvm/gems/ruby-1.9.3-p194@ucode/gems/chingu-0.8.1/lib/chingu/helpers/input_dispatcher.rb:83:in `block in dispatch_actions'
    from /Users/ryanpadveen/.rvm/gems/ruby-1.9.3-p194@ucode/gems/chingu-0.8.1/lib/chingu/helpers/input_dispatcher.rb:80:in `each'
    from /Users/ryanpadveen/.rvm/gems/ruby-1.9.3-p194@ucode/gems/chingu-0.8.1/lib/chingu/helpers/input_dispatcher.rb:80:in `dispatch_actions'
    from /Users/ryanpadveen/.rvm/gems/ruby-1.9.3-p194@ucode/gems/chingu-0.8.1/lib/chingu/helpers/input_dispatcher.rb:45:in `dispatch_button_down'
    from /Users/ryanpadveen/.rvm/gems/ruby-1.9.3-p194@ucode/gems/chingu-0.8.1/lib/chingu/game_state.rb:155:in `button_down'
    from /Users/ryanpadveen/.rvm/gems/ruby-1.9.3-p194@ucode/gems/chingu-0.8.1/lib/chingu/game_state_manager.rb:251:in `button_down'
    from /Users/ryanpadveen/.rvm/gems/ruby-1.9.3-p194@ucode/gems/chingu-0.8.1/lib/chingu/window.rb:202:in `button_down'
    from /Users/ryanpadveen/RubymineProjects/ChinguHighScore/example16.rb:107:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'

Process finished with exit code 1