2015-10-20 2 views
2

У меня возникла проблема с указанием положения x и позиции y игрока. Когда я называю origin_object.x и origin_object.y с классом дает снаряд мне эту ошибку:Неопределенный метод (NoMethodError) ruby ​​и Gosu

asteroids.rb:35:in `update': undefined method `x' for #<Player:0x007fa7c307c520> (NoMethodError) 
    from /usr/local/lib/ruby/gems/2.2.0/gems/gosu-0.10.4/lib/gosu/patches.rb:135:in `tick' 
    from /usr/local/lib/ruby/gems/2.2.0/gems/gosu-0.10.4/lib/gosu/patches.rb:135:in `tick' 
    from asteroids.rb:68:in `<main>' 

Это мой код:

class Player 

    attr_accessor :x, :y, :angle, :lives, :score 
    def initialize 
    @image = Gosu::Image.new("assets/nave1.png") 
    @x = @y = @vel_x = @vel_y = @angle = 0.0 
    @score = 0 #Puntaje 
    @lives = 5 #Vida 
    end 
end 

это файл снаряд

class Projectile 
    attr_reader :x, :y, :angle 
    def initialize(origin_object) 
    @alive = true 
    @x, @y = origin_object.x, origin_object.y 
    @angle = origin_object.angle 
    @speed_modifier = 7 
    @image = Gosu::Image.new('assets/projectile.png') 
    @distance_traveled, @max_distance = 0, 50 
    end 
end 

Это основной файл (который я использую для запуска проекта)

require 'gosu' 
require './lib/Player.rb' 
require './lib/Projectile.rb' 

class GameWindow < Gosu::Window 

    def initialize 
    super(1000, 700, false) #Creacion Pantalla 
    self.caption = "Asteroids Redes" #Titulo Pantalla 
    @font = Gosu::Font.new(self, "assets/victor-pixel.ttf", 34) 
    end 

    def setup_game 
    @player = Player.new 
    @player.warp(650, 350) 
    @game_in_progress = true 
    @projectiles = [] 
    end 

    #--------------------------------------# 
    def update 

    if Gosu::button_down? Gosu::KbQ #Salir con tecla Q 
     close 
    end 
    if button_down? Gosu:: KbP 
     setup_game unless @game_in_progress 
    end 

    if @player #si existe jugador permite moverlo 
     if Gosu::button_down? Gosu::KbSpace 
     p @player.x 
     #@projectiles << Projectile.new(p @player) 
     end 
     if Gosu::button_down? Gosu::KbLeft or Gosu::button_down? Gosu::GpLeft then 
     @player.turn_left 
     end 
     if Gosu::button_down? Gosu::KbRight or Gosu::button_down? Gosu::GpRight then 
     @player.turn_right 
     end 
     if Gosu::button_down? Gosu::KbUp or Gosu::button_down? Gosu::GpButton0 then 
     @player.accelerate 
     end 
     @player.move 
     @projectiles.each {|projectile| projectile.move} 
    end 

    end 
    #--------------------------------------# 
    def draw 
    unless @game_in_progress #Si el no se esta ejecutando muestra el menu 
     @font.draw("ASTEROIDS", 260, 220, 50, 3, 3, Gosu::Color::rgb(255, 255, 255)) 
     @font.draw("Presiona 'p' Para Jugar", 300, 320, 50, 1, 1, Gosu::Color::rgb(13, 123, 255)) 
     @font.draw("Presiona 'q' Para Salir", 305, 345, 50, 1, 1, Gosu::Color::rgb(13, 123, 255)) 
    end 
    if @player #Si existe jugador lo dibuja 
     @player.draw 
     @projectiles.each {|projectile| projectile.draw} 
    end 
    end 
    #--------------------------------------# 
end 
#........................................# 
window = GameWindow.new 
window.show 

ответ

0

Я скопировал код из ваших трех файлов, добавил пустые заглушки метода для move, draw, accelerate, turn_left/turn_right, так что код не будет немедленно сбой.

Когда я запускаю игру и нажимаю P, тогда пробел, я вижу «0.0» в терминале в результате p @player.x.

Возможно, вы используете другую версию кода, чем то, что вы включили в свой вопрос?

Смежные вопросы