hilaloytun
2022-7-21 10:03:06

@hilaloytun has joined the channel


hilaloytun
2022-7-21 10:08:47

Hello all, I have a problem with world program. [on-mouse mouse-event-handler] in big-bang causes error: “place-image: expects a real number as second argument, given #<image>”

(define (mouse-event-handler cw x y me) (cond [(mouse=? me “button-down”) (place-image IMG x CAR-Y BACKGROUND)] [else cw]))

Could you help why is it happening?


hilaloytun
2022-7-21 10:15:33

This is the main function: (define (main ws) (big-bang ws [on-tick tock] [to-draw render] [on-mouse mouse-event-handler] [on-key handle-key] [stop-when gone])) helpers: (define (render cw) (place-image IMG cw CAR-Y BACKGROUND)) (define (gone cw) (= cw (- BACKGROUND-W 10))) (define (handle-key c ke) (cond [(key=? ke " ") 0] [else c])) (define (mouse-event-handler cw x y me) (cond [(mouse=? me “button-down”) (place-image IMG x CAR-Y BACKGROUND)] [else cw]))


hilaloytun
2022-7-21 10:18:58

The second problem I get is; If I press a key during animation, it doesn’t execute the “stop-when step”(which is [stop-when gone]). If I leave the animation alone, don’t touch the keys, it executes the “stop-when” step.


laurent.orseau
2022-7-21 10:35:26

The place-image issue seems to be with render where the second argument shouldn’t be cw


hilaloytun
2022-7-21 12:05:12

Thank you very much; I think I am missing something, since “cw” is supposed to the the “x position” of the image.


laurent.orseau
2022-7-21 12:18:49

That’s not how it’s used in mouse-event, where according to the docs the first argument to the mouse-event handler must be the world state: https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-mouse%29%29


soegaard2
2022-7-21 12:56:47

FWIW cw stands for “current world” (at least that’s my guess).


hilaloytun
2022-7-21 13:48:39

yes, cw is “current world” (the x point on the scene, where the image’s center pixel is located)


soegaard2
2022-7-21 13:49:42

Ah! Your world consists only of that x-coordinate?


hilaloytun
2022-7-21 13:50:51

the animation on the scene is dynamic only on the x- coordinate, there’s a linear movement.


laurent.orseau
2022-7-21 13:52:28

But mouse-event-handler returns an image instead of a coordinate


laurent.orseau
2022-7-21 13:52:35

on “button-down”


soegaard2
2022-7-21 13:54:25

Laurent is right. The idea is that the mouse handler computes a new world state. Then later, render will draw an image for that world state.


hilaloytun
2022-7-21 13:55:41

so, wherever I click on the scene, only x-position should be changed, y stays the same.


soegaard2
2022-7-21 13:55:46

Your immediate problem is how to keep track of both the x-coordinate and whether the mouse was clicked or not. You most likely want to change your world state to a struct that has two fields: one for the x-coordinate and one for mouse information.


laurent.orseau
2022-7-21 13:56:29

@hilaloytun You need to make sure that mouse-event-handler returns an x-coordinate in all branches of your cond


hilaloytun
2022-7-21 13:57:42

ah, ok, I now understand, (not the solution, but the problem itself). thank you.