We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › unexpected token: void
Page Index Toggle Pages: 1
unexpected token: void (Read 647 times)
unexpected token: void
Feb 16th, 2010, 2:43am
 
Hey guys.

Ive got a problem with my code. I tried to implent a class for the first time, followed the tutorial featured on this side but im always getting the "unexpected token: void" error (marked in code). Sorry for posting my whole sourcecode, but i cant tell wich part of it is crucial to solve the problem.

Code:

import fullscreen.*;
import japplemenubar.*;

FullScreen fs;

// ROCKETS
PImage Rocket1;
PImage Rocket2;
PImage Shot;
float speedRocket = 0.3;
float inertia = 0.98;
float accX1 = 0;
float accY1 = 0;
float accX2 = 0;
float accY2 = 0;
float xLeft1 = 0;
float xRight1 = 0;
float yUp1 = 0;
float yDown1 = 0;
float xLeft2 = 0;
float xRight2 = 0;
float yUp2 = 0;
float yDown2 = 0;
float dir1 = 0;
float dir2 = 0;
float xPos1 = 100;
float yPos1 = screen.height-100;
float xPos2 = 500;
float yPos2 = 450;
boolean left1 = false;
boolean right1 = false;
boolean space1 = false;
boolean down1 = false;

// SHOT
Shot = shot1;
float shotSpeed = 30;

void setup() {            <-------  unexpected token: void
 size(screen.width, screen.height);
 frameRate(60);
 fs = new FullScreen(this);
 fs.enter();
 smooth();
 noStroke();

 // LOADING IMAGES

 Rocket1 = loadImage("rocketP1.gif");
 Rocket2 = loadImage("rocketP2.gif");
 Shot = loadImage("shot.gif");
 
 shot1 = new Shot(30, 400, 500, 30);
}
// CONTROLS
void keyPressed() {
 if (key == CODED) {
   if (keyCode == UP)
     space1 = true;
   if(keyCode == DOWN)
     down1 = true;
   if(keyCode == LEFT)
     left1 = true;
   if(keyCode == RIGHT)
     right1 = true;
   //if(keyCode== ALT)
     
 }
}
void keyReleased() {
 if (key == CODED) {
   if (keyCode == UP) {
     space1 = false;
   }
   if(keyCode == DOWN) {
     down1 = false;    
   }
   if(keyCode == LEFT) {
     left1 = false;
   }
   if(keyCode == RIGHT) {
     right1 = false;
   }
 }
}



void controls() {

 if (left1)
   dir1 -= 3;
 if(right1)
   dir1 += 3;

 // MOVEMENT
 if (space1){
   accX1 += speedRocket * sin(radians(dir1));
   accY1 += -speedRocket * cos(radians(dir1));
 }

 accX1 *= inertia;
 accY1 *= inertia;

 // SPLITTING UP ACCELERATION INTO POSITIV/NEGATIV
 if(accX1 >= 0)
   xRight1 = accX1;
 if (accX1 < 0)
   xLeft1 = accX1;
 if (accY1 >= 0)
   yDown1 = accY1;
 if (accY1 < 0)
   yUp1 = accY1;

 // SCREENBORDER ACCELERATION
 if (xPos1 <= screen.width)
   xPos1 += xRight1;
 if (xPos1 >= 0)
   xPos1 += xLeft1;
 if (yPos1 <= screen.height)
   yPos1 += yDown1;
 if (yPos1 >= 0)
   yPos1 += yUp1;
}

void display() {
 // DRAWING ROCKETS
 pushMatrix();
 translate(xPos1, yPos1);
 rotate(radians(dir1));
 image(Rocket1, -Rocket1.width/2, -Rocket1.height/2);
 popMatrix();
 image(Rocket2, xPos2, yPos2);
 shot1.display();
}

void draw() {
 background(0);
 controls();
 display();
}

class Shot {

 float dir;
 float x;
 float y;
 float speed;

 Shot(float tempDir, float tempX, float tempY, float tempSpeed) {
   dir = tempDir;
   x = tempX;
   y = tempY;
   speed = tempSpeed;
 }

 void display() {
   pushMatrix();
   translate(x, y);
   rotate(radians(dir));
   image(Shot, -Shot.width/2, -Shot.height/2);
   popMatrix();
 }
}











Re: unexpected token: void
Reply #1 - Feb 16th, 2010, 3:10am
 
The main problem is this line:

Code:
Shot = shot1; 



You declare a class object in just the same way as any built-in class:

Code:
Shot shot1; 



I don't have the fullscreen library so can't test the code, but I would make a couple of other suggestions:

1.  "PImage Shot;" - I'd be uneasy about giving a variable the same name as my class; probably more an issue of style, though of course your Shot declaration highlighted above is actually assigning the value of 'shot1' (which doesn't yet exist) to the PImage 'Shot'...

Edited:
Actually since I mention style I'd also suggest avoiding capitals on variable names.  Now this is to some extent a matter of taste, but there are established conventions - and these themselves change from one language to another - and in Java capitals are quite often only assigned to class names, and not variables or methods (source).


2.  I might be tempted to declare all the variables as empty before setup and then populate them in an init() function called from setup.  One major advantage of this is that when you want to start a new game you just call this function to reset all the variables to their starting values.
Page Index Toggle Pages: 1