Attaching blood through Actionscript
In this tutorial you will learn how to make a blood animation appear and play when you click. You can change the code a bit so the blood will appear when an enemy get hits or something. I am using Flash CS3, but Flash 8 and Flash MX 2004 will work as well.
NOTE: If you are using Flash CS3, make sure you choose a AS 2.0 file.
Click here to see an example of what we will be making. (Click the screen)
You will need to make your blood animation. The easiest way to do this would be to hit Ctrl+F8 to make an empty MC (Movie Clip) and do the animation in there. This is what mine looks like:
Make sure the registration point (the little cross) of the MC is in the middle.
Open up the library (Ctrl+L) and right click on the MC containing the blood animation. Choose linkage and a window should open.
Click the box that says ‘Export for Actionscript’ and enter ‘blood’ into the identifier box.
We can now attach the blood MC with actionscript. Enter this code onto the frame:
var i:Number = 0;
makeBlood.onMouseDown = function() {
attachMovie("blood","blood"+i,getNextHighestDepth());
setProperty("blood"+i, _x, _xmouse);
setProperty("blood"+i, _y, _ymouse);
i++
};
Mouse.addListener(makeBlood);
Now if you test your movie (Ctrl+Enter) a the blood animation should appear when you click the mouse, but it won’t disappear. We will fix that later, after I explain this code.
Line 1: This is making a new object. This will be a mouse listener to check if the mouse button is pushed.
Line 2: Just making a variable called ‘i’ that increases everytime the blood is made. This is so each blood MC has a different name. blood1, blood2 etc..
Line 3: This is saying is the mouse button is down then to run the script between the curly braces.
Line 4: Here the blood MC is attached to the stage using the ‘attachMovie()’ function. It is giving the MC an instance name of blood+i, so if blood is equal to 6 then its name will be blood6.
Lines 5-7: This is making the blood MC’s _x and _y coordinates equal to the mouses. Line 7 is increasing i by 1.
Line 9: This is attaching the makeBlood listener to the mouse.
Now double click on the blood MC in the library and select the last frame and enter this code. This will make the blood disappear.
That just uses the removeMovieClip() function to remove the MC when it gets to that frame. If you test your movie you should get something like this.















Thanks a bunch!
Honestly, your the best.
hahaha i was about to ask you how i should attach movieclips with actionscript becuse for i can only attach one and then if i attach one more the last one disaper
thanks alot, but i didn’t use it for blood, i used it to make bullet holes instead
very sweet
very cool. i am tinkering with it to see if i could make it attach the blood when a hit test happens…how would i do that?
Well instead of using a listner you can just put all the code that is between makeBlood.onMouseDown{} in a hitTest. So something like:
var i:Number = 0;
onEnterFrame = function(){
if(this.hitTest(that)){
attachMovie(”blood”,”blood”+i,getNextHighestDepth());
setProperty(”blood”+i, _x, some._x);
setProperty(”blood”+i, _y, some._y);
i++
}
};
Just change what the hitTest is checking and the _x and _y in the setProperty{}.
Wow, honestly, that is the worst ‘tutorial’ I’ve ever seen. First of all, the setProperty function is deprecated, as in it shouldn’t be used anymore.
This only works in flash 7+ all coded then.
function blood(x, y) {
n = random(15)+30;
for (z=0; z
Actually you can still use setProperty with Flash 8. If you where coding in Flash CS3 you must select a action script 2.0 file.
setProperty was only deprecated in AS 3.0.
No, setProperly was deprecated in AS2, it became fully deprecated in AS3.
Sorry then. But the tutorial will still work in the versions of flash I said it would at the start.
I never said it wouldn’t work. I’m saying it’s very inefficient and ineffective. Dynamically doing the blood through code is a much more effective way of doing it.
Hmm, that last thing screwed up…
function blood(x, y) {
n = random(15)+30;
for (z=0; z
This thing seems to screw it up every time…
What exactly isn’t working with it?
Every time I paste it in with code tags it cuts it only does 2 lines or there abouts.
Then dont use the code tags
The example link does not work
No, it works. It’s a .txt file, click it, copy the text into flash, and click anywhere on the stage.
It is fixed now.
what can you use instead of setProperty in cs3? how can they take that out and not offer an alternative?
setProperty was deprecated in as2. as3 has fully deprecated it and I don’t blame. them. First of all, I wouldn’t bother doing this method in AS3. AS3 has the power to make extraordinary blood effects, and doing something like this would be a waste of resources. Personally, I still haven’t fully learnt as3 so I can’t give any code examples. But if you wish to continue the method of setProperty, just use an as2 file rather than an as3 file.
Just use:
something._x = _xmouse;
instead of:
setProperty(something, _x, _xmouse);
It does the same thing.
No, in cs3 (I’m assuming he means AS3 too) there is no _x property. Nor is there an attach movie property. What you would have to do is:
When you go to give the blood a linkage name, give it a class name instead. Something like, bloodMc
then to attach it:
function attachBlood(event:Event){
var blood:bloodMc = new bloodMc();
stage.addChild(blood);
blood.x=mouseX;
blood.y=mouseY;
}
stage.addListener(Event.MOUSE_DOWN, attachBlood);
That will attach it on mouse click.
I used your fighting game one but how can I make it so blood comes out when the enemy is hit?
Instead of attaching the blood when the mouse is clicked, attach it when the enemy is hit. Instead of attaching it to the mouses x/y, attaching it to the enemies.
The example link doesn’t seem to be working. I keep getting a 404 error web page.
It is working now.
Nope, it still goes to a 404 error page.
awesty: is there any code so that when the blood touches something that something would go to a new frame.