我想让对象互相交互。 (红宝石)

时间:2013-02-07 20:39:56

标签: ruby libgosu

我使用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)

我该怎么办?

1 个答案:

答案 0 :(得分: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)