AS:Main
_________________________________________
Hi!This tutorial is going to explain how to change an objects color through ActionScript,such as in Rainbow Ball by Glaiel_Gamer and GAMECUBICLE.
Now there are two different ways to do this.First of all,there is just strait frame color changing,as in if your object is on a certain frame,you use the ActionScript to change the color.The instance name I'm going to use is Item.
Part 1 Test
First of all,here is the code you put on the frame to change an objects color(on specific frame)
var item = new Color("item");
item.setRGB(0x0000FF);
Simple?Well I'll explain a bit.
First,you will be setting the variable with 'var'.My variable name is item.'new Color' will...erm...define what the variable is used for(?) and "item"(in paranthesis(whatever)) is the instance name of the object being changed.
'item.setRGB(color);' is the new color of the item.Where '0x0000FF' is at is where your color goes.Make sure there is always '0x' at the beginning.Below are a couple color codes...
FF0000 = Red
0000FF = Blue
00FF00 = Green
FFFF00 = Yellow
Those are just basic codes for colors.If you want to get the color,then just go to your color menu,roll over a color,and where it says '#(numbers)' is the code.Moving on...
Part 2 Test
Now,instead of changing an object on a frame,we will use a code to change a color on a Key press.
This code goes in the first frame,along with a MovieClip instanced 'item'...
item.onEnterFrame = function() {
var item = new Color("item");
if (Key.isDown(Key.SPACE)) {
item.setRGB(0x0000FF);
} else {
item.setRGB(0xFF0000);
}
};
Now,this is pretty much the same thing,but with an 'if' and 'onEnterFrame'.Explanation...
'item.onEnterFrame=function(){}' is for setting the frame function....or something.It's basically like a ClipEvent,but for the frame.Always before the 'onEnterFrame' you put the instance of the item you're changing.I've already explained what 'var item= new Color("item") does.I'm sure everyone knows what an 'if' statement is...and if not...Click Here.I've also already explained the RGB part,it's just redone with an 'if' and an 'else'(to set the RGB back).Moving on to the final color part...
Part 3 Test
Now,in this part,you can use the arrow keys to move around,and when you hit an object,you change colors!Yay!
Here are some things you'll need in this part...
Character Instance of "item"
Create a Movieclip,Instance of "red" and color of red
Create a Movieclip,Instance of "yellow" and color of yellow
Create a Movieclip,Instance of "blue" and color of blue
Create a Movieclip,Instance of "green" and color of green
I find it a lot easier and space saving to just copy the character,make it smaller,and change it's color with the 'color' thingy that's built in.
So,here is the code of your character...
onClipEvent (load) {
speed = 5;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
this._y -= speed;
}
if (Key.isDown(Key.DOWN)) {
this._y += speed;
}
if (Key.isDown(Key.LEFT)) {
this._x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
this._x += speed;
}
if (this.hitTest(_root.red)) {
var item = new Color(this);
item.setRGB(0xFF0000);
} else if (this.hitTest(_root.blue)) {
var item = new Color(this);
item.setRGB(0x0000FF);
} else if (this.hitTest(_root.green)) {
var item = new Color(this);
item.setRGB(0x00FF00);
} else if (this.hitTest(_root.yellow)) {
var item = new Color(this);
item.setRGB(0xFFFF00);
}
}
Basic Movement
Loops and Conditions
Clip Events
I'm sure those links will cover explaining the code,plus the stuff I've already posted.
Well,that's all I've got on color...hope it helps.
I wonder why it's so long,only being about color?