How to change object size upon bouncing in

I don’t know how to change the length of an object upon bouncing interaction in In the code below, I wish to make the bar shorter from the top upon every hit, but don’t know what keyword to search for in the reference doc. Any suggestions?

var bals = [];

function setup() {
  createCanvas(400, 400);
  ball = createSprite(200, 10, 12, 12);
  bar = createSprite(200, 300, 2, 100);
  bar.immovable = true;

function draw() {
  if (frameCount % 120 == 0) {
    bal = createSprite(200, 10, 12, 12);

  for (i = 0; i < bals.length; i++) {
    bals[i].setSpeed(2, 90);
    bals[i].bounce(bar, shortenBar);


function shortenBar(ball, bar) {
  //make bar 10pixel shorter at the top upon each hit

Besides, how to make a sprite circular in shape as the default is square?
Thanks for your help!

1 Like

hi, is the idea about

-a- that is a p5.js question, so better as posting code is
to link to your

-b- you use createSprite?
so it could have helped to say that
you use a library ;

-c- you make a NAIL and call it immovable,
but actually you want it to move
++ disable immovable
++ make it heavy
++ give it friction

-d- also why the NAIL should be round?
but possible with using

    ball.draw = function() {      ellipse(0,0,20,20)     } 


what adds also random HAMMER
color and x position
for fun

1 Like

Thank you for your reply. Actually the bar isn’t a nail, and I need it to shorten upon each hit instead of moving down. Is there a way to do that?

1 Like

sorry, as i state in the example code i not know that library.

you could worst case use a
and make a new one on a lower position…
but i know that is actually not the point, it is how to use the collision event?
!make your own bounce code

sprite.height -=1;
could also work inthere


Thanks! You pointed me to the right direction. Adjusting height and y position will solve the problem.

1 Like