Is there a way to make an object bounce around the screen randomly?

Is there a way to make an object, for example a ball, bounce around the screen. Sort of like a DVD logo that bounces around?

if you want a DVD logo-like effect, here’s an example (i’m not going to make a wick file for this).

start by making/importing whatever will be your DVD logo-like thing. then select it and convert it to a clip.

in the clip’s default script, give it 2 variables, this.xvel and this.yvel. set them to whatever numbers you want, but i’d expect them to be small-ish numbers (start trying something between 3 and 7). you can use negative numbers too. those 2 variables will be how fast the object will move.

now you want to check when the object touches a wall. we probably want it so that the object bounces based on the edge of the object, not the center of the object, so we have to incorporate the width and height of the object (and project).

by doing the following checks in the update script, you make sure that the object’s velocity keeps your object inside the bounds of the canvas.

  • if the X coordinate of the object (found with this.x) is less than this.width/2, make xvel positive. starting from the middle and subtracting half the width, we get the left edge of the object.
  • if the X is more than project.width - this.width/2, make xvel negative.
  • if the Y is less than this.height/2, make yvel positive.
  • if the Y is more than project.height - this.height/2, make yvel negative.

all we need to do now is to actually increment the X and Y by their velocities. so in the update tab still, add xvel to the X and yvel to the Y.

assuming you did these steps right (and assuming I didn’t make any typos), you should be done.

1 Like

Thank you, it is working very well!

1 Like