#Can't initialise a method from a class to another
1 messages ยท Page 1 of 1 (latest)
The classes are in different tabs
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();
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?
do u know on access modifiers ?
No
private - public - protected etc ?
yes but I'm in processing, I haven't used any of these
should using these change it?
that's why the methods are not showing
only make public what u want to make public
lemme try real quick
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
Would you be able to hop on call and I screen share with you? I have an assignment due tomorrow xD
i dont really do VC at this time of night ( kiddo ๐ )
Sorry didn't know your time zone
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 ๐
( used to be a drawing in here, so u need to click the image to see bigger -_- )
`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?
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();
}
}
Detected code, here are some useful tools:
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;
}
}
}
}```
Detected code, here are some useful tools:
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;
}
}
}
}
this is the drag class with drag public too
right, question, how is drag suppose to know the coordinates ?
Idk
at the moment u got no data in drag what so over
If I paste the move method in the mushroom class without calling it, just paste it, it works
because the variable name is in there, it will work
however when u seperate Drag, u need to make a way to communicate
How do I do that?
u can make setters, make the variables accessible, or constructor
few more but as this is already unknown, i wont mention them
Yea very new to coding so this is fine for now
do u know what a setter / getter is ?
Idk how to use it
righto, a setter can set some variable's content and a getter can read that content
how do i use them in this case?
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
you're right, it's part of my assignments for learning java, game is just like a little project
u kinda need the knowledge to make something bigger then 5 methods
true true
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;
}
....
}
Detected code, here are some useful tools:
basicly as your not exposing ( good practise not to ) your variable, u can use a setter method to set its value from another class
so set in drag and get it from mushroom?
that way your shielding the actual variable from external access, but u can set or change its value
no idea. 1 problem at a time. Your drag method has got no info what so ever on the coordinates of the object, so u need to communicate to that
so I need to tell drag where the object is, is this what you mean?
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
okay okay
( also your code shoudlnt compile unles u omitted sections )
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
Detected code, here are some useful tools:
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();
}
This is the main class
what program are u using to write code ?
processing
does that mark faults aswell or just executes ?
it marks faults but basic
it wouldn't tell me that nothing is being communicated to drag
but syntax errors yes
that its should have a field day there ๐
mouseX , mouseY
both no where defined
x / y uninitialized
etc
yeah its not detecting. the first mouseX u see. Do u see any definition for that ?
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.
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
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
at the moment , it might be better to have a quick glance on the basics.
Okay, what do you suggest?
your almost there, you're lacking a small click
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
Detected code, here are some useful tools:
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;
}
}
}
}
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
Detected code, here are some useful tools:
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();
}
}
the drag class constructor you mean?
yeah, its not needed. But for readabilty it makes wonders
yea
add public on the constructor for readability
done, doesn't make any difference
( in 2 weeks even your own written code is gibberish so try to maintain readability )
public does nothing in processing
so there is no point at using it
@quartz anvil
Ok I'm listening
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
Look at this
If you're wondering why I can move the other 2 toppings, the code is directly in the class
like I said, it's not the olive, or whatever drag is to do such a thing
So?