Computer Science Canada how do I make a random falshing color. |
Author: | ecookman [ Tue Jul 14, 2009 3:36 pm ] |
Post subject: | how do I make a random falshing color. |
I am new to java, and am making a asteroids game. I thought it would be cool if I could have my spaceship flash different colors, is this possible, and if it is, how would this be possible. |
Author: | Zren [ Tue Jul 14, 2009 4:06 pm ] |
Post subject: | Re: how do I make a random falshing color. |
All depends on how your rendering your Spaceship. |
Author: | Tony [ Tue Jul 14, 2009 4:46 pm ] |
Post subject: | Re: how do I make a random falshing color. |
ecookman @ Tue Jul 14, 2009 3:36 pm wrote: I thought it would be cool if I could have my spaceship flash different colors...
I don't think that would be cool. Zren @ Tue Jul 14, 2009 4:06 pm wrote: All depends on how your rendering your Spaceship.
It all depends on how you're rendering your spaceship. |
Author: | saltpro15 [ Tue Jul 14, 2009 7:57 pm ] |
Post subject: | RE:how do I make a random falshing color. |
You tell him Tony! Learn yourself some grammer Zren! |
Author: | Zren [ Tue Jul 14, 2009 8:25 pm ] |
Post subject: | Re: RE:how do I make a random falshing color. |
Oh hush you two. My grammar strangely suffers during the period between June and September. Oh and by the way salty: grammer. Bloody Grammar Nazis (expression alright). |
Author: | ecookman [ Tue Jul 14, 2009 8:50 pm ] |
Post subject: | RE:how do I make a random falshing color. |
@tony lol good thing you won't be making the game and I am making my spaceship using vector art... if that helps any. I will dig up the code for it and post it later. |
Author: | ecookman [ Tue Jul 14, 2009 9:00 pm ] | ||||
Post subject: | Re: how do I make a random falshing color. | ||||
this is my main class and here are my other ones (p.s if you are going to run the applet, you have to run it from this class...using netbeans press shift F6 when clicked on this class to launch it)
This is all of the code I have. What happens so far, if you aren't going to run it... i have my ship and it freely can move around the screen, when it touches a asteroid it resets. |
Author: | Zren [ Tue Jul 14, 2009 10:54 pm ] | ||
Post subject: | Re: how do I make a random falshing color. | ||
Since it's vector art: Have the ship class contain the following.
Then, where you do the logic part of the class: You now have a few variations to do. Random colour (1,6,4,2,5...), cycle in order (1,2,3,1...), cycle back and forth (1,2,3,2...). Random: Set the colourCounter to a random number to the size of the Colours array. In order: colourCounter++ if (colourCounter>Colours.size) colourCounter = 0 Back & Forth: You need another variable indicating direction. It's basically in Order with an extra if statement. This method is very nice to achieve a glow effect if the colours are in order by how light they are. Then have it render with the colour set to Colours[colourCounter]. You get? |
Author: | TheGuardian001 [ Wed Jul 15, 2009 12:01 am ] | ||
Post subject: | Re: how do I make a random falshing color. | ||
Alternatively, if you want truly random colors, you could set the color using the Random class and the Color.getHSBColor(float h, float s, float b). Although creating an array of Colors yourself and cycling through them is probably more efficient for the program.
|
Author: | ecookman [ Wed Jul 15, 2009 12:09 pm ] |
Post subject: | Re: how do I make a random falshing color. |
//space craft public SpaceCraft(){ r= 255; g= 0; b= 100; colour = new Color(r,g,b); active=true; .... //i added this on the bottom of the class public void updateposition(){ r= (int)(Math.random()*255); g= (int)(Math.random()*255); b= (int)(Math.random()*255); colour=new Color(r,g,b); super.updateposition(); and then // VectorSprite class public void paint(Graphics g){ // i added this g.setColor(colour); I have no clue how to get it to transition from one color to another |
Author: | TheGuardian001 [ Wed Jul 15, 2009 1:49 pm ] |
Post subject: | Re: how do I make a random falshing color. |
Following your instructions on those additions, it works for me. |
Author: | ecookman [ Wed Jul 15, 2009 1:50 pm ] |
Post subject: | RE:how do I make a random falshing color. |
i am not sure on how to implement it. |
Author: | Zren [ Wed Jul 15, 2009 5:44 pm ] |
Post subject: | Re: how do I make a random falshing color. |
Another thing that would be best is if you have it rotating/randoming on a timer that way it isn't changing EVERY single cycle. Here's what I'd have changed from the initial code to get something working. It's not exactly how your doing it, but it should help you find where you need to go. Set of Colours Random VectorSprite Class Color colour getColour method returns colours[colourCounter] paint method setColor( this.getColour() ); Spaceship Class int colourCounter Color colours[] Init method define colours and set counter to 0 this.nextColour() nextColour method increment next colour check if counter is over array size, and if so, reset set VectorSprite variable colour to colours[colourCounter] set VectorSprite variable colour new random colour Asteroids Class ActionPerformed method ship.nextColour() If you want to add a timer around it, then add a new variable in either VectorSprite or Spaceship (Depending on if you only want the ship to go flash-like), that would represent the time between colour changes. Also add a variable to represent the time at the last colour change. |
Author: | ecookman [ Wed Jul 15, 2009 8:07 pm ] |
Post subject: | RE:how do I make a random falshing color. |
what if I made a if statement around each of my " r g b" variables and have it say something like if the value of the variable is 255 then variable -1. but then how would i get it to counting back up w/o having infinite if statements. (sorry this is my first time using java and this is kinda complicated for me... I have never worked with counting or timers.) |
Author: | Zren [ Wed Jul 15, 2009 9:07 pm ] | ||
Post subject: | Re: how do I make a random falshing color. | ||
This would loop forever going 0, 1, 2, ..., 255, 0, 1, ... ActionPerformed is basically inside a loop, which exits when the applet closes. You following? |
Author: | ecookman [ Thu Jul 16, 2009 10:26 am ] |
Post subject: | RE:how do I make a random falshing color. |
ooh. cool so easy. thanks and just a general question... How come the sound effects for my game don't don't work on my computer, but other ones? (and yes the sound effects are in the same location in the class folder). |