Code error on Lesson 9 in Tutorial

38 views
Skip to first unread message

spamcatcher

unread,
Jan 13, 2013, 11:24:54 AM1/13/13
to ruby...@googlegroups.com
If anyone is using the tutorial pages Lesson 9 has an omission
 
Where you pass the @player.laser to the bullet update method (approx line 31 in Window code) as noted in the comments, this will fail unless you add the laser value to the Player class as such:
 
class Player
attr_reader :lives, :x, :y, :laser
.
.
.
 
hope this helps.

Mark

unread,
Jun 22, 2013, 7:08:48 PM6/22/13
to ruby...@googlegroups.com
It still fails - now with "wrong number of arguments (1 for 0)"
hmmm
I don't see how the live_bullets call works here - or how to fix it.

Alberto Morales

unread,
Jun 23, 2013, 1:19:27 AM6/23/13
to ruby...@googlegroups.com
Can you send the snippet of your code to see how you are using live_bullets.

Alberto.

--
You received this message because you are subscribed to the Google Groups "ruby4kids" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby4kids+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Mark

unread,
Jun 28, 2013, 3:45:33 AM6/28/13
to ruby...@googlegroups.com
Thanks Alberto
 
class Window < Gosu::Window
....
  def run_game
    live_bullets.each {|bullet| bullet.update(@player.laser)}
    @player.update
  end
 
  def live_bullets
    @bullets.select{|bullet| bullet.alive == true}
  end
...
end
 
and
 
class Bullet
  attr_reader :x, :y, :alive, :laser
...
  def update
    @y=@y+10
    if @y>@window.width
      @y=0
      @x=rand(@window.width)
    end
    hit_by?(laser)
  end
...
end
 
 
The Player class doesn't have a .laser method.
Adding :laser to the attr_reader in the Player class confuses the program because it now expects @player.laser to be a variable but is not expecting a variable to be passed to the bullet.update method (the laser variable in the "hit_by?(laser)" call is dragged into Bullet by attr_reader).
 
So I'm a bit confused, and my program is too.
 
I'm wondering if it has to do with the "bullet" words: the Window class now has a number of "bullets" defined as
@bullets = 2.times.map {Bullet.new(self)}
but no definition of "bullet" (single and lowercase) - could it be that I need to change the calls such as
 live_bullets.each {|bullet| bullet.update(@player.laser)}
to reflect this?
 
Thanks so much for your help.
 
Mark
Message has been deleted

Mark

unread,
Jul 1, 2013, 9:05:37 PM7/1/13
to ruby...@googlegroups.com
Ok - so that last bit shows that I didn't understand the syntax of the .each or .select methods for an array.
I realise now that
>>  live_bullets.each {|bullet| bullet.update(@player.laser)}
is defining a temporary object 'bullet' to hold the result of the .each call, or something like that.
 
I've got it now - I've missed out the (laser) argument for the definition of the bullet.update method. So it does work by adding :laser to the attr_reader after all.
Sorry to bother you.
 
Mark
Reply all
Reply to author
Forward
0 new messages