Color Changing Solved

A place for tutorials on how to get the most out of Flash

Color Changing Solved

Postby bob12321 » Mon May 07, 2012 5:18 am

The color will now change and will now stay changed when switching between a movie clip frames.
(special thanks to AnotherArrow for the finished result)
Click to Play
(Javascript Required)

changeColorProblem.swf [ 224.01 KiB | Viewed 2893 times ]



Here is the source code:
Code: Select All Code
import flash.events.Event;
import fl.events.ColorPickerEvent;
import flash.geom.ColorTransform;
var myHairColorVar:uint = 0x000000; // Black
//color changer
hairColorPicker.selectedColor = 0x000000;
var colorInfo3:ColorTransform = testBox.symbol1.transform.colorTransform;
hairColorPicker.addEventListener(ColorPickerEvent.CHANGE, colorChanged3);
testBox.addEventListener(Event.ENTER_FRAME,myOnEnterFrame);
function myOnEnterFrame(e:Event):void
{
   colorInfo3.color = myHairColorVar;
   if(testBox.currentFrame == 1)
   {
     testBox.symbol1.transform.colorTransform = colorInfo3;
   }
}
function colorChanged3(e:ColorPickerEvent):void
{
   myHairColorVar = hairColorPicker.selectedColor;
}

//counter on top left of screen
var counter:Array = new Array();
var currentFrameOn:int = 1;
var Number1:String = "1";
var Number2:String = "2";
counter[1] = Number1;
counter[2] = Number2;
speechText.text = counter[currentFrameOn];

//frame changing button
nextButton.addEventListener(MouseEvent.CLICK, nextButtonclick1);
function nextButtonclick1(event:MouseEvent):void
{
   switch(currentFrameOn)
   {
      case 1:
      {
         currentFrameOn = 2;
         speechText.text = counter[currentFrameOn];
         testBox.gotoAndStop(2);
         break;
      }
      case 2:
      {
         currentFrameOn = 1;
         speechText.text = counter[currentFrameOn];
         testBox.gotoAndStop(1);
         break;
      }
   }
}
Last edited by bob12321 on Mon May 07, 2012 6:59 am, edited 1 time in total.
User avatar
bob12321
 
Joined: Sat Jul 23, 2011 5:12 pm

Re: Color Changing Problem

Postby AnotherArrow » Mon May 07, 2012 5:51 am

Ok, just by looking over some of your code...

The hailColorPicker function "colorChanged3" is setting the color only once... when it is called.

You are going to need a variable (stored in the root / base class).

There are two ways to approach the next part, but I'll explain the easiest of the two... the other one is a little to... um... elaborate and probably unnecessary at this point (it deals with optimization).

Every time you change frame or reload that symbol. It resets itself to whatever color you have it defaulted to in flash. You will need to create a "onEnterFrame" and/or "onLoad" function for the symbol. The symbol needs to update itself (by calling the function) to the saved variable that is stored elsewhere.

so add an storage variable,
add the listener for the onEnterFrame;
add the onEnterFramefunction,
change function colorChanged3 to something like this:
Code: Select All Code
import flash.events.Event;
import fl.events.ColorPickerEvent;
import flash.geom.ColorTransform;

var myHairColorVar:uint = 0x000000; // Black

//color changer
hairColorPicker.selectedColor = 0x000000;
var colorInfo3:ColorTransform = testBox.symbol1.transform.colorTransform;
hairColorPicker.addEventListener(ColorPickerEvent.CHANGE, colorChanged3);

testBox.addEventListener(Event.ENTER_FRAME,myOnEnterFrame);

function myOnEnterFrame(e:Event):void
{
   colorInfo3.color = myHairColorVar;
   testBox.symbol1.transform.colorTransform = colorInfo3;
}

function colorChanged3(e:ColorPickerEvent):void
{
   myHairColorVar = hairColorPicker.selectedColor;
}

User avatar
AnotherArrow
 
Joined: Fri Oct 29, 2010 3:22 am

Re: Color Changing Problem

Postby BlueLight » Mon May 07, 2012 5:54 am

So you got a variable Called "hairColorPicker" and selectedColor lets you pick the color which is 0x000000 (black i believe)

"colorInfo3" is a variable and i have no clue what ':' does but i'm guessing your changing the color to the testBox color.
What should the eventListsner do?

Is your function being used? I would put a trace in it so you know when it's being called. It can't hurt.
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Color Changing Problem

Postby AnotherArrow » Mon May 07, 2012 6:02 am

BlueLight Wrote:So you got a variable Called "hairColorPicker" and selectedColor lets you pick the color which is 0x000000 (black i believe)

"colorInfo3" is a variable and i have no clue what ':' does but i'm guessing your changing the color to the testBox color.
What should the eventListsner do?

Is your function being used? I would put a trace in it so you know when it's being called. It can't hurt.


