Trying screencast 2 to make the player move

20 views
Skip to first unread message

Jeff Johnson

unread,
Mar 2, 2011, 5:16:36 PM3/2/11
to ruby4kids
I am running on Win7 and using Ruby192
My_game.rb reads

$: << File.expand_path(File.dirname(__FILE__))
require 'rubygems'
require 'gosu'
require 'player'

class MyGame < Gosu::Window
def initialize
super(300, 400, false)
@player = Player.new(self)
end

def update
if button_down? Gosu::Kbleft
@player1.move_left
end
end

def draw
@player.draw
end

My player.rb file reads as
class Player

def initialize(game_window)
@game_window = game_window
@icon = Gosu::Image.new(@game_window, "images/player1.png", true)
@x = 50
@y = 50
end

def draw
@icon.draw(@x, @y, 1)
end

def move_left
@x = @x - 10
end
end

When I run the ruby my_games.rb in command prompt the ruby window
opens briefly then goes away and receive the below error message

my_game.rb:12:in 'update': uninitialized constant Gosu::Kbleft
(NameError)
from my_game.rb:24:in 'show'
from my_game.rb:24:in '<main>'

Any idea what I'm doing wrong?

NeutralAngel

unread,
Mar 2, 2011, 5:19:16 PM3/2/11
to ruby4kids
Hi Jeff,

You want to replace Gosu::KbLeft with Gosu::Button::KbLeft.

Nathan

Alberto Morales

unread,
Mar 2, 2011, 5:20:15 PM3/2/11
to ruby...@googlegroups.com
The constant is. Ruby, and most programming languages are case sensitive. The L is uppercase, and I am not sure if you left the Button part of the constant name.

Gosu::Button::KbLeft

Alberto.

Jeff Johnson

unread,
Mar 2, 2011, 5:28:49 PM3/2/11
to ruby4kids
OK. Great that worked now my_game.rb file now reads
$: << File.expand_path(File.dirname(__FILE__))
require 'rubygems'
require 'gosu'
require 'player'

class MyGame < Gosu::Window
def initialize
super(300, 400, false)
@player = Player.new(self)
end

def update
if button_down? Gosu::Button::KbLeft
@player1.move_left
end
end

def draw
@player.draw
end

end

when I go to execute in cmd prmpt the ruby screen is presented with my
image and when I press the left button the screen disappears and this
error is presented.
my_game.rb:14:in 'update': undefined method 'move_left' for
nil:NilClass <NoMethodError>
from my_game.rb:25:in 'show'
from my_game.rb:25:in '<main>'

Alberto Morales

unread,
Mar 2, 2011, 5:44:19 PM3/2/11
to ruby...@googlegroups.com
@player1 = Player.new(self)

Alberto

PastorJeffJ

unread,
Mar 2, 2011, 5:55:34 PM3/2/11
to ruby4kids
thanks works great.

On Mar 2, 5:44 pm, Alberto Morales <amora...@opnet.com> wrote:
> @player1 = Player.new(self)
>
> Alberto
>
Reply all
Reply to author
Forward
0 new messages