Best viewed in Firefox

Awesty Productions

Attaching blood through Actionscript

April 28th, 2007 by awesty

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 makeBlood:Object = new Object();
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.

removeMovieClip(this);

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.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed | Trackback URI

28 Comments »

Comment by Bob Subscribed to comments via email
2007-04-29 00:40:04

Thanks a bunch!

Honestly, your the best.

 
Comment by hansa
2007-04-29 03:54:07

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

 
Comment by Timmy Subscribed to comments via email
2007-04-29 04:15:33

thanks alot, but i didn’t use it for blood, i used it to make bullet holes instead
very sweet

 
Comment by austin Subscribed to comments via email
2007-04-30 06:05:21

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?

Comment by awesty
2007-04-30 08:32:26

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{}.

Comment by Vengeance
2007-04-30 21:48:55

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

Comment by awesty
2007-05-01 20:20:03

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.

Comment by Vengeance
2007-05-01 20:23:11

No, setProperly was deprecated in AS2, it became fully deprecated in AS3.

(Comments wont nest below this level)
Comment by awesty
2007-05-04 14:16:15

Sorry then. But the tutorial will still work in the versions of flash I said it would at the start.

 
Comment by Vengeance
2007-05-04 16:00:33

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.

 
 
 
 
 
 
Comment by Vengeance
2007-04-30 21:49:47

Hmm, that last thing screwed up…
function blood(x, y) {
n = random(15)+30;
for (z=0; z

 
Comment by Vengeance
Comment by awesty
2007-05-14 17:34:42

What exactly isn’t working with it?

Comment by Vengeance
2007-05-14 17:54:23

Every time I paste it in with code tags it cuts it only does 2 lines or there abouts.

Comment by awesty
2007-07-02 17:59:52

Then dont use the code tags ;)

 
 
 
 
Comment by chelion Subscribed to comments via email
2007-05-10 02:01:41

The example link does not work :(

Comment by Vengeance
2007-05-10 16:32:40

No, it works. It’s a .txt file, click it, copy the text into flash, and click anywhere on the stage.

 
Comment by awesty
2007-07-02 18:01:37

It is fixed now.

 
 
Comment by crila
2007-05-15 06:24:51

what can you use instead of setProperty in cs3? how can they take that out and not offer an alternative?

Comment by Vengeance
2007-05-15 15:44:31

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.

 
Comment by awesty
2007-07-02 17:58:59

Just use:

something._x = _xmouse;

instead of:

setProperty(something, _x, _xmouse);

It does the same thing.

Comment by Vengeance
2007-07-05 18:14:17

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.

 
 
 
Comment by Trunk Monkey
2007-06-18 07:23:21

I used your fighting game one but how can I make it so blood comes out when the enemy is hit?

Comment by awesty
2007-07-02 17:56:57

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.

 
 
Comment by Shadowdragon Subscribed to comments via email
2007-06-25 04:01:08

The example link doesn’t seem to be working. I keep getting a 404 error web page.

Comment by awesty
2007-07-02 17:56:05

It is working now.

Comment by Shadowdragon Subscribed to comments via email
2007-07-02 19:03:53

Nope, it still goes to a 404 error page.

 
 
 
Comment by tim Subscribed to comments via email
2008-06-09 01:45:38

awesty: is there any code so that when the blood touches something that something would go to a new frame.

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.