Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-04-2007, 12:13 PM   #1
akufen
Registered User
 
Join Date: Dec 2007
Posts: 14
Default Changing the color of a shape

Hey all

I'm new to Flash and AS3 and I'm trying to do a very simple thing. I want to change the color of a rectangle when the rectangle is clicked. I've managed to figure out how to detect a click on the rectangle with the following code

Code:
function clickHandler(event:MouseEvent):void {
	trace("rectangle was clicked");
}

rectangle.addEventListener(MouseEvent.CLICK, clickHandler);
How do I change the color of the rectangle instance in AS3 when it's clicked?

thanks!
akufen is offline   Reply With Quote
Old 12-04-2007, 12:34 PM   #2
ivanparas
Member
 
ivanparas's Avatar
 
Join Date: Nov 2007
Location: Charleston, SC
Posts: 88
Send a message via AIM to ivanparas
Default

What kind of object is the rectangle? A movie clip from your library? A sprite that was created with AS?
__________________
I don't know why he saved my life.
Maybe in those last moments he loved life more than he ever had before. Not just his life, anybody's life, my life.
All he'd wanted were the same answers the rest of us want. Where did I come from? Where am I going? How long have I got?
All I could do was sit there and watch him die.
ivanparas is offline   Reply With Quote
Old 12-04-2007, 01:38 PM   #3
akufen
Registered User
 
Join Date: Dec 2007
Posts: 14
Default

a movie clip basically. I just drew out a rectangle with the rectangle tool, converted it to a movie clip and renamed the instance to 'rectangle'.
akufen is offline   Reply With Quote
Old 01-25-2008, 07:34 PM   #4
dragoblade
Member
 
Join Date: Nov 2007
Posts: 56
Default

ya i also need something like that
i made a sprite and i wana chage the color of it, anyone know how to?
dragoblade is offline   Reply With Quote
Old 01-26-2008, 03:18 AM   #5
springframework
Senior Member
 
Join Date: Mar 2006
Posts: 470
Default

you use ColorTransform

there is an example at the bottom of that link



Jesse Couch

Last edited by springframework; 01-26-2008 at 03:20 AM.
springframework is offline   Reply With Quote
Old 01-27-2008, 12:41 AM   #6
dragoblade
Member
 
Join Date: Nov 2007
Posts: 56
Default

i see in the example that it gives some perameters
ActionScript Code:
var rOffset:Number = transform.colorTransform.redOffset + 25;             var bOffset:Number = transform.colorTransform.redOffset - 25;  this.transform.colorTransform = new ColorTransform(1, 1, 1, 1, rOffset, 0, bOffset, 0);

i can understand that the last 4 are rgb and alfa
but what are the first 4?

so how would i make something black and then on another click make it white

thanks for helping
dragoblade is offline   Reply With Quote
Old 01-27-2008, 02:19 AM   #7
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

ActionScript Code:
var box:Sprite = new Sprite(); box.graphics.beginFill(0x000000); //start off with black box.graphics.drawRect(100, 100, 200, 100); addChild(box); box.addEventListener(MouseEvent.CLICK, handler); box.buttonMode = true; var ct1:ColorTransform = new ColorTransform(); ct1.color = 0xFFFFFF; //white var ct2:ColorTransform = new ColorTransform(); ct2.color = 0x000000; //black var mySwitch:Boolean; function handler(event:MouseEvent):void {     mySwitch = !mySwitch; //toggle     if(mySwitch){         box.transform.colorTransform = ct1;     } else {         box.transform.colorTransform = ct2;     } }
The above example creates the sprite programmatically. For a MovieClip that's been created at authoring time, delete or comment out the first four lines and give the onstage clip an instance name of "box." Hope that helps some.

Edit: A lazier, but pretty much equally effective way of doing this would be to put the different graphics on separate frames of the MovieClip, give each frame a stop() command, and then make the MovieClip toggle between its two frames when it's clicked. You could change way more than the color by doing it that way. Different strokes for different folks... haha.
__________________
My Tutorials * My Website

Last edited by Mazoonist; 01-27-2008 at 02:26 AM.
Mazoonist is offline   Reply With Quote
Old 01-27-2008, 02:50 AM   #8
dragoblade
Member
 
Join Date: Nov 2007
Posts: 56
Default

i tryed this but this
ActionScript Code:
var i:Number; var piy:Number=0; var pixx:Number=0; for (i=0; i<100; i++) { var box:Sprite = new Sprite(); box.graphics.beginFill(0x000000); //start off with black box.graphics.drawRect(100, 100, 200, 100); addChild(box); box.addEventListener(MouseEvent.CLICK,handler); box.buttonMode = true; box.x=pixx; box.y=piy; box.name="box"+i; box += 1; if (pixx==10) { pixx=0; piy+=1; } }//end of loop var ct1:ColorTransform = new ColorTransform(); ct1.color = 0xFFFFFF; //white var ct2:ColorTransform = new ColorTransform(); ct2.color = 0x000000; //black var mySwitch:Boolean; function handler(event:MouseEvent):void {    //var wit:Number= b;     if(mySwitch){         box.transform.colorTransform = ct1;         //getChildByName("box"+wit);     } else {         box.transform.colorTransform = ct2;     }     mySwitch = !mySwitch; //toggle }
gave me these errors
TypeError: Error #1034: Type Coercion failed: cannot convert "[object Sprite]1" to flash.display.Sprite.
at art_fla::MainTimeline/art_fla::frame1()
TypeError: Error #2007: Parameter colorTransform must be non-null.
at flash.geom::Transform/set colorTransform()
at art_fla::MainTimeline/handler()

i'll be fine if u dont have anwer but what does buttan mode do?

Last edited by dragoblade; 01-27-2008 at 03:05 AM.
dragoblade is offline   Reply With Quote
Old 01-27-2008, 03:00 AM   #9
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

Hey dragoblade,

The buttonMode property just causes the cursor to change into a hand when you hover with the mouse. Buttons automatically do this, but MovieClips don't.
__________________
My Tutorials * My Website
Mazoonist is offline   Reply With Quote
Old 01-27-2008, 03:27 AM   #10
dragoblade
Member
 
Join Date: Nov 2007
Posts: 56
Default

thanks, it is almost always good to know more
plz if u can help with me the as problem(i unfortinatly seem to be haveing a lot of those nowadays )
dragoblade is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing alpha in a shape tween? Father_Chaos Other Flash General Questions 9 02-28-2006 02:31 AM
cannot set/get properties on class instance - why? kracko ActionScript 2.0 10 08-07-2005 08:26 PM
Skinning components with embedded components epeace Components 5 06-16-2005 08:35 PM
Flash 4 creating an array on screen with duplicate movieclip Eend ActionScript 1.0 (and below) 8 02-18-2005 09:07 PM
changing curser shape mayam ActionScript 1.0 (and below) 1 09-04-2001 01:10 AM


All times are GMT. The time now is 01:29 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.