We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hei guys I started to write a code to kind of simulation the Atomic Fission. So I have two classes : on for the Atoms and one for the Neutrons. And I want to use the X and Y ( and a bunch of others) Variables from the Atom class in the Neutron Class. I tried to referr to them with classname.variablename (for example: atoms.atomX) but then there is a failiure code with: the global variable atomX does not exist. So my question is: how can I use a Variable from another class?
My code in the Atom class is:
class Atom {
float atomWid = 60;
float atomHei = atomWid;
float atomX = random((0+atomWid/2), (width-atomWid/2));
float atomY = random((0+atomHei/2), (height-atomHei));
float atomXspeed = random(-2, 2);
float atomYspeed = random(-2, 2);
void show() {
noStroke();
fill(40, 40, 40);
ellipse(atomX, atomY, atomWid, atomHei);
}
void move() {
atomX += atomXspeed;
atomY += atomYspeed;
if ((atomX-atomWid/2) < 0) {
atomXspeed *= -1;
}
if ((atomX+atomWid/2) > width) {
atomXspeed *= -1;
}
if ((atomY-atomHei/2) < 0) {
atomYspeed *= -1;
}
if ((atomY+atomHei/2) > height) {
atomYspeed *= -1;
}
}
}
and my code in the Neutron class is:
class Neutron {
void show() {
noStroke();
fill(255, 10, 10);
ellipse(**atoms.atomX, atoms.atomY, atoms.atomWid, atoms.atomHei**);
}
void move() {
}
}
and here is my setup() and draw() function (i seperated the three codes in different tabs btw):
Atom[] atoms = new Atom[5];
Neutron[] neutrons = new Neutron[2];
void setup() {
size(600, 600);
for (int i=0; i<atoms.length; i++) {
atoms[i] = new Atom();
}
for (int i=0; i<neutrons.length; i++) {
neutrons[i] = new Neutron();
}
}
void draw() {
background(240, 240, 240);
for (int i=0; i<atoms.length; i++) {
atoms[i].show();
atoms[i].move();
}
for ( int i=0; i<neutrons.length; i++) {
neutrons[i].show();
}
}
Sry for the stupid question & bad explanation Im new here :) I really hope someone can help me ... thanks :)
Answers
Btw I dont know why some parts of the code arent in order ... sry for that
To prepare your code for the forum so it looks right, edit your post above, make sure there is a blank line before and after the code, select the code and click on the C button. Then save changes and your code will be formatted.
Im gonna try I didnt know how to do it before sry ;)
You've posted the same question 3 times. I'm going to delete the other two. Please don't do it again.
The problem here is that the variable
atoms
is an array not a singleAtom
object.In your program you are associating a neutron with a particular atom so one solution would be to
1) modify the Neutron: show method to
2) Now in the draw method to
This is not ideal because how do you know which neutrons are associated with which atom?
You need to step back and think about what you are representing. In nuclear fission/fusion neutrons are either part of an atom or moving unattached between atoms. In which case I would modify the neutron class to have a reference to an Atom object.
@koogs thanks you beat me to it.
Thanks a lot Im gonna try to do it in another way ... you really helped me there :)
Given an Atom is made outta Proton + Neutron + Electron, you should go w/ inheritance by composition: *-:)
I appreciate your help, but honestly I dont get this code ... Im just not on your level ;) But thanks anyway :)