Как да помогнем?

### 1 коментар

What is the goal of this program? Right now, this is what the program does:

As soon as any key is pressed, an object starts bouncing from side to side on the screen, playing a sound every time it hits a wall.

I'm not sure if this is the intended performance of the program, but I can hear sound every time the object hits a border, so the sound is working. Just remember that in order to enter keystrokes into a KA program, you must first click on it before you type anything.

If you want to make function always run instead of you having to press a key, just remove the keyPressed = function(){};. I would advise removing it anyways since it is generally not a good idea to have a function inside a function. Here is what I would suggest.

`//these are the variablesvar y = -200;var x = 200;var speed = 10;draw = function(){    background(151, 244, 247);    fill(255, 0, 0);    ellipse(x, y, 70, 70);    fill(255, 200, 0);    ellipse(x - 15, y - 15, 10, 10);    ellipse(x + 15, y - 15, 10, 10);    line( x - 15, y + 15, x + 15, y + 15);    ellipse (x, y - 32, 70, 30);    fill ( 255, 200, 0);    ellipse ( x - 15, y + 30, 20, 20);    ellipse ( x - 0, y + 30, 20, 20);    ellipse ( x + 15, y + 30, 20, 20);    ellipse ( x + 10, y + 40, 20, 20);    ellipse ( x - 10, y + 40, 20, 20);    ellipse ( x + 2, y + 50, 20, 20);    //this makes is move    if(y > 350){        speed = -10;        playSound(getSound ("rpg/hit-thud"));    }    if(y < 50){        speed = 10;        playSound(getSound ("rpg/hit-thud"));    }        y = y + speed;        if(x > 350){        speed = -10;        playSound(getSound ("rpg/hit-thud"));    }    if(x < 50){        speed = 10;        playSound(getSound ("rpg/hit-thud"));    }        x = x + speed;};keyPressed = function(){    //this resets the variables when a key is pressed    y = -200;    x = 200;    speed = 10;};`

So, the keyPressed function works outside fo the draw function. Whenever you press a key, everything inside the keyPressed function is run once. In this case, it just changes the x, y, and speed back to their original values.

Hope this helps,

Daniel

Влезте в услугата, за да оставите коментар.