ForumsProgramming Forumchange color

14 6082
andyhunter
offline
andyhunter
30 posts
Nomad

is it possible to change the color of my movie clip each time it gets added to the stage?

  • 14 Replies
Carlytoon
offline
Carlytoon
324 posts
Nomad

If you mean that the movieclip changes color randomly only when is created, you can do this:
In a new as file we gonna make a new class, write this:
package{
import flash.display.MovieClip;
import flash.display.Shape;
import flash.events.Event;

public class part2 extends MovieClip{
public function part2():void{
addEventListener(Event.ADDED_TO_STAGE,added);
}
public function added(e:Event):void{
var array1:Array=["red","black",&quoturple","yellow","blue","green"];
var i:int=Math.random()*array1.length;

switch(array1[i]){
case "red": graphics.beginFill(0xFF0000);
break;
case "black": graphics.beginFill(0x000000);
break;
case &quoturple":graphics.beginFill(0x800080);
break;
case "yellow": graphics.beginFill(0xFFFF00);
break;
case "blue":graphics.beginFill(0x0000FF);
break;
case "green": graphics.beginFill(0x008000);
break;
}
graphics.drawCircle(0,0,4);
graphics.endFill();
addEventListener(Event.ADDED_TO_STAGE,algo);
}
public function algo(e:Event):void{
removeEventListener(Event.ADDED_TO_STAGE,algo);
}
}
}

Dont forget to save it &quotart2" without commas.


Then in the actions of a saved as3 file write this:

stage.addEventListener(MouseEvent.CLICK,crear);
function crear(e:MouseEvent):void{
var pointart2= new part2();
point.x=mouseX;
point.y=mouseY;
addChild(point);
}

If you dont understand something please tellme

andyhunter
offline
andyhunter
30 posts
Nomad

will this work in AS2 or just AS3, because i'm using AS2

Carlytoon
offline
Carlytoon
324 posts
Nomad

will this work in AS2 or just AS3, because i'm using AS2

I dont know, I only use as3 and i dont know as2, i hope someone here knows both languages and tell us if this works on as2, sorry.
master565
offline
master565
4,104 posts
Nomad

will this work in AS2 or just AS3, because i'm using AS2


Considering it's written in AS3, no, it won't. These kind of things are important to specify.

I one time wrote something like this for a particle system. I had a random number (lets call it randomColor), and every time i needed a new color i had it recalculate the number. I also had a uInt var (lets call it currentColor) that hold a color value such as 0x006633.

Lets say you want 4 colors, have it generate a random number between 1 and 4. Then in a function, have an If statement checking
If(randomColor == 1)
{
currentColor = 0x990000
}
If(randomColor == 2)
{
currentColor = 0x009900
}
If(randomColor == 3)
{
currentColor = 0x000099
}

And so on and so forth for as many colors as you want. Then before it is added have it apply a color transform to the movie clip using currentColor as the color. I don't know how to apply a color transformation in AS2 so go check it up yourself.
WhiskeyedJack
offline
WhiskeyedJack
80 posts
Shepherd

And here it is ColorTransform.

Some helpful things:

Create a random hex rgb value:

Math.floor(Math.random() * 0x1000000) // 0xFFFFFF + 1


and ColorTransform.rgb, which is a variable where you can set the new rgb value of a clip.
Ps3dude10
offline
Ps3dude10
7 posts
Nomad

I could only get it to add one MC to stage and change color only when I clicked on it.This is what I got.

import flash.geom.Transform;
import flash.geom.ColorTransform;
var timer:Number = 0;

var rect:MovieClip = _root.attachMovie("MC", "MC"+_root.getNextHighestDepth(), _root.getNextHighestDepth());

var trans:Transform = new Transform(rect);
trace(trans.colorTransform);
// (redMultiplier=1, greenMultiplier=1, blueMultiplier=1, alphaMultiplier=1, redOffset=0, greenOffset=0, blueOffset=0, alphaOffset=0)

var blueColorTransform:ColorTransform = new ColorTransform(0, 1, 1, 1, 0, 0, 255, 0);

rect.onPress = function() {
trans.colorTransform = blueColorTransform;
trace(trans.colorTransform);
// (redMultiplier=0, greenMultiplier=1, blueMultiplier=1, alphaMultiplier=1, redOffset=0, greenOffset=0, blueOffset=255, alphaOffset=0)
}
function onEnterFrame()
{
timer += 1;

if(Timer >= 120)
{
timer = 0;
rect;
}
}

can you tell me how to fix it so it keeps adding MC and so it changes without having to be clicked.

WhiskeyedJack
offline
WhiskeyedJack
80 posts
Shepherd

Are we trying to add a new rectangle every 120 frames?

andyhunter
offline
andyhunter
30 posts
Nomad

that last post was really from me not ps3dude10. and no I want to add my car MC every 1/6 second and I want the car to randomly chose a different color each time it's added to stage.

master565
offline
master565
4,104 posts
Nomad

that last post was really from me not ps3dude10. and no I want to add my car MC every 1/6 second and I want the car to randomly chose a different color each time it's added to stage.


Did you try my solution?
andyhunter
offline
andyhunter
30 posts
Nomad

yes but i couldn't get it to work this is what I got so far:

import flash.geom.Transform;
import flash.geom.ColorTransform;

class Traffic extends MovieClip
{
var speed;
var type;


function onLoad()
{

speed = 2;
type = Math.floor(Math.random()*3+1);
_x = 650;
_y = Math.random()*200+50;

}
function onEnterFrame()
{
var trans:Transform = new Transform(this);
var redColorTransform:ColorTransform = new ColorTransform(0, 1, 1, 1, 0, 255, 0, 0);
var greenColorTransform:ColorTransform = new ColorTransform(0, 1, 1, 1, 0, 255, 0, 0);
var blueColorTransform:ColorTransform = new ColorTransform(0, 1, 1, 1, 0, 0, 255, 0);
trace(trans.colorTransform);
_x -= speed;
_rotation -= 5;

if(this.hitTest(_root.ship))
{
if(type == 1)
{
trans.colorTransform =redColorTransform;
trace(trans.colorTransform);
}
if(type == 2)
{
trans.colorTransform = greenColorTransform;
trace(trans.colorTransform);
}
if(type == 3)
{
trans.colorTransform = blueColorTransform;
trace(trans.colorTransform);
}
this.removeMovieClip();
}
if( _x < -30 )
{
this.removeMovieClip();
}
}
}

andyhunter
offline
andyhunter
30 posts
Nomad

It's working now .

andyhunter
offline
andyhunter
30 posts
Nomad

right now it just makes it all one color. Is there any way to change the MC hue? I know you can in Photoshop and I think you can in Flash, but do you know how to do it in AS2?

master565
offline
master565
4,104 posts
Nomad

I'm not sure if you can do it in AS2, but this page shows how to do it in AS3

andyhunter
offline
andyhunter
30 posts
Nomad

Ok, I'll probably start making my game in AS3.

Showing 1-14 of 14