Help with a project, beginner question

edited October 2013 in Questions about Code

Hi, I'm working on a project thats a program which has a picture in the background and allow the user to cycle through objects, resize them and place them on the screen. (such as mustaches on people, monocles, glasses). I've added many diff objects to be added to the pictures and it cycles through them fine by pressing 1-4, q,w for now. You can resize the objects with the scroll wheel. It makes them stick on mouse click. However after it sticks one object, how can I make it go back and start cycling through objects again so users can add more stuff? After mouse is clicked the image sticks but then it doenst do anything.

I'm sorry if this is confusing, I am new to programming and maybe didnt explain myself the best way. Please ask me any questions for clarification. Also if just looking at my code isnt enough let me know and i will upload project files. Thanks in advance

PImage stache;
PImage stache2;
PImage stache3;
PImage stache4;
PImage monocle;
PImage glasses;
float x;
float y;
float stacheWidth;
float stacheHeight;
float stache2Width;
float stache2Height;
float stache3Width;
float stache3Height;
float stache4Width;
float stache4Height;
float stacheScale;
float glassesScale;
float monocleWidth;
float monocleHeight; 
float glassesWidth;
float glassesHeight;
boolean imageDropped;
int k;


void setup() {
  size(640, 800);
  imageMode(CENTER);
  stache = loadImage("mustache.png");
  stache2 = loadImage("stache2.png");
  stache3 = loadImage("stache3.png");
  stache4 = loadImage("stache4.png");
  monocle = loadImage("monocle.png");
  glasses = loadImage("glasses.png");
  stacheWidth = stache.width;
  stacheHeight = stache.height;
  stache2Width = stache2.width;
  stache2Height = stache2.height;
  stache3Width = stache3.width;
  stache3Height = stache3.height;
  stache4Width = stache4.width;
  stache4Height = stache4.height;
  monocleHeight = monocle.height;
  monocleWidth = monocle.width;
  glassesHeight = glasses.height;
  glassesWidth = glasses.width;
  imageDropped = false;
  stacheScale = .8;
  glassesScale = .9;
  k = 0;

}

void draw() {
  background(loadImage("obama.jpeg"));
  if (k == 1){
  if (imageDropped == true ) {
    image(stache, x, y, stacheWidth, stacheHeight);
    noLoop();

  }
  else {
    image(stache, mouseX, mouseY, stacheWidth, stacheHeight);
  }

    }



  if (k == 2){
  if (imageDropped == true ) {
    image(stache2, x, y, stache2Width, stache2Height);
  }
  else {
    image(stache2, mouseX, mouseY, stache2Width, stache2Height);
  }

  }
  if (k == 3){
  if (imageDropped == true ) {
    image(stache3, x, y, stache3Width, stache3Height);
  }
  else {
    image(stache3, mouseX, mouseY, stache3Width, stache3Height);
  }

  }
  if (k == 4){
  if (imageDropped == true ) {
    image(stache4, x, y, stache4Width, stache4Height);
  }
  else {
    image(stache4, mouseX, mouseY, stache4Width, stache4Height);
  }

  }
  if (k == 5){
  if (imageDropped == true ) {
    image(monocle, x, y, monocleWidth, monocleHeight);
  }
  else {
    image(monocle, mouseX, mouseY, monocleWidth, monocleHeight);
  }
     }
      if (k == 6){
  if (imageDropped == true ) {
    image(glasses, x, y, glassesWidth, glassesHeight);
  }
  else {
    image(glasses, mouseX, mouseY, glassesWidth, glassesHeight);
  }

  }
if (mousePressed){
  x = mouseX;
  y = mouseY;
  imageDropped = true;
}

}

void keyPressed() {
  if (key == '-') {

  } 
  if (key == '+') {

  }
if (key == '1'){
   k = 1;
}
if (key == '2'){
   k = 2;
}
if (key == '3'){
   k = 3;
}
if (key == '4'){
   k = 4;
}
if (key == 'q'){
   k = 5;
}
if (key == 'w'){
   k = 6;
}
}
void mousePressed() {


}
void mouseReleased(){

}
void mouseWheel(MouseEvent event) {
    float e = event.getAmount();

  if (e == -1){
    stacheWidth *= 1.2;
    stacheHeight *= 1.2;
    stache2Width *= 1.2;
    stache2Height *= 1.2;
    stache3Width *= 1.2;
    stache3Height *= 1.2;
    stache4Width *= 1.2;
    stache4Height *= 1.2;
    monocleWidth *= 1.2;
    monocleHeight *= 1.2;  
    glassesWidth *= 1.07;
    glassesHeight *= 1.07;
  }
 if (e == 1){ 
   stacheWidth *= stacheScale;
    stacheHeight *= stacheScale;
    stache2Width *= stacheScale;
    stache2Height *= stacheScale;
    stache3Width *= stacheScale;
    stache3Height *= stacheScale;
    stache4Width *= stacheScale;
    stache4Height *= stacheScale;
    monocleWidth *= stacheScale;
    monocleHeight *= stacheScale;  
    glassesWidth *= glassesScale;
    glassesHeight *= glassesScale;
  }
 }

Answers

  • _vk_vk
    edited October 2013

    Hi, it's hard to say by just looking at the code, but the call to noLoop() seems a big suspect. As it is going to stop the sketch. You need to find another way to drop your's images. What crosses my mind is to have objects (a class) to hold the images, than each instance of the class can have its own boolean controlling if they are to be moved or what. You may find useful those articles from wiki:

    http://wiki.processing.org/w/From_several_variables_to_arrays

    http://wiki.processing.org/w/From_several_arrays_to_classes

    also I just posted an old code i made for another answer which is a very basic drag and drop sketch using classes, it may inspire you, check it out :)

    http://forum.processing.org/two/discussion/203/help-dragging-arrayed-images

    good luck :)

  • that no loop was a test because i was playing around with noloop(), loop() and redraw() to see if i could get it. it only goes into effect when i press the 1 key.

    i will try to create and objects class. I just learned about making classes so it will be kind of challenging

    Thanks for the links. I will check it out and see if I can figure it out from there.

  • edited October 2013

    edit for duplicate comment

  • _vk_vk
    edited October 2013

    I see, any way, you will need one boolean per image to have them independent from one another... Right now when you set imageDropped it affects all of them. Without objects you would need some thing like:

    boolean imageDropped1

    boolean imageDropped2

    boolean imageDropped3

    and so on...

    then you would need to check for mouse position inside mousePressed an dset the proper boo lea each time. Goes more or less like (pseudo code)

    void mousePressed(){
    if( mouse is over image1){
        imageDroped1 = true;
        } 
    // and so on... boring...  class approach is much easier, and it's not really difficult
    i think you should go for it 
     }
    
Sign In or Register to comment.