|
-
[RESOLVED] DuplicateMovieClip?
hey, i put a post on actionscript 3.0 in the newbies forum cause i didnt even see this one, i need to be able to duplicate movie clips at random, and im realy confused, ill put the code here too, but for more info, u might wanna look at my other post
Code:
chevelle_mc.addEventListener(MouseEvent.MOUSE_OVER, carStart);
function carStart(carMove:Event):void {
Mouse.hide();
chevelle_mc.startDrag();
}
//1.
chevelle_mc.addEventListener(Event.ENTER_FRAME, circleHit);
//2.
function circleHit(truckMove:Event):void {
if (chevelle_mc.hitTestObject(truck_mc)||(chevelle_mc.hitTestObject(truck2_mc))) {
chevelle_mc.stopDrag();
truck_mc.stop()||truck2_mc.stop();
text_mc.visible = true;
} else {
text_mc.visible = false;
}
}
heres teh link: http://board.flashkit.com/board/showthread.php?t=805042
Last edited by Z_Dub; 11-03-2009 at 05:11 PM.
Reason: link
-
If you want us to read another post, the least you could do is provide a link to it. Better yet, just explain in one place what it is you are having trouble with.
There's nothing in the code you posted about duplicating a movieclip, nor do I even see where you'd want to.
There is a weird line though:
Code:
truck_mc.stop()||truck2_mc.stop();
Why would you do that? Usually when you see someone do something "clever" with ||, they're trying to take advantage of evaluation shortcuts. Since stop doesn't return anything (void), the second statement will always run. That line is just a confusing way of writing this:
Code:
truck_mc.stop();
truck2_mc.stop();
There is no duplicateMovieClip functionality built into as3. But, if you have a class associated with your clip, you can create a new one as it is in the library by using the new operator. Let's say your truck mcs are instances of a class called Truck.
Code:
var truck3:Truck = new Truck(); //create a new Truck
addChild(truck3); //add the new Truck to the display.
This will not duplicate any properties of an instance that you have changed, such as transforms, position, adding children, etc.
On the other hand, if you want to duplicate only the visual appearance of an item, you can use Bitmaps.
Please describe your problem a bit more.
-
that wierd line is an or statement, method, function thingie, it means that it will collide with truck1 OR truck2, so far it works fine
yeah, i know theres not a duplicate movie clip in there, im having trouble figureing out where and how to make it.
thats sad, whyd they get rid of duplicate movie clip? no wonder it wont work
so... what should i do to get teh equivelent of duplicateMovieClip, and how do i put it into a class? i would like them to retain the same instance names, so they all collide the same, and i need them to spawn at a random Y but the sameX
oh and i did try putting the trucks on different lines, it didnt do anything, no error, just nothing, someone told me about the || thing and so far it works
Last edited by Z_Dub; 11-03-2009 at 05:09 PM.
-
No, this line is the one that means it will collide with truck1 OR truck2:
Code:
if (chevelle_mc.hitTestObject(truck_mc)||(chevelle_mc.hitTestObject(truck2_mc))) {
THIS one uses the OR operator for no reason at all.
Code:
truck_mc.stop()||truck2_mc.stop();
As I tried to say above, it depends on what sort of duplication you want to do. You haven't even told us what you want to duplicate. If you want another truck, or another chevelle, you can associate those clips with classes and use the new operator to get a new instance of that clip. If you want something else, please explain more.
-
I read your other thread. You want to create new instances of the truck clip. So, in your library where you have the clip, set a class for it. Call it "Truck". Flash will warn you that it did not find a class by that name and will create one for you. That's fine.
You want to have a bunch of Trucks running around and deal with them all, so keep them in an array:
Code:
var trucks:Array = new Array();
Now, this code creates a new Truck:
Code:
var truck:Truck = new Truck();
This puts the truck in the array for later reference:
Code:
trucks.push(truck);
And to get that on the screen, you have to add it to the displayList.
And you can loop through all your trucks by iterating over the array. You do NOT need to set or use instance names.
Code:
text_mc.visible = false;
for (var i:int = 0; i < trucks.length; i++){
var t:Truck = trucks[i];
if (chevelle_mc.hitTestObject(t)) {
chevelle_mc.stopDrag();
t.stop(); //this only stops the truck that hit chevelle. if you want to stop all trucks, do that in another loop after this.
text_mc.visible = true;
break; //stop the loop because chevelle already hit a truck.
}
}
-
ok thanks, does that code u gave me have a random part implemented? basicly, i want truck to go across the screen, at a random time, and at a random y coord. and yeah, i do understand what your saying about the useless or operator with the stop, i didnt realize i had out it there, so, what would my code look like? would it go under teh else?
-
No, the positioning and animation is not in that code. To get a random number in a range from 0 - N, use Math.random()*N. You can then set the y value of the truck to that number.
Code:
truck.y = Math.random()*stage.stageHeight;
There isn't an else in the way I wrote it above because that same if is used many times. In order to see whether anything hit after the loop, you could keep track with a boolean variable, or just use text_mc.visible. But I've already put the stop line in there if that's what you were trying to figure out where to place.
To do something at random intervals, you can set a Timer, and change the delay each time through.
Code:
var maxDelay:Number = 5000; //5 seconds
var timer:Timer = new Timer(Math.random()*maxDelay);
timer.addEventListener(TimerEvent.TIMER, launchTruck);
timer.start();
function launchTruck(e:TimerEvent):void{
timer.delay = Math.random()*maxDelay;
var truck:Truck = new Truck();
truck.y = Math.random()*stage.stageHeight;
trucks.push(truck);
addChild(truck);
}
This code still doesn't have anything in it to make the truck move, or remove it from the display and array when it goes off stage. See if you can come up with those yourself, and if you run into trouble I'll try to help more.
-
ok thanks, the truck has a tween that moves it across the screen, im just tryn to figure out how to put a bunch of em in it random opver and over again until they collide, ill put this code in and see what happens 
oh i changed the code so that the text is on another frame, its easier that way, the second frame is the beggining frame, the third the game, and the fourth the text and restart button
heres the new code
Code:
chevelle_mc.addEventListener(MouseEvent.MOUSE_OVER, carStart);
function carStart(carMove:Event):void {
stop();
Mouse.hide();
chevelle_mc.startDrag();
}
//1.
chevelle_mc.addEventListener(Event.ENTER_FRAME, circleHit);
//2.
function circleHit(truckMove:Event):void {
if (chevelle_mc.hitTestObject(truck_mc)||(chevelle_mc.hitTestObject(truck2_mc))) {
chevelle_mc.stopDrag();
truck_mc.stop()||truck2_mc.stop();
gotoAndPlay(4);
}
}
Last edited by Z_Dub; 11-04-2009 at 10:28 AM.
-
hmmmmm
im still not quite sure just where to put my code, when i put it in where it looks like itd go best, i get a call to a possibly undefined method on var truck:Truck = new Truck();
i also get a type was not found or was not a compile constant on the
var truck:Truck = new Truck();
and the
var t:Truck = trucks[i];
ill keep working on it, thx again for helping me
lol im going out of my mind trying to get this in on time and its too late to just take the easy way out and make a video
sorry for the double post
-
You must assign a class to the library symbol before you can use that code. Until you do, Truck is undefined.
Where did you define the tween which animates the truck? It cannot be on the individual truck_mc and truck2_mc, or else the new Trucks won't have it. Given what you've said, here's how I would make a bunch of trucks at random and move them across the screen, checking for collisions. This code is untested.
Code:
var trucks:Array = new Array();
var maxDelay:Number = 5000; //5 seconds
var timer:Timer = new Timer(Math.random()*maxDelay);
timer.addEventListener(TimerEvent.TIMER, launchTruck);
timer.start();
function launchTruck(e:TimerEvent):void{
timer.delay = Math.random()*maxDelay;
var truck:Truck = new Truck();
truck.y = Math.random()*stage.stageHeight;
trucks.push(truck);
addChild(truck);
}
addEventListener(Event.ENTER_FRAME, checkFrame);
function checkFrame(e:Event):void{
var hit:Boolean = false;
var toremove:Array = new Array();
for (var i:int = 0; i < trucks.length; i++){
var t:Truck = trucks[i];
t.x += 5; //move truck to the right. Adjust to change speed.
if (chevelle_mc.hitTestObject(t)) {
chevelle_mc.stopDrag();
hit = true;
toremove.push(t);
break; //stop the loop because chevelle already hit a truck.
}
if (t.x > stage.stageWidth){
toremove.push(t);
}
}
removeTrucks(toremove);
if (hit){
gotoAndPlay(4);
}
}
function removeTrucks(toremove:Array):void{
while (toremove.length > 0){
var t:Truck = toremove.pop();
removeChild(t);
trucks.splice(trucks.indexOf(t), 1); //remove it from the trucks array too.
}
}
-
the tween is like, inside teh truck, u no, like doubleclick it and you can edit it, thats where the tween is
wow, this is a lot more code than i expected to ever be working with
lol thanks for spending all this time on me
-
ok... i put teh code in, checked it, it worked, played my movie and i got 4 errors
heres teh first one, it has a problem with the var truck:Truck = new Truck();
1046: Type was not found or was not a compile-time constant: Truck.
the other issue is with var truck:Truck = new Truck();
1180: Call to a possibly undefined method Truck.
the third is a problem with var t:Truck = trucks[i];
1046: Type was not found or was not a compile-time constant: Truck.
and the last one is with var t:Truck = toremove.pop();
1046: Type was not found or was not a compile-time constant: Truck.
ok thats it, ive got plenty of time now to figure this out, so ill look at it and see what i can do about it
-
Those are all caused by the same thing: You still haven't associated your truck symbol in the library with a Truck class. I don't know how many more ways I can tell you to do that.
-
well gosh, sorry, this has all taken place throughout the school day, i find my self running from class to class, and then the network goes down 
ok got it i assigned it a class called truck
Last edited by Z_Dub; 11-04-2009 at 03:17 PM.
-
-
No problem. I didn't mean to get snippy with you, but it's hard to debug things remotely. It's doubly hard when it seems like there's a communication barrier on top of that.
-
i can see what you mean there, now all i have to do is figure out how to save it on this network without crashing flash >.<
-
oh, and one last little teensy tiny question, how do i reload the hole video? im trying to get all the little black spikes off the screen when i lose and restart...i found a line of code but its like crazy java HTML stuff, it dosent look like its what im looking for though :P
Code:
flash.external.ExternalInterface.call(jsFuncRestart);
-
Yeah, that's not what you want. That would require you write a javascript function to remove and re-add the swf, which is just not the right way to go about it.
To remove the spikes(trucks) from the screen, look at the removeTrucks function. You pass it an array full of trucks to remove, and it does it. You COULD simply call
Code:
removeTrucks(trucks.concat()); //concat to get a new copy of the array
But that's inefficient since it will have to call indexOf so many times. Do something like this:
Code:
function removeAllTrucks():void{
while(trucks.length > 0){
removeChild(trucks.pop());
}
}
-
ok, after that ill add some sounds and turn it in friday, i cant work on it now cause im at home and dont have flash, u know of any good lookalikes that are free? id really love to keep doing this, ive learned a lot, and i cant stop now
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|