make the change stay when release the mouse

edited April 2017 in Questions about Code

Hi, I'm new to processing. I want to make an interactive thing that this thing has several pages, and you can change the page by click on different buttons. And in one page called "ground", you can "plant trees" (insert images of tree on the background image). I used " if(mousePressed)" to insert the image where I clicked, but when I released the mouse the image disappeared. How to make the image stay after release the mouse? And can i insert the image more than once? Thank you for help!

PImage open;
PImage groundI;
PImage tree;

String pageState;




void setup()
{

  size(480,800);
  pixelDensity(displayDensity());
  noStroke();

  open = loadImage("01.png");
  groundI = loadImage("ground.png");

  tree=loadImage("tree.png");



  pageState = "page0" ;


}

void draw()
{


  if(pageState == "page0")
  {
    page0();
  }

  else if(pageState == "pageground")
  {
    pageground();
  }

}


void page0() 
{
 image(open,0,0);

 if(mousePressed)
 {
   if(mouseX>30 && mouseX<105 && mouseY>130 && mouseY< 205)
   {
     //++page; 
     pageState = "pageground";
   }
 }
}

 void pageground()
{
  image(groundI,0,0,480,800);


  if(mouseX>0 && mouseX<480 && mouseY>350 && mouseY< 800 && mousePressed )
  {
      image(tree,mouseX,mouseY);
  }

}
Tagged:

Answers

  • Are you inserting the same image? If so, then you need to have an ListArray of PVectors. You can check the reference to get familiar with this feature. Also check the example section in the processing.org website.

    With ListArrays, every time you click, you add a new PVector to the container and then draw will place an image in all the positions stored in the container.

    There is another approach. You call background once and then you place your images one by one after every mouse event. As far as you don't change the page, or you don't call background, all the images will be accumulated in the sketch. However if you are using multiple pages, you will lose all the information if you change pages as I foresee you will need to redraw the whole scene again.

    Kf

Sign In or Register to comment.