Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6965839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:06:49+00:00 2026-05-27T16:06:49+00:00

I am developing a game with Ruby using the Gosu and Chipmunk gems. I

  • 0

I am developing a game with Ruby using the Gosu and Chipmunk gems. I have the following class in the file named HeroBullets.rb:

require 'gosu'

class HeroBullets
  attr_accessor :y
  def initialize(window)
    @x = 20
    @y = 0
  end
end

I know need to access this class from another file, Physics.rb which handles all the Chipmunk code.

At the top I have:

require 'chipmunk'

load 'HeroBullets.rb'

class Physics
   attr_accessor :play_area 

def initialize(window)

    @hBullets = Array.new(25)
    @hBullets << HeroBullets.new(window)
    @hBullets << HeroBullets.new(window)
end

And further down there is:

  def fire_arrow(y)
    for i in 0...@hBullets.count
      @bullet = @hBullets[i]
      if(@bullet.y == y)
        @hBullets[i].active = true
      end
    end
  end

The Error I get is:

Physics.rb:112:in block in fire_arrow': undefined methody' for nil:NilClass 
(NoMethodError) from Physics.rb:110:in each' from Physics.rb:110:infire_arrow'
from FileManager.rb:90:in fireHero' from .../lib/main.rb:90:inupdate' from .../lib/main.rb:129:in `'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:06:50+00:00Added an answer on May 27, 2026 at 4:06 pm

    The problem is that if @hBullets has 10 elements, @hBullets.count will output 10, but @hBullets[10] does not work, because the index of an array starts at 0 not at 1. The tenth element will be in @hBullets[9]. You get the error message because the element you are trying to access is nil, not because “attr_accessor is not being read”.

    That being said, Ruby offers much easier ways to iterate over an array. I would rewrite your code like this:

    def fire_arrow(y)
      @hBullets.each do |bullet|
        bullet.active = true if bullet.y == y
      end
    end
    

    Another problem with your code is that you initialize a new array like this:

    @hBullets = Array.new(25)
    

    This creates an array with 25 elements that are all nil. You should start with an empty array instead:

    @hBullets = Array.new
    

    Or:

    @hBullets = []
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been developing a game using Flex, and used the Timer class to
I am developing game editor in c++.I have implemented reflection mechanism using DiaSDK.Now I
I am developing a game in which I am using Landscape mode I have
While developing a game I have designed a class that in normal operation has
I'm developing game app and using Symfony 2.0. I have many AJAX requests to
Am developing a game using HTML5 and CSS3. I have a bunch of divs
Hi i am developing game level editor.Currently I am using win32 with directX.But with
I'm developing a game using XNA and C# and was attempting to avoid calling
I'm developing a game. I want to have game entities each have their own
I'm developing a game for my dissertation, and I'm using the spiral method approach.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.