Make objects/shapes appear on top of other objects/shapes dynamically

edited April 2016 in Library Questions

I have a bunch of objects that are square videos(in an array). If the mouse hovers over one of them - it will expand (and play). the problem I have is that if an object expands but there is an object next to it and that object happens to be run after the expanding one in draw then that object will always be on top of the other object.

Is there a way to dynamically change the order in which draw runs? So I could say if object active - run last in draw (so its on top of all other objects etc.

Thanks for the help! (code below - although you would need to replace the mp3 and .mov in lines 22/23 to run this, and I have the video library and minim installed)

import processing.video.*;

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;

Minim minim;
AudioSample sample;
Tile[]Tiles = new Tile[10]; 

void setup() {
  fullScreen(2);
  //noCursor();
  imageMode(CENTER);
  minim = new Minim(this);

  for (int i = 0; i< 10; i++){
    Tiles[i]= new Tile(this);
    Tiles[i].setMovie("TORCH_2_1.mov");
    Tiles[i].setAudio(minim, "Paper Rustle_1.mp3");
  }


}

void draw() {
  background(0);

  Tiles[0].presentTile(200, 200);
  Tiles[1].presentTile(225, 200);
  Tiles[2].presentTile(250, 200);
  Tiles[3].presentTile(275, 200);
  Tiles[4].presentTile(300, 200);
  Tiles[5].presentTile(325, 200);

}

class Tile {
  PApplet app;
  Movie movie = null;
  AudioSample sample = null;

  boolean overBox = false;
  boolean canTrigger = true;
  float boxMax = 100;
  float boxMin = 25;
  int boxSize;

  Tile(PApplet papp) {
    app = papp;
    boxSize = 25;
  }

  void setMovie(String fname) {
    movie = new Movie(app, fname);
    movie.loop();
    movie.pause();
  }

  void setAudio(Minim m, String fname) {
    sample = m.loadSample(fname, 512);
  }

  void isOver(float x, float y) {
    overBox = (mouseX > x-(boxSize/2) && mouseX < x +(boxSize/2) &&  
    mouseY > y-(boxSize/2) && mouseY < y+(boxSize/2)); 
  }


  void presentTile(float bx, float by) {

    isOver(bx, by);
    if (movie != null) {
      if (movie.available() == true) 
        movie.read(); 
      if (overBox && (boxSize < boxMax)) {
        image(movie, bx, by, boxSize++, boxSize++);
        movie.loop();
      }
      if (!overBox && boxSize > boxMin ) {
        image(movie, bx, by, boxSize-=1, boxSize-=1);
        movie.pause();

      }
      image(movie, bx, by, boxSize, boxSize);
    }
    if (sample != null) {
      if (canTrigger && overBox) {
        sample.trigger();
        canTrigger = false;
      }
      if (!overBox) {
        sample.stop();
        canTrigger = true;
      }
    }
  }
}
Tagged:
Sign In or Register to comment.