#Can't initialise a method from a class to another

1 messages ยท Page 1 of 1 (latest)

wraith pantherBOT
#

<@&987246883653156906> please have a look, thanks.

quartz anvil
#

The classes are in different tabs

soft peak
#

Please format that code

#

You can use triple quotes for that `

#
code
#

And also write java after the quotes so you'll get syntax highlights

#

But back to your question, what do you mean you cant initialize a method?

#

You prob mean on how you can call a method from a different class?

#

You declare the type Mushroom then the name of that object you gonna create and then new Mushroom(); which calls the constructor

#

Now you are able to put a dot after the object and call its method

#

Like this mushroom.draw();

quartz anvil
#

I have tried that but it doesn't take effect, the image doesn't move

#

I tried calling it the way you explained

#

`Drag dragmushroom;
class Mushroom {
int x, y;
int size;
PImage mushroom;
Drag drag1 = new Drag(); //

Mushroom(int x, int y, int size) {
this.x = x;
this.y = y;
this.size = size;
mushroom = loadImage("mushroom.jpg");
dragmushroom = new Drag();
}
void updateMushroom() {
imageMushroom();
mushroomMove();
}
void imageMushroom() {

image(mushroom, x, y, size, size);

}
void mushroomMove() {
dragmushroom.move();
}
}
` java

#

This is the updated mushroom class code

#

`Pizza pizza;
Tomato tomato;
Olive olive;
Mushroom mushroom;

void setup() {
size(600, 600);
background(233);
pizza = new Pizza(600, height - 530);
tomato = new Tomato(100, 500, 60);
olive = new Olive(250, 500, 60);
mushroom = new Mushroom(400, 500, 60);
}

void draw() {
background(233);
imageMode(CENTER);
pizza.update();
pizza.printX();
tomato.update();
olive.update();
mushroom.updateMushroom();
}
` java
And this is the class main code

#

In which constructor should I define int x, int y and int size? In the drag constructor? or the move method?

calm axle
#

do u know on access modifiers ?

quartz anvil
#

No

calm axle
#

private - public - protected etc ?

quartz anvil
#

yes but I'm in processing, I haven't used any of these

#

should using these change it?

calm axle
#

that's why the methods are not showing

quartz anvil
#

so public class drag

#

instead of just class drag>

#

?

calm axle
#

only make public what u want to make public

quartz anvil
#

lemme try real quick

calm axle
#

in Drag u made a private method called move

#

if u want to use that outside of a the Drag class, then u need to make an 'opening ' for acess to it

quartz anvil
#

Would you be able to hop on call and I screen share with you? I have an assignment due tomorrow xD

calm axle
#

i dont really do VC at this time of night ( kiddo ๐Ÿ˜‰ )

quartz anvil
#

Sorry didn't know your time zone

calm axle
#

look on what access modifiers are and u should be able to see why u cant find any methods

#

im mid europe, my kiddo is sleeping atm so aint going to wake her ๐Ÿ˜‰

quartz anvil
#

Nono that's fine

#

I can use your help here still

#

On text

wraith pantherBOT
calm axle
#

( used to be a drawing in here, so u need to click the image to see bigger -_- )

quartz anvil
#

`public class Mushroom {
int x, y;
int size;
PImage mushroom;
Drag dragMushroom;

Mushroom(int x, int y, int size) {
this.x = x;
this.y = y;
this.size = size;
mushroom = loadImage("mushroom.jpg");
dragMushroom = new Drag();
}
void updateMushroom() {
imageMushroom();
moveMushroom();
}
void imageMushroom() {

image(mushroom, x, y, size, size);

}
void moveMushroom() {
dragMushroom.move();
}
}` java

#

Changed it like this

#

What am I missing?

calm axle
#

its 3x ` java at beginning, sec let me repaste your code

#
public class Mushroom {
  int x, y;
  int size;
  PImage mushroom;
  Drag dragMushroom;

