Vairable size Array in a class

ch3ch3
edited November 2015 in JavaScript Mode

I started developing a sketch in processing and I thought I'd try and switch it to javascript so I can have it online. Haven't managed to fully run it on the browser yet and I think the problem is declaring and using an int[] within the class. Do arrays not work in a different way?

here is part of the class, where the commended lines make it break :

class VImage
{
  int cDepth;
  int w;
  int h;
  int size;
//  int[] pixels;
  int pen;


  VImage(int wIn, int hIn, int cIn)
  {
    cDepth = cIn;
    w = wIn;
    h = hIn;
    size = w * h;
    pen = 0;


//    int[] pixels = new int[size];

    clear();
  }
.....
...

Answers

  • _vk_vk
    edited November 2015 Answer ✓

    p5.js ou procesing.js? What happens wrong when trying in browser?

    Anyway, related or not with the issue. You are re-declaring the array inside the constructor. I think at line 20 you would want

    pixels = new int[size];
    

    Also I don't think is a good idea to name your array pixels as this is the name of the one existent in the API . Specially in javaScript.

  • oohh yea! calling it pixels was indeed the problem.

    thank you for pointing this out.

    Out of curiosity what do you mean by p5.js and processing.js? Does processing have two different javaScript projects? I've just changed the mode of processing (2.2.1) to JavaScript on the top right corner.

  • edited November 2015

    "JavaScript Mode" relies on Processing.JS project (a.K.a pjs):
    http://ProcessingJS.org/reference/
    It is focused on transpiling "Java Mode" sketches into actual JS syntax for web deployment.

    The newest p5.js project doesn't care about Java syntax and it's a Processing re spin:
    http://p5js.org/reference/

  • actually it's processing.js, I saw it on firefox's console.

  • oh interesting... is there any benefit in switching to p5.js?

    New to the javaScript world, so still loads to find out.

  • edited November 2015 Answer ✓

    If you know JS or some other transpilable language like TypeScript, you're better off w/ p5.js.

    As a workaround, there's also "CoffeeScript Mode" which is also based on PJS:
    https://GitHub.com/fjenett/coffeescript-mode-processing

    Although its syntax is closer to Ruby & Python, at least it's got classes like Java. :P

    In order to program in JS, you need to know about prototype & the meaning of this though. :-SS

    Or rely on TypeScript or Babel to write in ECMA6's new features and got them transpiled as ECMA5.

Sign In or Register to comment.