Judging from the code... I believe hairColorPicker is the name of the ... um... colorPicker symbol at the bottom left. SelectedColor is just one of its variables.
colorInfo3 is a temp variable used for swapping color info.
Eventlistener is a AS3's method of controlling and telling the computer when to call something.
In the ColorPicker's case... whenever the colorPicker's color is "Changed", it will call the function "colorChanged3".
In the new OnEnterFrame case that I added... every time the symbol "testBox" enters the frame, it will call the function "myOnEnterFrame".
User avatar
AnotherArrow
 
Joined: Fri Oct 29, 2010 3:22 am

Re: Color Changing Problem

Postby BlueLight » Mon May 07, 2012 6:07 am

So i still find Event listener confusing.
Don't you mean that a EventListener is something that listens for a event like a click on a component (Flash i guess it would be a Symbol) and once that event happens on that Component, then a function is called?
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Color Changing Problem

Postby bob12321 » Mon May 07, 2012 6:22 am

Yeah sorry for the variable names kind of threw this example of my problem together quickly so I copied my code from my actual game and didn't change any of the variable names. But AnotherArrow did solve my problem and my code works fine now so thanks again AnotherArrow and I'll post the newly completed version in a sec.
User avatar
bob12321
 
Joined: Sat Jul 23, 2011 5:12 pm

Re: Color Changing Problem

Postby AnotherArrow » Mon May 07, 2012 6:32 am

BlueLight Wrote:Don't you mean that a EventListener is something that listens for a event like a click on a component (Flash i guess it would be a Symbol) and once that event happens on that Component, then a function is called?
Yes... that is exactly what in EventListener does. And much much more. Events are not limited to just mouse clicks, but events such as when the symbol enters a new frame or when it is added to the stage... or when a key is pressed on the keyboard....etc.

It should be noted that this is Code for AS3. AS2 wouldn't need to use EventListeners.
User avatar
AnotherArrow
 
Joined: Fri Oct 29, 2010 3:22 am

Re: Color Changing Solved

Postby BlueLight » Mon May 07, 2012 7:18 am

Symbol entering a new frame.... *Mind blown!*
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Color Changing Problem

Postby GoRepeat » Mon May 07, 2012 8:41 pm

AnotherArrow Wrote:
BlueLight Wrote:Don't you mean that a EventListener is something that listens for a event like a click on a component (Flash i guess it would be a Symbol) and once that event happens on that Component, then a function is called?
Yes... that is exactly what in EventListener does. And much much more. Events are not limited to just mouse clicks, but events such as when the symbol enters a new frame or when it is added to the stage... or when a key is pressed on the keyboard....etc.

It should be noted that this is Code for AS3. AS2 wouldn't need to use EventListeners.



Because AS2 isn't the devil! I mean just looking at this stuff. You have 50 lines of code to accomplish what I do in AS2 with 4 lines. I guess this is more efficient from a runtime standpoint, but I really don't get it from a development standpoint.
Picarto LiveStream: https://picarto.tv/GoRepeat
Other Stuff: Click Here
User avatar
GoRepeat
Moderator
 
Joined: Wed Jul 21, 2010 2:26 am
Location: Behind the Looking Glass

Re: Color Changing Solved

Postby AnotherArrow » Mon May 07, 2012 9:30 pm

You have to look at in a bigger picture i guess.... imagine copying those 4 lines of code in as2 over 50 plus symbols... then imagine having to add/edit or remove said code for any reason.... ouch.
Now, in AS3, if setup right. One AS3 script file can be linked/used to cover those same 50+ symbols... but, should you have to add/edit or remove the code, you only need to edit one file.

I guess to relate it better to your work, gore... though I haven't seen much of your code... I am assuming you have similar AI coding among all your enemies. Pretty much the basic move here, move there, attack at x time, do this at y time. Right? While you may have tweaked each individual enemy to something slightly different, they all follow the same basic code. In AS3, you could use 1 class to cover all the enemies movements and AI. Should you decide you need to make adjust to how fast all enemies move, how much damage all enemies do, etc, you would only have to go to one file to change all that.

But in the end it comes down to personal flavor. If you are comfortable in AS2, stick with it. AS2 was created with the Artist in mind, making things simple and easy to accomplish with little to no programming experience needed (which is why its still included in flash development). AS3 is the programmers language of choice, following the flow of C++, C# and Java. Programmers will feel much more at home creating class files and such.

And AS3 is not the devil. So stop spreading your liez!
User avatar
AnotherArrow
 
Joined: Fri Oct 29, 2010 3:22 am

Re: Color Changing Solved

Postby BlueLight » Tue May 08, 2012 3:05 am

So by the sounds of it, AS2 sucks when it comes to object oriented code.

