Montana-Media-Arts / 120_CreativeCoding

Main Code Repo for MART 120. This contains lecture code examples, the HW Wiki, and HW Code Examples.
https://montana-media-arts.github.io/creative-coding-1/
MIT License
131 stars 13 forks source link

Function with in a function #46

Closed MarcLenahan closed 6 years ago

MarcLenahan commented 6 years ago

Does anyone know if a function can be placed inside a function. I want to make it so that if a cursor is on the right side of the window and pressed it would change the color of the right side of the window. This would use a mousePressed function and an if/else statement.

michaelmusick commented 6 years ago

This is absolutely possible, and we will be getting to it in a week or so!

allenharguess701 commented 6 years ago

you can write the function outside, lets say tbe draw() as its own function like bounce() for instance. then inside the draw function you call it by siting bounce() jusg remember it will loop with the draw each time the draw function loops

On Oct 12, 2017 5:43 AM, "Michael Musick" notifications@github.com wrote:

This is absolutely possible, and we will be getting to it in a week or so!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Montana-Media-Arts/120_CreativeCoding_Fall2017/issues/46#issuecomment-336102830, or mute the thread https://github.com/notifications/unsubscribe-auth/AeFz7G8hRSb9FT6hXXY3KUFUogY7C6TGks5srftlgaJpZM4P2izo .

quintinbruderer commented 6 years ago

There is a lot going on in here...take your time and read this thoroughly. If this doesn't make sense yet, it soon will. 👅
Since I can't do "Live Demos", or have the effort to figure it out right now, just copy paste this code into an empty example.

You actually already are putting functions within functions. All your functions used (like fill, shapes, noCursor, etc) are living in other functions, like setup and draw.


//***************
// creating 3 functions. Functions, within functions (within functions)

// doSomething draws 2 circles. Simple. It uses the 2 passed in functions...
// ... to create the fills. Not so simple.

function doSomething(colorChooser, greyScaler) {
  console.log(colorChooser()) // check the console and see the 'string' result
  fill(colorChooser());
  ellipse(20,20,20);

  console.log(greyScaler()); // check the console and see the integer produced
  fill (greyScaler())
  ellipse(40,40,20);
};

// colorChooser will create a 'string' of an rgb value. (Probably could be done better)
// the string will look something like rgb(40,200,140)
function colorChooser() {
  return 'rgb(' + floor(random(255)) + ',' + floor(random(255)) + ',' + floor(random(255)) + ')'
};

// greyScaler will "divide"/multiply 255 (the fullest color range) by a decimal.

function greyScaler() {
  return floor(255 * (random(1)));
};

//***************

// now back to confort area
function setup () {
  createCanvas(300,500);
  // we have to pass the doSomething function the 2 other functions as arguments. Notice without () parenthesis.
  // ...Because we dont want to call them, just pass them. Earlier we use them, as we want to call them for their returned result
  doSomething(colorChooser, greyScaler);

}

function draw () {
  // cause green is the coolest color. And to show a regular drawn circle.
  // do realize we could call doSomething here instead of setup too.
  // if we wanted to go even more crazy and animate/strobe the doSomething circles on each loop.
  fill (0,255,0);
  ellipse (60, 60, 20);
}