Loading...
Logo
Processing Forum

Code changing

in Programming Questions  •  2 years ago  
Is there any way to change code during the run of the sketch? My problem is that I need to get change a level for a game. Levels are stored in a class called "Level". So, to bring a new level, say level 1, I go level.level1   And that will spawn the new level. What I want to be able to do is to change the "1" at the end of level.level1 so I can switch levels. Any other code corrections would be nice too, though! Code:

Copy code
  1. import fisica.*;
  2. import toxi.geom.*;
  3. import toxi.math.*;
  4. import toxi.geom.mesh2d.*;

  5. FWorld world;
  6. FPoly tank, gun;
  7. Tank player;
  8. Level level;
  9. /**
  10.   .setStroke(173, 216, 199);
  11.   .setFill(22, 147, 165);
  12.   */


  13. float tankX, tankY, tankXvelocity, tankYvelocity = 0;

  14. void setup() {
  15.   size(1024, 512, P2D);
  16.   background(22, 147, 165);
  17.   Fisica.init(this);
  18.   world = new FWorld();
  19.   world.setEdges();
  20.   world.setEdgesFriction(20);
  21.   stroke(0);
  22.   noFill();
  23.   world.setGrabbable(false);
  24.   player = new Tank(100, height-40);
  25.   winball(40,100,100);
  26.   level = new Level();
  27.   
  28. }

  29. void draw() {
  30.   background(22, 147, 165);
  31.   if ( keyPressed) {
  32.    if ( key == 'd' || key == 'D' ) {
  33.      tank.adjustVelocity(20,0);
  34.    } else if ( key == 'a' || key == 'A' ) {
  35.      tank.adjustVelocity(-20,0);
  36.    }
  37.   }
  38.   world.step();
  39.   world.draw(this);
  40. }
  41. class Tank{
  42.   int spawnX, spawnYd;
  43.   void Tank(int xSpawn, int ySpawn) {
  44.     tank = new FPoly();
  45.     tank.setStroke(173, 216, 199);
  46.     tank.setFill(22, 147, 165);
  47.     tank.vertex(xSpawn, ySpawn);
  48.     tank.vertex(xSpawn+20, ySpawn);
  49.     tank.vertex(xSpawn+30, ySpawn+5);
  50.     tank.vertex(xSpawn+25, ySpawn+10);
  51.     tank.vertex(xSpawn, ySpawn+10);
  52.     tank.vertex(xSpawn-5, ySpawn+5);
  53.     tank.vertex(xSpawn-3, ySpawn+2);
  54.     world.add(tank);
  55.   }
  56. }

  57. void winball(  int sizeOf, int Xball, int Yball);
  58.    FBlob winball = new FBlob();
  59.    winball.setStroke(173, 216, 199);
  60.    winball.setFill(22, 147, 165);
  61.    winball.setAsCircle(Xball,Yball,sizeOf);
  62.    world.add(winball);
  63. }
EDIT: Having trouble with it again. It is highlighting line 66 and giving the message "unexpected token: ("     Any ideas?

Replies(3)

Re: Code changing

2 years ago
Replace the semi-colon at the end of the definition of winball with an opening brace...

And you are taking the wrong side of the problem. In Java/Processing, code is compiled, and cannot be changed easily. Actually, in most programming languages I know, you won't handle the problem this way!

It is hard to give a precise answer to your question, as you didn't include the code for the Level class. But you might be able to pass a constructor parameter giving the current level, for example. Or just use a member variable to handle it.

Re: Re: Code changing

2 years ago
Alright, this is the updated code:   
Copy code
  1. import fisica.*;
  2. import toxi.geom.*;
  3. import toxi.math.*;
  4. import toxi.geom.mesh2d.*;

  5. FWorld world;
  6. FPoly tank, gun;
  7. Tank player;
  8. /**
  9.   .setStroke(173, 216, 199);
  10.   .setFill(22, 147, 165);
  11.   */


  12. float tankX, tankY, tankXvelocity, tankYvelocity = 0;
  13. boolean menu = true;

  14. void setup() {
  15.   size(1024, 512, P2D);
  16.   background(22, 147, 165);
  17.   Fisica.init(this);
  18.   world = new FWorld();
  19.   world.setEdges();
  20.   world.setEdgesFriction(20);
  21.   stroke(0);
  22.   noFill();
  23.   world.setGrabbable(false);
  24.   player = new Tank(100,200);
  25.   winball(40,100,100);
  26.   level = new Level();
  27.   
  28. }

  29. void draw() {
  30.   background(22, 147, 165);
  31.   if ( keyPressed) {
  32.    if ( key == 'd' || key == 'D' ) {
  33.      tank.adjustVelocity(20,0);
  34.    } else if ( key == 'a' || key == 'A' ) {
  35.      tank.adjustVelocity(-20,0);
  36.    }
  37.   }
  38.   if(menu == true){
  39.     
  40.   } else {
  41.     world.step();
  42.     world.draw(this);
  43.   }
  44. }
  45. class Tank{
  46.   void Tank(int xSpawn, int ySpawn){
  47.     tank = new FPoly();
  48.     tank.setStroke(173, 216, 199);
  49.     tank.setFill(22, 147, 165);
  50.     tank.vertex(xSpawn, ySpawn);
  51.     tank.vertex(xSpawn+20, ySpawn);
  52.     tank.vertex(xSpawn+30, ySpawn+5);
  53.     tank.vertex(xSpawn+25, ySpawn+10);
  54.     tank.vertex(xSpawn, ySpawn+10);
  55.     tank.vertex(xSpawn-5, ySpawn+5);
  56.     tank.vertex(xSpawn-3, ySpawn+2);
  57.     world.add(tank);
  58.   }
  59. }


  60. void winball(int sizeOf, int Xball, int Yball){
  61.    FBlob winball = new FBlob();
  62.    winball.setStroke(173, 216, 199);
  63.    winball.setFill(22, 147, 165);
  64.    winball.setAsCircle(Xball,Yball,40);
  65.    world.add(winball);
  66.    
  67. }
 

Now it just keeps coming up with "The constructor VT74.Tank(int, int) is undefined". Any ideas? Also, could you explain the terms you used? Still new to programming, so must learn where I can :)

Re: Code changing

2 years ago
A constructor is a special function of same name than the class, taking parameters (or not), and called when you do new SomeClass(someParameter).
Here, you have preceded the name of the constructor with void, which makes it an ordinary function. Remove the void to promote it back to constructor status...
Your class is quite useless, actually, as it has no member variables (tank, tankX and similar variables could go there). It can be replaced by a simple function.

Have you read the tutorials on Objects in the Learning section of Processing?