Confusion on classes and objects

Hello! I just started using classes in my processing games(as in, this is my first attempt). I am incredibly lost as to how to do it correctly. I looked at the processing 3 language reference and it doesn’t have much on classes.

In my head, the following code should work to create an enemy that moves around the screen as a red rectangle. However, when I press run, all the variable declarations for enemy(E1.x = 57; and so on) get an error and say I should consider adding variabledeclaratorId. I could find nothing on this on the internet and so I assume it is simply a poorly worded error message. Could someone please fix this, and help me find references for classes in processing? The syntax and where to put the statements(setup, draw, or in-between?) has me lost.

void setup()
{
   size(200, 200);
}

Enemy E1 = new Enemy;
E1.x = 57;//x position
E1.y = 98;//y position
E1.w = 20;//width
E1.h = 30;//height
E1.speedX = 7;
E1.speedY = -12;

void draw()
{
   background(0);

   //update enemy position
   E1.x += E1.speedX;
   E1.y += E1.speedY;

   fill(255, 0, 0);
   rect(E1.x, E1.y, E1.w, E1.h);//draw enemy
}

class Enemy
{
   int x, y, speedX, speedY, w, h;
}
1 Like

Welcome to the forum.

One of the best tools in a programmers tool chest is knowing the resources available to you and learning to navigate, filter and use them.

This is a very short list:

Resources < Click here to expand !

I encourage you to review the resources available here:

Check out the Processing tutorials and the Coding Train.

https://processing.org/tutorials/objects/

There is also the Java documentation:
https://docs.oracle.com/javase/tutorial/java/javaOO/index.html

:)

2 Likes

Hello @Edgard142ofWelshdale

This playlist will give you a good foundation in classes and objects.

:nerd_face:

3 Likes