Im building a raiden type game… you know with the airplanes and what not… but I keep writing erroneous code for the plane to fire… this is my first time using duplicate movie clips and I’m not quite sure what Im doing wrong…
this is the code…
if (Key.isDown(Key.SPACE)){
i++;
duplicateMovieClip (plane.bullet,"bullet"+i,i);
plane["bullet"+i]._y-=4;
plane["bullet"+i]._x= plane._x;
}
it doesnt fire or anything… I hate to ask such a stupid question and I did look at different tutorials… any help or pointers or anything would be greatly appreciated…
thanks alot bro… that would solve basically all my problems… a good understanding of duplicate movie clips is all I need and I know basically enough to churn out some pretty cool stuff.
well the problem is very simple. Since the code you showed us lies on the frame of the timeline I believe, the code executes when it hits that frame. The problem is that it basically runs once and thats it. You need the onEnterFrame function to cycle through frame by frame to check for the spacebar input. So just add this on a frame that lies on the root timeline:
onEnterFrame = function(){
if (Key.isDown(Key.SPACE)){
i++;
duplicateMovieClip (plane.bullet,“bullet”+i,i);
plane[“bullet”+i]._y-=4;
plane[“bullet”+i]._x= plane._x;
}
}
you should probably declare i somewhere.
DOH!
I knew it was something stupid, I put it in the movie clip code… it wasnt even that it wasnt working (kinda?) its just the code made the movie real slow and the script had to be aborted… anyways thanks alot man.
Just an add-on to this - a cool way to do this is to create a “prime” clip, like “enemyShip”, that sits onscreen, out of view, and never moves. And, that clip itself can contain its own instructions for duplication - but you have to make sure you add one piece of code so that only the original piece duplicates itself x number of times, or your system will lock up, as each clip makes multiple copies.
so you can have a movie clip of a bullet called “enemyShip”, and within it:
onClipEvent (load) {
if (_name == "enemyShip) {
for (i = 1; i < 5; i ++) {
_root.enemyShip.duplicateMovieClip ([“enemyShip” + i], i);
}
}
}
So the original clip loads, makes five copies of itself, naming them enemyShip1 through enemyShip5, on levels 1 through 5, and then it just sits there. Very efficient.
You can do the same thing with bullets, giving the prime bullet all the code it needs to function (moving itself along the screen, detecting for hits) and use another piece of code in onClipEvent(enterFrame) { that says something like:
if (_name != “bullet”) {
So the original bullet could be duplicated by the enemy ships at certain intervals, given names like bullet1, bullet2, etc. but the movement/hit detect codes wouldn’t execute for the original, prime bullet.
Let me know if that makes sense. I’ve used this kind of functionality on a few games I’ve made like this one, which I’m still working on: