Search code examples
javaoopprocessing

How to apply changes to a particular object of the class in Processing?


I have a problem while working with Processing. I have created class "Stone" and then I have created 3 objects Stone stone1, stone2, stone3 in the main scetch. What I need is to make stone transparent when user moves it. The problem is that when the stone is moved, all 3 stones become transparent, not the particular stone that was moved. Can you please give me a hint, how can I apply transparency just for the moved stone?

My class Stone

    class Stone {
    Playfield p;
    PImage stone;
    boolean overStone = false;
    boolean locked = false;
    boolean stoneMoved;
    int positionX;
    int positionY;
    int stoneXOld;
    int stoneYOld;
    int stoneSize;
    boolean transparencyOn;

    Stone(int stoneXtemp, int stoneYtemp, int stoneSizeTemp, Playfield pf) {
        stone = loadImage("stone.png");

        positionX = stoneXtemp;
        positionY = stoneYtemp;
        stoneXOld = positionX;
        stoneYOld = positionY;

        imageMode(CENTER);
        stoneSize = stoneSizeTemp;
        p = pf;
    }

    void display() {
        if (transparencyOn == true) {
            tint(255, 126);
        } else {
            noTint();
        }
        image(stone, stoneX, stoneY, stoneSize, stoneSize);
    }

    void update() {
        if (mouseX > positionX - stoneSize / 2 && mouseX < positionX + stoneSize / 2 &&
                mouseY > positionY - stoneSize / 2 && mouseY < positionY + stoneSize / 2) {
            overStone = true;
            if (!locked) {
                tint(160);
            }
        } else {
            noTint();
            overStone = false;
        }
    }

    void interact() {
        if (overStone && mousePressed) {
            locked = true;
            tint(230);
            positionX = mouseX;
            positionY = mouseY;
        } else {
            locked = false;
            noTint();
        }
    }
    
    boolean stoneMoved() {
        return stoneMoved = (!overStone && !locked && positionX > stoneXOld + 100 && positionY > stoneYOld + 100);
    }

    void afterMove() {
        if (stoneMoved()) {
            p.stoneAfterMove(stoneSize * 2);
        }
        transparencyOn = true;

    }
}

and the playfield:

import processing.video.*;
aaMovie water;
Stone stone1,stone2,stone3;
PImage img;

void setup()
{
size(800,800);
water=new Movie(this,"Water.mp4");
water.loop();

stone1=new Stein(70,70,100,this);
stone2=new Stein(180,70,80,this);
stone3=new Stein(270,70,60,this);

}

void draw(){
background(water);

stone1.display();
stone2.display();
stone3.display();

stone1.update();
stone2.update();
stone3.update();

stone1.interact();
stone2.interact();
stone3.interact();
}

void movieEvent(Movie m){
m.read();
}

void mouseReleased(){
stone1.afterMove();
stone2.afterMove();
stone3.afterMove();
}

Solution

  • The solution is found. I needed global var boolean moved to control the moved stone in both classes and then set the transparencyOn in the right places of code