View Full Version : Random Movieclip Duplicates
SChaput
09-06-2008, 05:06 PM
I am trying to create a game in which once space bar is pressed to initiate the game i want a movieclip to randomly generate on the y axis and tween across the screen, a few seconds after i want the same movieclip to generate at another random location on the y axis. When the player hits any of these movieclips i want all of them to stop in their tracks.
If anyone could shed any light on this i would really appreciate it.
BLiZZaRD
09-06-2008, 05:30 PM
Well a lot of it will depend on how you have the rest set up. for basics, though we will make assumptions... for your MC will be called "enemy"
onClipEvent (load) {
function reset(){
this._x=600;
this._y=random(200)+100;
enemySpeed=random(4)+1;
}
reset();
}
This code will take your enemy MC, and make it appear at random along the y axis of your stage. It will also randomize the speed it will (eventually) travel across the stage (in pixels)
Next is the number for loop:
numEnemy=3;
for (i=2; i<=numEnemy; i++){
enemy1.duplicateMovieClip( "enemy"+i, i+100 );
}
This will take the enemy MC and duplicate it, giving each a specific name enemy1, enemy2, etc.. the numEnemy is the number of enemies on the stage at one time, you can change this as you need.
Then to make them move, we need something to trigger (press space, etc.)
onClipEvent (enterFrame) {
if (Key.isDown(Key.SPACE)){
this._x-=enemySpeed+3;
} else {
this._x-=enemySpeed;
}
if (this._x<-10) {
reset();
}
}
Again, depending on your set up and other factors, this will duplicate and move the enemies around. Your collision detection methods will determine how you stop the MCs, a for loop checking for collision then an all stop code should suffice, but this should get you started.
SChaput
09-06-2008, 05:46 PM
Thank you for the quick response. I was able to implement the first part of your explanation and make the enemy appear randomly on the y axis. However im not sure where to put the other two parts of code, having to do with the number of enemies and the start game button. Currently to start the game i just have a button that says
on (keyPress"<Space>"){
play();
}
BLiZZaRD
09-06-2008, 06:35 PM
Do you have something to interact with the enemies? A hero or ship that will go in the opposite direction as the enemies?
The start code will go on the main time line, in the Actions layer, for example, the others go on the MCs themselves.
SChaput
09-06-2008, 06:51 PM
im afraid i just dont understand or maybe am just not thinking in the right mind set. I have a ship that is coded like this:
onClipEvent (load) {
power = 1;
yspeed = 0;
xspeed = 0;
gravity = .5;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.SPACE)) {
yspeed -= power;
}
yspeed += gravity;
_y += yspeed;
}
---
And to start the game i have the ship as a button, and once space is pressed it goes to the second frame, where the enemy is randomly generated.
BLiZZaRD
09-06-2008, 07:14 PM
okay... on the enemy MC:
onClipEvent (load) {
function reset(){
this._x=600;
this._y=random(200)+100;
enemySpeed=random(4)+1;
}
reset();
}
on main timeline (usually first frame):
numEnemy=3;
for (i=2; i<=numEnemy; i++){
enemy1.duplicateMovieClip( "enemy"+i, i+100 );
}
on your ship (assuming it's name is hero)
onClipEvent (enterFrame) {
if (Key.isDown(Key.SPACE)){
this._x-=enemySpeed+hero.yspeed;
} else {
this._x-=enemySpeed;
}
if (this._x<-10) {
reset();
}
}
If you can't get it to work, zip up your fla and I will take a look when I get home in a few hours. :D
Medyman
09-06-2008, 07:19 PM
The start code will go on the main time line, in the Actions layer, for example, the others go on the MCs themselves.
tsk tsk tsk, lol
If you're creating a game, it might be beneficial to keep all of your code together. Otherwise, it gets to be a real pain to track down where bugs are when they occur.
SChaput
09-06-2008, 07:21 PM
haha well i tried to add stuff but im not sure if i should replace my older code..or add this newer stuff in...and just all around lost. Im going to include my fla file in a zip and would really appreciate it if you could look at it.
P.S. The game might look familiar as i know i sent it to you months ago. However, since then i have improved the organization of it significantly..i hope.
Thank you very much.
BLiZZaRD
09-06-2008, 07:22 PM
I agree, but if OP is having troubles with duplicating MCs, I am also assuming using Flash 7 or 8 and AS2, and using key presses to activate buttons?? I imagine the code is already all over the place. getting it to work is probably more important at this point to the OP than clean code, which can be done after game is working ;)
Oh my! I remember this game! LOL!! I will get on it when I get home!
Medyman
09-06-2008, 07:23 PM
I agree, but if OP is having troubles with duplicating MCs, I am also assuming using Flash 7 or 8 and AS2, and using key presses to activate buttons?? I imagine the code is already all over the place. getting it to work is probably more important at this point to the OP than clean code, which can be done after game is working ;)
Good point.
SChaput
09-06-2008, 07:28 PM
hahah i thought you might.
Anyways i am currently using CS3.
Thanks again.
BLiZZaRD
09-06-2008, 07:31 PM
okay, good to know. I will also use CS3 when I open it and get my hands in it.
SChaput
09-06-2008, 09:16 PM
Alright sounds great.
I'm really most concerned at this point at making the enemy appear in the black area. To do that i believe i need to make another layer in the MC, but im not exactly sure.
And a sufficient score keeper that stops when hittest happens. I might be able to figure that one once things start to get moving.
Thanks again.
BLiZZaRD
09-07-2008, 12:02 PM
Umm.. it would help to have the .fla I mean the .swf is nice.. but I am not at my decompiler computer, so it will be hard to edit this one ;)
SChaput
09-07-2008, 01:28 PM
Oh my, what was i thinking, haha. Apparently the file is too large, even zipped, for the forums. Im not sure what you want me to do but as of now i am hosting it here,
http://home.southernct.edu/~chaputs1/game.fla.zip
BLiZZaRD
09-07-2008, 01:44 PM
got it now. Will hopefully have something working by the end of the work day!
SChaput
09-07-2008, 01:59 PM
Great! Can't wait to see how it works.
Thanks.
BLiZZaRD
09-08-2008, 03:12 PM
got caught up with crap at work and today I am away from my programming computer :( I have it on my laptop and am working on it as steadily as I can, will post something asap!
SChaput
09-08-2008, 06:10 PM
Sounds great!
SChaput
09-11-2008, 10:17 PM
Just following up and wondering how everything is going.
Thanks again.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.