NYUAD Mobile Media » Class 6 http://www.mobvcasting.com/nyuadmobilemedia/notes Notes for NYU Abu Dhabi Mobile Media Course Taught by Shawn Van Every, Spring 2012 Fri, 04 May 2012 08:03:11 +0000 en-US hourly 1 http://wordpress.org/?v=3.4.2 Mouse and Keyboard Events with Example Method http://www.mobvcasting.com/nyuadmobilemedia/notes/2012/02/13/mouse-and-keyboard-events-with-example-method/ http://www.mobvcasting.com/nyuadmobilemedia/notes/2012/02/13/mouse-and-keyboard-events-with-example-method/#comments Mon, 13 Feb 2012 12:45:12 +0000 vanevery http://www.mobvcasting.com/nyuadmobilemedia/notes/?p=125 Continue reading ]]> int startX; int startY; void setup() { } void draw() { background(255,255,255); } void mouseDragged() { println("Mouse Dragged"); line(startX, startY, mouseX, mouseY); startX = mouseX; startY = mouseY; dosomething(); } void mousePressed() { println("Mouse Pressed"); startX = mouseX; startY = mouseY; dosomething(); println(dosomething()); } void mouseReleased() { println("Mouse Released"); println(dosomething()); } boolean keydown = false; void keyPressed() { if (keydown == false) { keydown = true; // do code here println(key); } } void keyReleased() { keydown = false; println("released " + key); } int counter = 0; int dosomething() { counter++; for (int i = 0; i < 10; i++) { rect(i*10,i*10,width-i*20,height-i*20); } return counter; } ]]> http://www.mobvcasting.com/nyuadmobilemedia/notes/2012/02/13/mouse-and-keyboard-events-with-example-method/feed/ 0 Marker Example – Class and Object http://www.mobvcasting.com/nyuadmobilemedia/notes/2012/02/13/marker-example-class-and-object/ http://www.mobvcasting.com/nyuadmobilemedia/notes/2012/02/13/marker-example-class-and-object/#comments Mon, 13 Feb 2012 12:44:27 +0000 vanevery http://www.mobvcasting.com/nyuadmobilemedia/notes/?p=123 Continue reading ]]> Main Sketch

Marker shawn;
Marker fred;
Marker currentMarker;

void setup() {
  shawn = new Marker(color(0,255,0), 5, 100, "Flash");
  fred = new Marker(color(0,0,255), 70, 100000, "Cartoon");
  
  currentMarker = shawn;
}

void draw() {
  currentMarker.leak(mouseX,mouseY);
}

void mousePressed() {
  
  currentMarker.draw(mouseX,mouseY);  
}

void mouseDragged() {
 currentMarker.draw(mouseX,mouseY); 
}

void keyPressed() {
 if (key == '1') {
   // Shawn
   currentMarker = shawn;
 }
 else {
   // Fred
   currentMarker = fred;
 } 
}

Marker Class

class Marker {
 
 color myColor;
 int myRadius;
 int amountOfInk;
 String brand;
 
 
 public Marker(color _myColor, int _myRadius, int _amountOfInk, String _brand) {
   myColor = _myColor;
   myRadius = _myRadius;
   amountOfInk = _amountOfInk;
   brand = _brand;
 }
 
 void draw(int x, int y) {
   fill(myColor);
   if (amountOfInk > 0) {
     ellipse(x,y,myRadius,myRadius);
     amountOfInk--;
   }
 }
 
 void leak(int x, int y) {
   fill(myColor);
   if (amountOfInk > 0) {
     ellipse(x+int(random(-10,10)),y+int(random(-10,10)),random(0,myRadius),random(0,myRadius));
     amountOfInk--;
   }
 }
  
}
]]>
http://www.mobvcasting.com/nyuadmobilemedia/notes/2012/02/13/marker-example-class-and-object/feed/ 0
Array of Balls with SoundPlayer http://www.mobvcasting.com/nyuadmobilemedia/notes/2012/02/13/array-of-balls-with-soundplayer/ http://www.mobvcasting.com/nyuadmobilemedia/notes/2012/02/13/array-of-balls-with-soundplayer/#comments Mon, 13 Feb 2012 12:42:54 +0000 vanevery http://www.mobvcasting.com/nyuadmobilemedia/notes/?p=121 Continue reading ]]> Main Sketch


Ball[] b = new Ball[10];

SoundPlayer sp;

void setup() {
  //size(1000,1000);
  
  sp = new SoundPlayer(this, "beep.ogg");
  
  for (int i = 0; i < b.length; i++)
  {
    b[i] = new Ball(int(random(10,100)), color(int(random(0,255)),int(random(0,255)),int(random(0,255))),
              int(random(0,width)),int(random(0,height)),int(random(0,10)),int(random(0,10)),width,height);
  }
}

void draw() {
  background(255,255,255);
  for (int i = 0; i < b.length; i++)
  {
    b[i].draw();
  }
}

void mousePressed() {
 sp.play(); 
}

Ball Class

class Ball {
 
 int myRadius;
 color myColor;
 int x;
 int y;
 int directionX;
 int directionY;
 int w, h;

  public Ball(int r, color c, 
              int _x, int _y, 
              int dx, int dy, int w, int h) {
     myRadius = r;
     myColor = c;
     x = _x;
     y = _y;
     directionX = dx;
     directionY = dy;
     
     this.w = w;
     this.h = h;
     
  }  
  
  void bounceX() {
    directionX = directionX * -1;
  }
  
  void bounceY() {
    directionY = directionY * -1;    
  }
  
  void update() {
    if (x < myRadius/2 || x > w-myRadius/2) {
      bounceX();  
    }
    if (y < myRadius/2 || y > h-myRadius/2) {
       bounceY(); 
    }
    x = x + directionX;
    y = y + directionY; 
  }
  
  
  void draw() {
   update();
   ellipseMode(CENTER);
   fill(myColor);
   ellipse(x,y,myRadius,myRadius); 
  }
  
}

SoundPlayer Class

class SoundPlayer extends Object  implements android.media.MediaPlayer.OnCompletionListener {

  android.media.MediaPlayer mediaPlayer;
  android.content.res.AssetFileDescriptor assetFileDescriptor;
  processing.core.PApplet theSketch;

  boolean ready = false;

  public SoundPlayer(PApplet _theSketch, String audioFile) {
    theSketch = _theSketch;

    try {
      assetFileDescriptor = theSketch.getAssets().openFd(audioFile);

      mediaPlayer = new android.media.MediaPlayer();
      mediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(),assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
      mediaPlayer.setOnCompletionListener(this);
      mediaPlayer.prepare();
      ready = true;
    } catch (Exception e) {
      println(e);
    }
  }

  public void play() {
    if (ready) {
      try {
        mediaPlayer.start();
        ready = false;
        println("playSound");
      } catch (Exception e) {
        println(e);
      }
    }
  }

  public void onCompletion(android.media.MediaPlayer mp) {
    try {
      println("Trying to prepare");
      mediaPlayer.stop();
      mediaPlayer.prepare();
      ready = true;
    }
    catch (Exception e) {
      println(e);
    }
  }
}
]]>
http://www.mobvcasting.com/nyuadmobilemedia/notes/2012/02/13/array-of-balls-with-soundplayer/feed/ 0