How to change object size upon bouncing in p5.play

I don’t know how to change the length of an object upon bouncing interaction in p5.play. 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() {
  background(255);
  if (frameCount % 120 == 0) {
    bal = createSprite(200, 10, 12, 12);
    bals.push(bal);
  }

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

  drawSprites();
}

function shortenBar(ball, bar) {
  ball.remove();
  //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
HAMMER and NAIL ?

-a- that is a p5.js question, so better as posting code is
to link to your
https://editor.p5js.org
project

-b- you use createSprite?
so it could have helped to say that
you use a library
http://molleindustria.github.io/p5.play/ ;

-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)     } 

example:
https://editor.p5js.org/kll/sketches/_1NnYvVhB

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
sprite.remove();
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?
https://molleindustria.github.io/p5.play/docs/classes/Sprite.html#method-bounce
!make your own bounce code

sprite.height -=1;
could also work inthere

2 Likes

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

1 Like