  Mushroom(int x, int y, int size) {
    this.x = x;
    this.y = y;
    this.size = size;
    mushroom = loadImage("mushroom.jpg");
    dragMushroom = new Drag();
  }
  void updateMushroom() {
    imageMushroom();
    moveMushroom();
  }
  void imageMushroom() {

    image(mushroom, x, y, size, size);
  }
  void moveMushroom() {
    dragMushroom.move();
  }
} 
wraith pantherBOT
quartz anvil
#
  int x, y;
  int size;

  Drag() {
    
  }

  void move() {
    if ( dist(mouseX, mouseY, x, y) < size/2) { //mouse is inside the circle
      if (mousePressed) {         //mouse is inside the circle and clicked
        x = mouseX;    //change circle position to mouse position
        y = mouseY;
      }
    }
  }
}```
wraith pantherBOT
# quartz anvil ```public class Drag { int x, y; int size; Drag() { } void move...

Detected code, here are some useful tools:

Formatted code
public class Drag {
  int x, y;
  int size;
  Drag() {
  }
  void move() {
    if (dist(mouseX, mouseY, x, y) < size / 2) {
      //mouse is inside the circle
      if (mousePressed) {
        //mouse is inside the circle and clicked
        x = mouseX;
        //change circle position to mouse position
        y = mouseY;
      }
    }
  }
}
quartz anvil
#

this is the drag class with drag public too

calm axle
#

right, question, how is drag suppose to know the coordinates ?

quartz anvil
#

so it needs constructor?

#

constructor defined?

calm axle
#

not neceseeary

#

il repeat my question. How is drag suppose to know the coordinates ?

quartz anvil
#

Idk

calm axle
#

at the moment u got no data in drag what so over

quartz anvil
#

If I paste the move method in the mushroom class without calling it, just paste it, it works

calm axle
#

because the variable name is in there, it will work

#

however when u seperate Drag, u need to make a way to communicate

quartz anvil
#

How do I do that?

calm axle
#

u can make setters, make the variables accessible, or constructor

#

few more but as this is already unknown, i wont mention them

quartz anvil
#

Yea very new to coding so this is fine for now

calm axle
#

do u know what a setter / getter is ?

quartz anvil
#

nope

#

.set and .get?

wraith pantherBOT
# quartz anvil .set and .get?

Looks like you attempted to use a command? Please note that we only use slash-commands on this server ๐Ÿ™‚

Try starting your message with a forward-slash / and Discord should open a popup showing you all available commands.
A command might then look like /foo ๐Ÿ‘

quartz anvil
#

Idk how to use it

calm axle
#

righto, a setter can set some variable's content and a getter can read that content

quartz anvil
#

how do i use them in this case?

calm axle
#

at the moment, i'm going to say something that u might not want to accept. But drop the game development and learn some more basic java and coding

#

its going to save u hours and hours of frustration

quartz anvil
#

you're right, it's part of my assignments for learning java, game is just like a little project

calm axle
#

u kinda need the knowledge to make something bigger then 5 methods

quartz anvil
#

true true

calm axle
#

for now a setter is something like this :

#
public class Drag(){

int xCoord;
int yCoord;

public Drag();

public void setxCoordinate(int x){
  this.xCoord=x;
}

....

}
wraith pantherBOT
calm axle
#

basicly as your not exposing ( good practise not to ) your variable, u can use a setter method to set its value from another class

quartz anvil
#

so set in drag and get it from mushroom?

calm axle
#

that way your shielding the actual variable from external access, but u can set or change its value

calm axle
quartz anvil
#

so I need to tell drag where the object is, is this what you mean?

calm axle
#
if (dist(mouseX, mouseY, x, y) < size / 2) 
#

what would be the result -> at the moment if u place the object at 50/100 and not share the coordinates to Drag ?

#

dist(0 , 0, 0, 0 ) < size / 2

quartz anvil
#

okay okay

calm axle
#

( also your code shoudlnt compile unles u omitted sections )

quartz anvil
#
Tomato tomato;
Olive olive;
Mushroom mushroom;

void setup() {
  size(600, 600);
  background(233);
  pizza = new Pizza(600, height - 530);
  tomato = new Tomato(100, 500, 60);
  olive = new Olive(250, 500, 60);
  mushroom = new Mushroom(400, 500, 60);
  
}

void draw() {
  background(233);
  imageMode(CENTER);
  pizza.update();
  pizza.printX();
  tomato.update();
  olive.update();
  mushroom.updateMushroom();
}``` java
wraith pantherBOT
# quartz anvil ```Pizza pizza; Tomato tomato; Olive olive; Mushroom mushroom; void setup() { ...

Detected code, here are some useful tools:

Formatted code
Pizza pizza;
Tomato tomato;
Olive olive;
Mushroom mushroom;
void setup() {
  size(600, 600);
  background(233);
  pizza = new Pizza(600, height - 530);
  tomato = new Tomato(100, 500, 60);
  olive = new Olive(250, 500, 60);
  mushroom = new Mushroom(400, 500, 60);
}
void draw() {
  background(233);
  imageMode(CENTER);
  pizza.update();
  pizza.printX();
  tomato.update();
  olive.update();
  mushroom.updateMushroom();
}
quartz anvil
#

This is the main class

calm axle
#

what program are u using to write code ?

quartz anvil
#

processing

calm axle
#

does that mark faults aswell or just executes ?

quartz anvil
#

it marks faults but basic

#

it wouldn't tell me that nothing is being communicated to drag

#

but syntax errors yes

calm axle
#

that its should have a field day there ๐Ÿ˜‰

#

mouseX , mouseY

#

both no where defined

#

x / y uninitialized

#

etc

quartz anvil
#

Does this make sense? This is how I see it in processing

calm axle
#

yeah its not detecting. the first mouseX u see. Do u see any definition for that ?

quartz anvil
#

The system variable mouseX always contains the current horizontal coordinate of the mouse.

Note that Processing can only track the mouse position when the pointer is over the current window. The default value of mouseX is 0, so 0 will be returned until the mouse moves in front of the sketch window. (This typically happens when a sketch is first run.) Once the mouse moves away from the window, mouseX will continue to report its most recent position.

calm axle
#

not what im asking

#

your using a variable mouseX

#

how would java know what it is ?

#

all u have is a variable called x and y

#

no mouseX defined anywhere

#

this is a shot from intellij

quartz anvil
#

I think I don't need to define mouseX or mouseY in processing? it's automatically defined?

#

or it would give me an error I think

calm axle
#

at the moment , it might be better to have a quick glance on the basics.

quartz anvil
#

Okay, what do you suggest?

calm axle
#

your almost there, you're lacking a small click

quartz anvil
#

I have added setters to the code

#
  int x, y;
  int size;

  Drag() {
    
  }
  void setX(int x) {
    this.x = x;
  }

  void setY(int y) {
    this.y = y;
  }

  void setSize(int size) {
    this.size = size;
  }
  void move() {
    if ( dist(mouseX, mouseY, x, y) < size/2) { //mouse is inside the circle
      if (mousePressed) {         //mouse is inside the circle and clicked
        x = mouseX;    //change circle position to mouse position
        y = mouseY;
      }
    }
  }
}``` updated drag class
wraith pantherBOT
# quartz anvil ```public class Drag { int x, y; int size; Drag() { } void setX(...

Detected code, here are some useful tools:

Formatted code
public class Drag {
  int x, y;
  int size;
  Drag() {
  }
  void setX(int x) {
    this .x = x;
  }
  void setY(int y) {
    this .y = y;
  }
  void setSize(int size) {
    this .size = size;
  }
  void move() {
    if (dist(mouseX, mouseY, x, y) < size / 2) {
      //mouse is inside the circle
      if (mousePressed) {
        //mouse is inside the circle and clicked
        x = mouseX;
        //change circle position to mouse position
        y = mouseY;
      }
    }
  }
}
quartz anvil
#
  int x, y;
  int size;
  PImage mushroom;
  Drag dragMushroom;

  Mushroom(int x, int y, int size) {
    this.x = x;
    this.y = y;
    this.size = size;
    mushroom = loadImage("mushroom.jpg");
    dragMushroom = new Drag();
    dragMushroom.setX(x);
    dragMushroom.setY(y);
    dragMushroom.setSize(size);
  }
  void updateMushroom() {
    imageMushroom();
    moveMushroom();
  }
  void imageMushroom() {

    image(mushroom, x, y, size, size);
  }
  void moveMushroom() {
    dragMushroom.move();
  }
}
``` updated mushroom class
wraith pantherBOT
# quartz anvil ```public class Mushroom { int x, y; int size; PImage mushroom; Drag dra...

Detected code, here are some useful tools:

Formatted code
public class Mushroom {
  int x, y;
  int size;
  PImage mushroom;
  Drag dragMushroom;
  Mushroom(int x, int y, int size) {
    this .x = x;
    this .y = y;
    this .size = size;
    mushroom = loadImage("mushroom.jpg");
    dragMushroom = new Drag();
    dragMushroom.setX(x);
    dragMushroom.setY(y);
    dragMushroom.setSize(size);
  }
  void updateMushroom() {
    imageMushroom();
    moveMushroom();
  }
  void imageMushroom() {
    image(mushroom, x, y, size, size);
  }
  void moveMushroom() {
    dragMushroom.move();
  }
}
calm axle
#

a constructor is most of the times public

#

so try to place it to avoid confusion

quartz anvil
#

the drag class constructor you mean?

calm axle
#

yeah, its not needed. But for readabilty it makes wonders

quartz anvil
#

Drag(int x, int y, int size)

#

like this?

calm axle
#

atm u got

quartz anvil
#

yea

calm axle
#

add public on the constructor for readability

quartz anvil
#

done, doesn't make any difference

calm axle
#

( in 2 weeks even your own written code is gibberish so try to maintain readability )

brave harness
#

so there is no point at using it

#

@quartz anvil

quartz anvil
#

How do you suggest I make this work?

#

Do you see any mistakes?

brave harness
#

you are doing things a bit in a reverse way

#

the way you are doing it currently

quartz anvil
#

Ok I'm listening

brave harness
#

is that you are trying to call move each frame which will then check if the mouse is pressed to then move

#

instead

#

you should only call move when the mouse is pressed and move is a simple setter

quartz anvil
#

Look at this

#

If you're wondering why I can move the other 2 toppings, the code is directly in the class

brave harness
#

like I said, it's not the olive, or whatever drag is to do such a thing

quartz anvil
#

So?

brave harness
#

it would be in Factory Game

#

you create a void mousePressed() method

#

and inside, you move whatever got clicked