We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I'm having trouble troubleshooting errors in my code. Processing says that there is an "unexpected token: void" but I can't seem to find the problem. It point to line 47 in the following code but I'm not sure why it's a problem.
//First declare objects used for this session
Blocker obstacleBlocktopdown;
Blocker obstacleBlockacross;
Avatar playerAvatar;
float move = 1;//?? needed?
//Player Avatar;
//Player Seeker;
boolean upPressed = false;
boolean downPressed = false;
boolean leftPressed = false;
boolean rightPressed = false;
void setup(){
size(1300, 900);
//Initialize object here
obstacleBlocktopdown = new Blocker(width/2, 1, 20, 20, 0, 2);
obstacleBlockacross = new Blocker(1, height/2, 20, 20, 3, .4);
playerAvatar = new Avatar(0, 0, 15, 15);
//Avatar = new Player(color(255,0,0),10,100,2);
//Seeker = new Player(color(0,0,255),100,10,1);
}
void draw(){
if (upPressed) {// playerAvatar moves up 1 pixel
playerAvatar.movesup();
}
if (downPressed) {//playerAvatar moves down 1 pixel
playerAvatar.movesdown();
}
if (leftPressed) {//playerAvatar moves to the left 1 pixel
playerAvatar.movesleft();
}
if (rightPressed) {//playerAvatar moves to the right 1 pixel
playerAvatar.movesright();
}
background(255);
//obstacleBlocktopdown.display();
//obstacleBlockacross.display();
obstacleBlocktopdown.run();
obstacleBlockacross.run();
playerAvatar.display();
//enacts the movement of desired object
void keyPressed (KeyEvent e) {
if (key == CODED) {
if (keyCode == UP) {
upPressed = true;
}
else if (keyCode == DOWN) {
downPressed = true;
}
else if (keyCode == LEFT) {
leftPressed = true;
}
else if (keyCode == RIGHT) {
rightPressed = true;
}
}
}
// stops movement once key(s) are released
void keyReleased(KeyEvent e) {
if (key == CODED) {
if (keyCode == UP) {
upPressed = false;
}
else if (keyCode == DOWN) {
downPressed = false;
}
else if (keyCode == LEFT) {
leftPressed = false;
}
else if (keyCode == RIGHT) {
rightPressed = false;
}
}
}
/* Avatar.drive();
Avatar.display();
Seeker.drive();
Seeker.display();*/
}
//I don't think my other classes are creating the problem but here they are for reference.
class Avatar {
float x;
float y;
float a;
float b;
//constructors
Avatar(){
}
Avatar (float _x, float _y, float _a, float _b) {
x = _x;
y = _y;
a = _a;
b = _b;
}
//Functions
void display () {
stroke(0);
fill(83, 125, 243);
ellipse(x, y, a, b);
}
void movesup(){
y = y - 1;
playerAvatar.display();
}
void movesdown() {
y = y + 1;
playerAvatar.display();
}
void movesleft() {
x = x - 1;
playerAvatar.display();
}
void movesright() {
x = x + 1;
playerAvatar.display();
}
}
// ANOTHER CLASS
class Blocker {
//Global variables
float x = 0;
float y = 0;
float a = 0;
float b = 0;
float speedX = 0;
float speedY = 0;
//Constructors
Blocker() {
}
Blocker(float _x, float _y, float _a, float _b, float _c, float _d) {
x=_x;
y=_y;
a=_a;
b=_b;
// speedX = random(-4, 4);
//speedY = random(-4, 4);
speedX = _c;
speedY = _d;
}
//Functions
void display () {
ellipse (x, y, a, b);
}
void movement(float spX, float spY) {//make blockers move in basic motions
x = x + speedX;
y = y + speedY;
}
void bounce() {
if (x > width) {
speedX = speedX * -1;
}
if (x < 0) {
speedX = speedX * -1;
}
if (y > height) {
speedY = speedY * -1;
}
if (y < 0) {
speedY = speedY * -1;
}
}
void run() {
//display();
obstacleBlocktopdown.movement(0, 2);
obstacleBlockacross.movement(3, .4);
bounce();
obstacleBlocktopdown.display();
obstacleBlockacross.display();
}
}
Any help will be appreciated.
Answers
Ctrl-t
Where does your draw() function end?
KeyPressed and KeyReleased should be outside of the draw() function.
Yes that works. Those methods shouldn't be in my draw() function. Thank you
One more question though, I'm trying to make constraints for the playerAvatar object so it doesn't go off screen. Here is the code I implemented. I hope I'm using the constrain method correctly...
I get an error saying "The function constrain(float, int, int) does not exist. I'm really new at this so it doesn't make much sense to me. I thought the syntax for the constrain() function was constrain(amt, low, high) where the amt refers to the value to be constrained? And it can return an int or float?
When we use the dot
.
operator, Java considers its right operand as a member of the left operand's object:https://processing.org/reference/dot.html
playerAvatar.constrain(playerAvatar.x, 10, width-10);
.
access operator and represents the object.playerAvatar.x
. Only diff. is that the member x actually exists there!https://processing.org/reference/constrain_.html
Much probably what you meant was assigning the result of constrain() back to field x: ;;)
playerAvatar.x = constrain(playerAvatar.x, 10, width-10);
It'd be much better if that was an Avatar's method instead: *-:)
Also take a look at my online example below:
http://studio.processingtogether.com/sp/pad/export/ro.91tcpPtI9LrXp/
The class Player there also relies on constrain() @ its member method move()! :bz
Hi, "GoToLoop", there is a slight problem with the constrainToScreen method. It makes the left and right buttons make the playerAvatar go on a diagonal and the moveup() and movedown() methods don't work. I'm trying to tweak it but I'm still running into problems.
And on an unrelated note, I can't control the color of some objects. I wanted to change the color of the Avatar object but it changed the color of all objects on screen. Why is that? The code is below...
// THE BLOCKER CLASS
// SETUP AND DRAW FUNCTIONS.
Why do all my objects change color?
Ok so I fixed the problem with the diagonal movement. (it turned out to be really simple, but I can't seem to get the colors right. Why do all my objects change to the same color?
color
!color
.I see that inside Avatar's display() method both stroke() & fill() are issued:
color
for Avatar. So it's a constant!static final color INK = #008000, OUTLINE = 0;
color
properties.color
, you're gonna need regularcolor
fields to store it.boolean
fields.boolean
fields.