|
Author |
Topic: bad function (Read 364 times) |
|
inadaze
|
bad function
« on: Dec 22nd, 2004, 3:42am » |
|
Could any see my mistake. I thought my logic was right but it doesn't work. I enter a variable that changes one of the RGB values and a true or false. What happens is the second if statement loops and keeps turning myColor back to 0? Thanks Jays Code: void indModColor(int myColor,boolean change){ if(change==true){ if(myColor<200){ //here it says myColor = 0 myColor = myColor+10; //here it says myColor = 10 }else{ change=false; println("yo"); } }else if(change==false){ if(myColor>0){ myColor--; }else{ change=true; } } }
|
|
|
|
st33d
|
Re: bad function
« Reply #1 on: Dec 22nd, 2004, 4:01am » |
|
What context are you using the function in? observe: Code: void setup(){} void draw(){ indModColor(300,false); indModColor(100,true); } void indModColor(int myColor,boolean change){ if(change==true){ if(myColor<200){ println(myColor); //here it says myColor = 0 myColor = myColor+10; //here it says myColor = 10 println(myColor); }else{ change=false; println("yo"); } }else if(change==false){ if(myColor>0){ myColor--; println("whatchooTalkinAboutWillis"); }else{ change=true; } } } |
| Must be something else that doesn't like your function. Logic looks fine to me.
|
I could murder a pint.
|
|
|
inadaze
|
Re: bad function
« Reply #2 on: Dec 22nd, 2004, 4:21am » |
|
I am using it as a superclass method that can be used multiple times in another method called fullModColor(). void fullModColor(){ this.indModColor(this.colorAlpha); this.shapeColor = color(this.colorRed,this.colorGreen,this.colorBlue,this.colorAlpha); } I wanted to do this so that I can change each individual value of RGB and alpha. The this.shapeColor is the color definition variable of my superclass. Basically I call the method fullModColor() on one of the subclasses called Circle in the main class called Launcher. I couldn't figure out why it did not work either. Thanks Jay
|
|
|
|
inadaze
|
Re: bad function
« Reply #3 on: Dec 22nd, 2004, 7:23pm » |
|
Here is all of it. I don't understand why this.colorRed does not receive the myColor value of 250. class Launcher { Circle c1 = new Circle(100,100,10,color(0,0,0,0)); Triangle t1 = new Triangle(30,30,8,color(0,0,0,0)); Square s1 = new Square(50,50,20,color(0,0,0,0)); void Launcher(){ } void refreshPage(){ background(0); c1.draw(); t1.draw(); s1.draw(); c1.fullModColor(); } } //Superclass class Shape{ int Xpos; int Ypos; int shapeSize; color shapeColor; int colorRed; int colorGreen; int colorBlue; int colorAlpha; boolean horizMove; boolean vertMove; boolean grow; boolean jitter; int jitterAmount; int jitterDirectChooser; boolean jitterDirection; void indModColor(int myColor){ myColor = 255; } void fullModColor(){ indModColor(this.colorRed); this.colorAlpha = 200; this.shapeColor = color(this.colorRed,this.colorGreen,this.colorBlue,this.colorAlpha); } void horizMove(){ if(horizMove){ if(this.Xpos<width-this.shapeSize){ this.Xpos=this.Xpos+10; }else{ horizMove = false; } }else{ if(this.Xpos>0+this.shapeSize){ this.Xpos=this.Xpos-10; }else{ horizMove = true; } } } void vertMove(){ if(vertMove){ if(this.Ypos<height-this.shapeSize){ this.Ypos=this.Ypos+10; }else{ vertMove = false; } }else{ if(this.Ypos>0+this.shapeSize){ this.Ypos=this.Ypos-10; }else{ vertMove = true; } } } void jitterDirectChooser(){ jitterDirectChooser = int(random(3)); if(jitterDirectChooser != 0){ if(jitterDirectChooser==2){ jitterDirection = true; }else{ jitterDirection = false; } }else{ jitterDirection = false; } } void jitter(){ jitterDirectChooser(); if(jitterDirection){ jitterAmount = int(random(-5,5)); this.Xpos = this.Xpos+jitterAmount; }else{ jitterAmount = int(random(-5,5)); this.Ypos = this.Ypos+jitterAmount; } } void diagMove(){ horizMove(); vertMove(); } void grow(){ if(grow){ if(this.shapeSize < 60){ this.shapeSize = this.shapeSize+1; }else{ grow = false; } }else{ if(this.shapeSize > 5){ this.shapeSize = this.shapeSize-1; }else{ grow = true; } } } } //------------------------------------------ //Subclass Circles class Circle extends Shape{ Circle(int Xpos,int Ypos,int shapeSize,color shapeColor){ this.Xpos = Xpos; this.Ypos = Ypos; this.shapeSize = shapeSize; this.shapeColor = shapeColor; } void draw(){ noStroke(); fill(this.shapeColor); ellipse(this.Xpos,this.Ypos,this.shapeSize,this.shapeSize); } } class Triangle extends Shape{ Triangle(int Xpos,int Ypos,int shapeSize,color shapeColor){ this.Xpos = Xpos; this.Ypos = Ypos; this.shapeSize = shapeSize; this.shapeColor = shapeColor; } void draw(){ noStroke(); fill(this.shapeColor); triangle(this.Xpos,this.Ypos,this.Xpos+this.shapeSize,this.Ypos+this.sha peSize,this.Xpos-this.shapeSize,this.Ypos+this.shapeSize); } } class Square extends Shape{ Square(int Xpos,int Ypos,int shapeSize,color shapeColor){ this.Xpos = Xpos; this.Ypos = Ypos; this.shapeSize = shapeSize; this.shapeColor = shapeColor; } void draw(){ noStroke(); fill(this.shapeColor); rect(this.Xpos,this.Ypos, this.shapeSize,this.shapeSize); } } //============================================ //This is done second void setup(){ background(0); size(500,500); ellipseMode(CENTER_DIAMETER); rectMode(CENTER_DIAMETER); myGame = new Launcher(); } //this is done first Launcher myGame; //this is done third void loop(){ myGame.refreshPage(); }
|
|
|
|
st33d
|
Re: bad function
« Reply #4 on: Dec 23rd, 2004, 2:17am » |
|
I don't wholly get your code. I haven't had to use inheritance yet and I also have a different syntax style. My approach to your indModColor would have been: Code: int indModColor(){ return 255; } void fullModColor(){ this.colorRed=indModColor(); this.colorAlpha = 200; //this.shapeColor = color(this.colorRed,this.colorGreen,this.colorBlue,this.colorAlpha); this.shapeColor = color(this.colorRed,this.colorGreen,this.colorBlue,this.colorAlpha); } |
| But I am a junior programmer (in experience but not age). So I may have miss-read what you're trying to do.
|
I could murder a pint.
|
|
|
inadaze
|
Re: bad function
« Reply #5 on: Dec 24th, 2004, 6:35pm » |
|
I am new also to inheritance. Just practicing. What I was trying to do with my function was to create a general method that could be used for each color and alpha for each subclass shape(circle,triangle...) that would increment through the color values slowly. In other words I wanted to be able to call the method on the shape so that the color would change slowly and constantly back and forth throughout the 256 values of each RED, GREEN and BLUE and ALPHA. Unfortunately, just returning the 255 would only be an on and off switch from minimum and maximum on each color which wouldn't work the way I want. But thank you for you attempt to help. I always appreciate it! Happy holidays! Jay
|
|
|
|
fjen
|
Re: bad function
« Reply #6 on: Dec 24th, 2004, 9:57pm » |
|
something like this? Code:class Shape { int sColor; void setColor (int _c) { this.sColor = _c; } void draw(){;} } class Rect extends Shape { void draw() { fill(sColor); rect(10,10,10,10); } } class Circle extends Shape { void draw() { fill(sColor); ellipse(50,50,10,10); } } Shape[] shapes; void setup() { size(200,200); shapes = new Shape[]{ new Rect(), new Circle() }; } void loop() { background(120); for (int i=0; i<shapes.length; i++) { shapes[i].setColor(color(random(255),random(255),random(255))); shapes[i].draw(); } } |
| this makes no sense to me: Code:void indModColor(int myColor){ myColor = 255; } |
| basic types (int, float, ..) are passed by value, so what you're doing is basicly changing the variable that holds the incoming value .. no more. /F
|
|
|
|
inadaze
|
Re: bad function
« Reply #7 on: Dec 31st, 2004, 9:35pm » |
|
Thanks, I did not realize that an int variable could hold the four int's in color(); and I did not think about placing a random in each RGB and Alpha together.
|
|
|
|
|