reset password
Author Message
hal255
Posts: 51
Posted 19:17 Oct 24, 2015 |

Hey guys. I'm not sure why I am getting the error on (ball-y-pos (first otherballs)) where it is expecting a ball, but getting a list of balls. Otherballs is a list of the remaining balls, so (first otherballs) should only give it one ball. Here is the function I wrote:

#|
  Function update-velocity takes in a list of balls and returns updated velocity of first ball
  Parameters:
    ball-list   - list of balls passed in, the first one will be used to compare with the remaining ones
    velocity    - comes in as empty, but will store velocity of the first ball when returned
  Returns: updated velocity of first ball
|#
(define (update-velocity ball-sublist velocity)
  (display (format "\nBEFORE UPDATE velocity: ~a" velocity))
  (if (empty? (rest ball-sublist)) velocity
      (let* ([this-ball (first ball-sublist)]                                                 ; tracks current ball
             [otherballs (rest ball-sublist)]                                                 ; tracks remaining balls
             [this-velocity (ball-velocity (first ball-sublist))])                            ; tracks velocity of first ball
        (cond [(or (>= (ball-y-pos (first ball-sublist)) LOWEST-Y-POS)                        ; if current ball has reached top or bottom
                   (<= (ball-y-pos (first ball-sublist)) HIGHEST-Y-POS))                      
               (update-velocity (list this-ball) (- this-velocity))]                          ; then switch direction of it's velocity
              [(>= (+ (ball-y-pos this-ball) (ball-y-pos (first otherballs))) LOWEST-Y-POS)   ; if distance of the two balls is greater than LOWEST-Y-POS
               (update-velocity (list this-ball (rest otherballs)) (- this-velocity))]
              [else (update-velocity (list this-ball (rest otherballs)) this-velocity)])       ; then switch direction of current ball's velocity
        )))

Here is the output:

done updating y-pos: 28
done updating y-pos: 474
done updating y-pos: 252
BEFORE UPDATE velocity: ()
BEFORE UPDATE velocity: -3
BEFORE UPDATE velocity: ()
BEFORE UPDATE velocity: 1
BEFORE UPDATE velocity: ()
BEFORE UPDATE velocity: 2. . ball-y-pos: contract violation
  expected: ball?
  given: (list (ball (object:image% ...) 475 -1 0))

 

hal255
Posts: 51
Posted 19:28 Oct 24, 2015 |

I finally see the problem...

  [(>= (+ (ball-y-pos this-ball) (ball-y-pos (first otherballs))) LOWEST-Y-POS)   ; if distance of the two balls is greater than LOWEST-Y-POS
               (update-velocity (list this-ball (rest otherballs)) (- this-velocity))]
              [else (update-velocity (list this-ball (rest otherballs)) this-velocity)])       ; then switch direction of current ball's velocity

 

It should be:

  [(>= (+ (ball-y-pos this-ball) (ball-y-pos (first otherballs))) LOWEST-Y-POS)   ; if distance of the two balls is greater than LOWEST-Y-POS
               (update-velocity (cons this-ball (rest otherballs)) (- this-velocity))]
              [else (update-velocity (cons this-ball (rest otherballs)) this-velocity)])       ; then switch direction of current ball's velocity

Last edited by hal255 at 19:29 Oct 24, 2015.
shewitt2
Posts: 5
Posted 20:50 Oct 24, 2015 |
hal255 wrote:

I finally see the problem...

  [(>= (+ (ball-y-pos this-ball) (ball-y-pos (first otherballs))) LOWEST-Y-POS)   ; if distance of the two balls is greater than LOWEST-Y-POS
               (update-velocity (list this-ball (rest otherballs)) (- this-velocity))]
              [else (update-velocity (list this-ball (rest otherballs)) this-velocity)])       ; then switch direction of current ball's velocity

 

It should be:

  [(>= (+ (ball-y-pos this-ball) (ball-y-pos (first otherballs))) LOWEST-Y-POS)   ; if distance of the two balls is greater than LOWEST-Y-POS
               (update-velocity (cons this-ball (rest otherballs)) (- this-velocity))]
              [else (update-velocity (cons this-ball (rest otherballs)) this-velocity)])       ; then switch direction of current ball's velocity

That indeed would cause the issue.  Glad you finally figured out what it was!