0

我正在使用 Gosu 库在 Ruby 中制作游戏 Pong。现在,我正在尝试让球与棋盘互动。

class Window < Gosu::Window
    # board size 30 X 298
    def initialize
        super 1440,720,false
        self.caption = "Pong"
        @Ball = Ball.new(self)
        @Ball.warp(720,360)
        @Board1 = Board.new(self,15,360)
        @Board2 = Board.new(self,1425,360) 
    end

    def update 
        @Ball.draw
        @Ball.move
        @Ball.bounceOffBoard(@Board1,@Board2)
       ........

在最后一行,我尝试将字段@Board1 传递给另一个类,以便@Ball 知道棋盘的坐标,看看它是否应该跳下。但它不断给我带来错误,比如

Pong.rb:105: formal argument cannot be a constant
    def bounceOffBoard(Board1,Board2)

我该怎么办?

4

1 回答 1

2

首先,不要用大写字母命名实例变量:

@Ball = Ball.new(self)
@Board1 = Board.new(self,15,360)
@Board2 = Board.new(self,1425,360) 

应该:

@ball = Ball.new(self)
@board1 = Board.new(self,15,360)
@board2 = Board.new(self,1425,360) 

和:

def bounceOffBoard(Board1,Board2)

应该:

def bounceOffBoard(board1, board2)
于 2013-02-07T20:43:56.617 回答