"Uncaught TypeError: string is not a function"

edited May 2016 in JavaScript Mode

Hi! I've written a short program that displays letters of the alphabet each frame which then fade out (by drawing a semi-transparent rectangle over them).

It works fine as a Processing application, but when I try to put it on the web with ProcessingJS 1.4.8 (and 1.4.1 for that matter), I get the error "Uncaught TypeError: string is not a function" which points to this line in the following code: text(text, random(float(width)), random(float(height)));

When I comment it out, the sketch runs (but without any text!) fine. I've tried bits and pieces that haven't worked, so I'm kinda stuck with what to try next to solve the problem :)

Thanks in advance!

    String text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String letter;
    int n = 0;
    color text_c = color(255, 123, 123);
    color background_c = color(50, 20);
    PFont font;

    void setup()
    {
        size(600, 600);
        font = createFont("Arial", 30);
        textFont(font);
        textAlign(CENTER, CENTER);
        noStroke();
    }

    void draw()
    {
        fill(background_c);
        rect(0, 0, width, height);
        fill(text_c);
        letter = text.substring(n, n);
        textSize(random(300.0f));
        text(text, random(float(width)), random(float(height)));

        if(n < text.length() - 1)
            n++;
        else
            n = 0;
    }

Answers

  • Answer ✓

    try rename var

    String text

    better

    String textMine

    line 1

  • Oh, of course! I forgot that I had to do that for ProcessingJS, thanks so much :)

  • edited May 2014

    Actually avoiding having variables crashing w/ same name functions is good programming in general! B-)
    Another potential bug is accidentally overshadowing a field by re-declaring it as a local variable! :-SS

Sign In or Register to comment.