Using 'shorten' in an else if statement

edited November 2017 in Questions about Code

I currently have some code that uses 'append' to add one to my array on a key press

void keyPressed() {
    if (key == '+' || key == '=') {
      Node n = new Node(width/2+random(-200, 200), height/2+random(-200, 200));
  nodes = (Node[]) append(nodes, n); 
      initNodesAndSprings();

I am now trying to add an else if to 'shorten' the array with a different key press, I am currently trying this approach...

void keyPressed() {
    if (key == '+' || key == '=') {
      Node n = new Node(width/2+random(-200, 200), height/2+random(-200, 200));
  nodes = (Node[]) append(nodes, n); 
      initNodesAndSprings();
    }
    else if (key == '-' || key == '_') {
      Node n = new Node(width/2+random(-200, 200), height/2+random(-200, 200));
  nodes = (Node[]) shorten(nodes, n); 
      initNodesAndSprings();
  }
}

however I am faced with the error "the function shorten() expects parameters like: shorten(boolean[])", I feel like this should be a simple fix but I cant figure it out.

Answers

  • edited November 2017

    The function shorten reduces the length by 1 so the second parameter is not required. Try -

    nodes = (Node[]) shorten(nodes);

    Also line 8 is no longer required. No need to create a new node if your reducing the number of nodes in the array.

Sign In or Register to comment.