changing a value in a class from another class?
in
Programming Questions
•
1 year ago
hoping someone can help.
i'm trying to make balls tat spawn from the mouse drag to turn on grid boxes in the same location.
i almost have it working, a ball will enter the area of the square and the square class will draw for that particular area. but rather than only turn on when the ball is in the area. i want the ball to change the life value of the square, so that i can fade the squares out after the ball has left that grid position.
i'm trying to run the function void.live in the square class, this sets the life value to 255. but it doesn't seem to update that particular arrays life value? i'm new to programming so not sure why this doesn't work. any help would be good.
here's the code.
import toxi.geom.*;
//declare
ArrayList Box_Collection;
Square[] Square_Collection;
int rows = 16;
int cols = 9;
float spacing = 1.0;
int value = 0;
int powerup = 1;
void setup() {
size(1280, 720, P3D);
background(0);
noStroke();
smooth();
//initialize
Box_Collection = new ArrayList();
//square background attempt
int total = (rows)*(cols);
Square_Collection = new Square [total];
for (int j = 0; j < rows; j++)
{
for (int i =0; i < cols; i++)
{
Vec3D squarepos = new Vec3D((j*80), (i*80), 0);
Square_Collection[(j*(cols))+i] = new Square(squarepos);
}
}
}
void draw() {
background (0);
for ( int i = 0; i < Square_Collection.length; i++ )
{
Square_Collection[i].display();
}
for (int i =Box_Collection.size()-1; i > 0;i-- ) {
Box mybox = (Box) Box_Collection.get(i);
for ( int j = 0; j < Square_Collection.length; j++ ) {
Square mysquare = Square_Collection[j];
Vec3D offset = new Vec3D (40,40,0);
float distance = ((mysquare.loc).add(offset)).distanceTo(mybox.loc);
if (distance > 0 && distance < 41){
// Square_Collection[j].run();
Square_Collection[j].live();
}
}
mybox.run();
if (mybox.finished()) {
Box_Collection.remove(i);
}
}
}
void mouseDragged() {
value = value + 1;
if (value > powerup) {
value = 0;
Vec3D origin = new Vec3D(mouseX, mouseY, 0);
// A new ball object is added to the ArrayList (by default to the end)
Box_Collection.add(new Box(origin));
}
}
class Box {
//global varibles
float life = 255;
float mousespeedx = (mouseX - pmouseX);
float mousespeedy = (mouseY - pmouseY);
Vec3D loc = new Vec3D (0, 0, 0);
Vec3D speed = new Vec3D ((random(-2,2)+mousespeedx),(random(-2,2)+mousespeedy), 0);
Vec3D grav = new Vec3D (0,0.2,0);
Vec3D Friction = new Vec3D (0.98,0.98,0);
//constructors
Box (Vec3D _loc) {
loc = _loc;
}
void run() {
display();
move();
bounce();
friction();
//gravity();
}
void friction(){
speed.scaleSelf(Friction);
}
void move() {
loc.addSelf(speed);
}
void bounce() {
if (loc.x > width) {
speed.x = speed.x * -1;
}
if (loc.y > height) {
speed.y = speed.y * -1;
}
if (loc.y < 0) {
speed.y = speed.y * -1;
}
if (loc.x < 0) {
speed.x = speed.x * -1;
}
}
void gravity() {
speed.addSelf(grav);
}
void display() {
// translate(posx, posy, 0);
// stroke(255,0,0);
fill(255, life);
noStroke();
ellipse(loc.x, loc.y, 5, 5);
}
boolean finished() {
// Balls fade out
life--;
if (life < 0) {
return true;
}
else {
return false;
}
}
//functions
}
class Square {
int sizex;
int sizey;
int life = 0;
Vec3D loc = new Vec3D (0, 0, 0);
Square (Vec3D _loc) {
loc = _loc;
}
void run() {
display();
}
void live(){
int life = 255;
}
void display() {
fill(150,0,0,life);
stroke(200,0,0);
rect(loc.x, loc.y, 80, 80);
life--;
}
boolean finished() {
// Balls fade out
life--;
if (life < 0) {
return true;
}
else {
return false;
}
}
}
to view the code working sort of how i want un-comment this line
// Square_Collection[j].run();
and comment out this section
for ( int i = 0; i < Square_Collection.length; i++ )
{
Square_Collection[i].display();
}
now a square will appear only when a ball is in the area.
cheers for any help
1