in java, I have a class to display code and the variables need; I admit there are a few oddities like a health variable.
now when ever I need something to displayed on the map, I just add the words "extends Actor" and all the variables and methods (functions) are use able by the class as if they are it's own.
If i need a small edit to a method i can copy paste the code directly in to my new class and then do the edit or i can insert just the methods name, return type, and available and write super.* method name and parameters* followed by the extra code. hell, code wise calling the super's version is smaller in theory.

as for the size of the code. Well that depends how effect you are. I could make my code 3 times as big if i created a variable for each time i created a throw away object. It just depends on what you can logically fit on one line and be clear.

From the sounds of it, ActionScript 2 is designed do actions you have no control over or even know about. That's great but what if your 4 lines of code is doing the work of 1000 lines of code and you don't know even how to find the other 996 lines of code; but I with my 50 lines of code am doing the work of 500 lines. Now my code is more effect because i'm able to do the same thing but the computer has to read less. Computer languages translate what we code into machine language, some time the compiler does stuff you have no control over or don't even understand.
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Color Changing Solved

Postby bob12321 » Fri May 18, 2012 12:11 am

I found out the other day that this is not 100% solved because this color changing code will cause flash to break on you if you try to use it on a bone tool animation. If anyone can figure out why this is please feel free to post the answer on this thread.
(By the way if it is not obvious already that this is as3, its as3)
User avatar
bob12321
 
Joined: Sat Jul 23, 2011 5:12 pm

Re: Color Changing Solved

Postby AnotherArrow » Sat May 19, 2012 5:16 am

It should work fine with bones.
Now, by causing flash to break... what is the exact error? (or it just doesn't run).
Gore uses bones all the time with his symbols... (yes in AS2), but AS3 should work too. It is most likely an incorrect application of the code.

I'm taking a shot in the dark, but base on the previous code, you probably have the symbols on the stage tagged with their own "instance name". (Like symbol1 in the example).
Unfortunately, though I'm not even 50% certain (i'll test it another day when i'm not about to go to bed)...., when you attach / link / bone symbols together, the instance name is lost.
Therefore, when you run the code, it can't find symbol1. Hence the crash.

IF that is the case, the only way around this is to make sure the symbol has its own class, rather then try having the main class edit or change it. This will ensure the symbol is independent of the main class. If you don't know how to make classes for symbols yet, I suggest reading up on them. It is what makes AS3 truly object-oriented and more potent and powerful then AS2.
User avatar
AnotherArrow
 
Joined: Fri Oct 29, 2010 3:22 am

Re: Color Changing Solved

Postby bob12321 » Mon May 21, 2012 5:55 am

Wow duh no wonder flash was crashing on me I had like 50 instance names that didn't exist. I feel stupid now. Oh well learn something new every crash right lol.
User avatar
bob12321
 
Joined: Sat Jul 23, 2011 5:12 pm

Re: Color Changing Solved

Postby BlueLight » Mon May 21, 2012 6:07 am

.... You mean you didn't initialize the variables?... I don't understand why you would think that would work.

Anyways you should really think about looking in to arrays.
They are a must when you start using for loops. Then again my program must use a for loop every five seconds.
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Color Changing Solved

Postby OwnerOfSuccuby » Mon May 21, 2012 5:51 pm

I think that Gorepete's Color Changer Tutorial is the best one.
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm

Re: Color Changing Solved

Postby bob12321 » Tue May 22, 2012 4:29 am

BlueLight Wrote:.... You mean you didn't initialize the variables?... I don't understand why you would think that would work.

Anyways you should really think about looking in to arrays.
They are a must when you start using for loops. Then again my program must use a for loop every five seconds.


I didnt know at the time that bone tools replaced your instance names.

OwnerOfSuccuby Wrote:I think that Gorepete's Color Changer Tutorial is the best one.


Well his is in actionscript 2, mine is in actionscript 3. It really all depends on what style of game creating you use. If your more on the artist side, then you would use acctionscript 2. If your more on the coding side, then you would use actionscript 3.
User avatar
bob12321
 
Joined: Sat Jul 23, 2011 5:12 pm

Re: Color Changing Solved

Postby OwnerOfSuccuby » Wed May 23, 2012 5:40 pm

I now experement with android - flash are working there not as i want it to work. So i tried to get codes of buttons there. To make some function work on mobile computer buttons - not only on mouse click. But some of buttons have the same code as buttons on key board, other button do not give me any code - i do not know how to get acsess to them =(

Is is AS3 sample - i post it under his AS2 version (but it looks better then that my version - the code is the same :mrgreen: )

I use it for do some thing with color when use JS or do some thing in html - so it is now like some utilite (i am bad in design so i have to use utilites to get color codes and etc. when do some thing :roll: ).

To change the color you need 3 things:
1)Get color code.
2)Set it to var and then your objects color = that var.
3)Object can have sctructure that's have no color - and it can not be changed - you have to avoid it.

But solution with color box is good too.
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm


Return to Tutorials



Who is online

Users browsing this forum: No registered users