using tabs with classes

edited December 2013 in Using Processing

My apologies in advance, this is probably a very basic question (but I've spent a frustrating night reading about it on these forums and cannot quite figure out what the problem is.) This is also for a class assignment in the first programming course offered at my uni, and I want to be sure I understand the problem.

This assignment gives us four classes in separate files and asks us to write the guts of the functions. I've finished that part (well, at least until bug hunting, for which I need this to run.) We're recreating the classic Asteroids game.

My problem lies in actually running the contents of multiple tabs. The four classes are named MainGame (has setup, main and draw in it and is opened first in the tabs), Asteroid, Laser and Player. Each has its own folder, with the same name as the sketch in it. The order given is the order I've opened them in, in the tabs.

Ways I've tried to open and run these files, and the errors I've gotten: The sketch files were all opened as tabs from their separate respective folders (with have the same name as the sketches). The error I got is "The public type file_name must be defined in its own file."

After MainGame was opened, I created tabs and pasted the class code from the original classes into the new tabs (named the same as the original sketches but now in the same folder as MainGame because of the way tabs work). As per a suggestion on one of the forum posts I read, I removed the public modifier from all but the class for MainGame. The error I got is "Cannot find anything named width." Width is being used almost immediately in the next class' constructor, Asteroid, as per the code that follows:

    class Asteroid {

      // constructor: creates instance of the class Asteroid
      Asteroid() {
        x = random( width );
        y = random( height );
      }

Since width is a system variable, I'm really unsure why processing isn't recognizing it here. No doubt it's my error, but damned if I know where.

Obviously, there's something I missed here about how class files in separate tabs communicate. For reference, I read the following, previous to posting this:

http://processing.org/reference/environment/ http://forum.processing.org/two/discussion/1359/how-to-use-the-tabs-in-processing/p1 http://processing.org/discourse/beta/num_1114632172.html http://www.processing.org/discourse/beta/num_1144058384.html http://forum.processing.org/two/discussion/125/how-do-i-use-a-variable-in-another-tab/p1

I will keep trying to figure out what's happening here, but at this point I'm very confused (and not a little frustrated.) o-+

Answers

    • By default, each tab is saved as a ".pde" file!
    • For the 1st tab only, it can't have a nested class/interface w/ that same name within it!
    • For the other tabs, there's an option to have ".java" extension in place of ".pde".
    • But doing so, we lose the luxury of using Processing's API directly on those classes/interfaces.
  • I'm aware (I hope) that each tab is saved as a pde file. I know that when you open the sketches, even from separate folders, processing saves a copy inside the same file as the first tab you opened. I will change MainGame so that the sketch and its class do not have the same name.

    Should I, then, save all subsequent classes as .java? What will allow me to actually run these class files together?

  • edited December 2013

    Should I, then, save all subsequent classes as ".java"?

    I wouldn't do that! Processing's pre-processor doesn't touch tabs w/ ".java" extension!

    Plus, no direct access to the whole Processing's API w/o import and requesting reference from 1st tab;
    which is itself a class which extends PApplet!

    Processing's pre-processor automatically glue all ".pde" tabs together as a single PApplet class! =P~

  • When I change the class name inside the pde which is open first (maingame), I get the error I originally got: "The public type maingame must be defined in its own file."

  • I guess, at this point, I have NO idea how to get processing to use the contents of multiple tabs. I can get processing to open them, but I can't get processing to run them at all.

  • edited December 2013

    For the 1st tab only, it can't have a nested class/interface w/ that same name within it!

    I've already warned ya 'bout that! Do not name a nested class in the 1st tab w/ the same name as that! :-w
    B/c Processing's pre-processor generates a ".java" file using the name of the 1st tab!
    However, the sketch itself is a class w/ the name of the 1st tab!!! And we can't have classes w/ equal names in the same file!

  • I did change the name of the class inside the pde open in the first tab. It is no longer MainGame, which is the name of the pde file-- I changed it from MainGame to maingame, and when that didn't work, to simply game. All of these changes result in the "public type must be defined in its own file" error.

  • edited December 2013

    Are you using any ".java" files inside your app folder?
    For default behavior, they all must be ".pde" files!
    And 1 of them gotta have the same name as the enclosing folder!
    Hit CTRL+ K to open saved app directory!

  • There are no .java files being used here. They are all .pde files.

    I think this may help. Here's the structure of what I'm doing:

    folder: MainGame

    pde files in the folder: MainGame, Asteroid, Laser, Player

    classname: for MainGame, internal class is now called game

    I really appreciate your willingness to answer this question on a Friday night, btw.

  • edited December 2013

    I did a little experiment here: Saved a sketch w/ 2 tabs -> "MainGame.pde" & "Asteroid.pde".
    Inside 1st tab, I've placed a class named Game, and for 2nd tab, a class named Asteroid.
    It compiled & run w/ no problems at all! >-)


    "MainGame.pde":

    void setup() {
      new Game();
      new Asteroid();
    
      exit();
    }
    
    class Game {
    }
    


    "Asteroid.pde":

    class Asteroid {
    }
    

  • I have no doubt that it normally works, really. I just don't know why it isn't working here. I've used processing up until this assignment, which requires separate pde files for each class, easily. I've also written classes that worked with each other, but they were in the same pde file and not separate files.

    If it's not that I'm mixing java and pde files, and the file naming conventions are correct, what else could it be?

  • edited December 2013

    Dunno! Why don't you make another project w/ those 4 tabs w/ their correct names.
    Then, copy/paste each class into its respective tab, starting from last 1.
    And check whether it compiles and runs each time!

  • I did and got the same errors I've been getting: if the classes are public, it tells me they need to be in their own files. If they aren't public, processing stops immediately at the word width in the second tab, Asteroid.

  • Processing's pre-processor inserts keyword public in almost everything which doesn't have an access definition already.
    Try my model above w/ 2 tabs. Only diff., paste your own Asteroid class code, and try to compile.

  • That's what I think I did last time. I opened a new sketch, pasted in the code for MainGame, opened a new tab, pasted in the code for Asteroid, saved and tried to compile.

    Is that what you're asking me to do?

  • Is there some code I can import here that would help? I'm not sure what would be relevant.

  • edited December 2013

    That's not what I advised you! I said to start pasting from last tab, 1 at a time!
    Check my example code in "MainGame.pde": It's almost empty! Only instantiates classes!
    Paste 1 tab class and check whether it compiles! Then go for the backwards next!

  • Okay, using just the class and its constructors (like the example), I made a class for maingame and one for asteroid. It runs.

  • Okay, I found the error. I imported PApplet explicitly in MainGame. Once I got rid of that, the program compiled.

    Thank you for your help.

Sign In or Register to comment.