making permanent changes to a library movieclip(changing weapon held)?

13 posts

Flag Post

is there any way to change a movieclip in my library via actionscript so that all instances of that movieclip(existing and yet to be created) have those changes applied? I’m trying to make it so that i can alter the look of the weapon my character has in his hand. this is a little hard to explain so if you don’t know what i mean, ask and i’ll try to rephrase it. TIA :)

 
Flag Post

Not that I know of.

I know that you can put this code to make all new instances of a movie clip do “B” instead of “A”

if (_root.weaponheald == 0) {"A"} else {"B"}
 
Flag Post

You can always put the different looks on separate frames inside your movieclip, then use

yourMovieClip.gotoAndStop(frameNumber);  
where yourMovieClip is the name of your movieclip and frameNumber is the number of the frame you want (though I’d use labels for the frames to make it easier to remember and if you move them around, you won’t have to change the numbers).

If these are animations, you can still do the same thing, just make sure to loop the animation back or have it stop on it’s last frame so it doesn’t go onto the animation of the next weapon (or make the animations there own movieclips).

 
Flag Post

Listen here, oldmanwinter, there’s a better way! (sorry I couldn’t help myself… your handle immediately brought a hundred jokes to mind). But seriously, what you probably want to do is create a class that contains all of the features of your equipment. So you could have a “weapon” class and store the various information about the weapon in an object. Then you could do one of a few things: you could do something similar to what Mitijea has suggested and have a movieclip with several frames, or you could just have several different movie clips of weapons. It depends what you mean by “look of the weapon.” If you are trying to change the color, you could easily store a hex value that you store in an object variable. If you are changing the size of the weapon, you could store the x and y scale and/or width and height. If the changes to the weapon are different enough you may just want to make different movie clips.

 
Flag Post

Good luck teaching object oriented programming. ^^

 
Flag Post

I’m not sure this would work, but could you make the gun graphic a movie clip inside your person movie clip? Then you can just change the frame of the gun movie clip for different guns. That’s how I’d try and do it at first. Still kind of new to flash so this is just a suggestion.

 
Flag Post

Mitijea already said it :P

 
Flag Post

I just reread the OP’s original question, and I believe there is a part we’ve left out.

If you want to change all instances of the movieclip, the best approach for a beginnner I can think of is to keep all these instances in an array and then use the same method I mentioned above to loop through them.

for (i = 0; i < myArrayOfMovieClips.length ; i++) {
MyArrayOfMovieClips[i].gotoAndStop(frameNumber);
}

Though if you understand classes and want to go that way, a static movieclip inside the class would do the trick and only require one call since a static variable is only created once for the class and each instance shares that one variable (in this case a movieclip) rather than each having there own.

 
Flag Post

That’s a good idea, but I don’t think a static MovieClip could work. MovieClips are children of a specific parent. Each time a new instance of that parent is created, a new instance of the child is created also.
If you tag a MovieClip variable as static in a class definition I imagine it just ignores it.

 
Flag Post

Dude I hate to nitpick, but the syntax for accessing an array element is square brackets [ ] not parenthesis.

MyArrayOfMovieClips(i).gotoAndStop(frameNumber);

should be


MyArrayOfMovieClips[i].gotoAndStop(frameNumber);

 
Flag Post

IndieFlashArray, yeah, sorry about that, I just got a chance to reread my post (my sister and family showed up just as I was finishing so didn’t get time to proofread), and saw that right away. Good thing I refreshed before just fixing. Hope I didn’t mislead someone there.

Moonkey, though I’ve never used a static movieclip, from a bit of googling it does seem like it works (or seems to work for others). I can’t see why it wouldn’t work, but then there are many things about flash that I think should work that don’t. (Like Arrays in classes not explicitly defined in the constructor function have a tendency to be static even when they are not marked that way… that one got me a few times until I found out about that bug.. errr… feature.)

 
Flag Post

The thing about declaring a MovieClip as static is that only the reference to it is static. Each time a new instance of the class is created, it will handle the initialization of its version of the MovieClip itself, and that will always be a new MovieClip. From a quick test, it doesn’t assign anything to the static reference even if you have something with that same instance name on the stage.

The reference thing explains what you’ve seen with arrays too. If you put the line
var myArray:Array = new Array();
inside a class definition, that Array is created when the class is first looked over. Any instance of the class will be given a reference to that array, and any changes to it will be apparent to all instances because they all hold a reference to the same array. But it’s not quite the same as static since for any instance you can assign a new Array() to myArray without changing what the other instances’ myArray points to.

btw, how do you get a code block on these forums? :s

 
Flag Post

I’ll have to give your post some more thought, Moonkey… in my tiredness it seems that you are saying that it should work for the same reason that arrays become static-like when not defined inside the constructor.

But I wanted to chime in to answer your ending query:

Use < pre > code < /pre > for code (without the spaces).