View Full Version : Mass items, same code
Dhryn
September 7th, 2006, 11:06 AM
I have a game where the character picks up coins, the coins adds to there score. I can use the following code and place it on each of the coins.
onClipEvent(enterFrame){
if(this.hitTest(_root.guy)){
_root.score += 1;
unloadMovie(this);
}
}
However, I want many coins on the stage and this will slow it down alot.
I am looking for something that can be writen on the main time line and then applied to all of the coins on the stage.
This could also be useful for multiple enemies which have A.I for moving and attacking.
Any help would be great
DangerousDan
September 7th, 2006, 07:53 PM
Modifying what I told the guy about how to make balls drop from the sky :|
ActionScript Code:
i =0;
function createCoin(num){
for(c=0;c <= num; c++){
i = getNextHighestDepth();
attachMovie("coin","coin"+i,i);
_root.["coin"+i].onEnterFrame = function(){
if(_root.["coin"+i].hitTest(_root.guy){
_root.score += 1;
unloadMovie<(_root.["coin"+i]);
}
}
}
}
setInterval(createCoin(3),5000);
That may or may not work but it is a base for to work off of. Any questions about it and I'll answer them (This goes on the frame not inside the character or coin MCs, this assumes the character's instance name is guy and the coin's name is coin. You will also need to export your links for linkage in order to attach them.)
REEFˇ
September 7th, 2006, 07:58 PM
I have a game where the character picks up coins, the coins adds to there score. I can use the following code and place it on each of the coins.
onClipEvent(enterFrame){
if(this.hitTest(_root.guy)){
_root.score += 1;
unloadMovie(this);
}
}
However, I want many coins on the stage and this will slow it down alot.
I am looking for something that can be writen on the main time line and then applied to all of the coins on the stage.
This could also be useful for multiple enemies which have A.I for moving and attacking.
Any help would be greatAre the coins already created through AS or are you looking for a code to duplicate coins? What are you trying to accomplish? You want to apply that code to every coin in the stage w/o having to choose them one by one or something?
DangerousDan
September 7th, 2006, 07:59 PM
Working on decoding the HTML from my AS... I tried copying the AS and copied the <font> tags as well from the forum...
Dhryn
September 8th, 2006, 08:28 PM
Are the coins already created through AS or are you looking for a code to duplicate coins? What are you trying to accomplish? You want to apply that code to every coin in the stage w/o having to choose them one by one or something?
The coins are on the stage already, I dont want to have the code loading the coins if that is possible.
REEFˇ
September 8th, 2006, 08:29 PM
Do you want to apply that code to every coin in the stage w/o having to choose them one by one or something?
DangerousDan
September 8th, 2006, 08:35 PM
The coins are on the stage already, I dont want to have the code loading the coins if that is possible.
Forget what I said then :stunned:, I dunno know any ways to do it without loading them via code. Well... You could cycle through them with a loop simillar to my code I think... I dunno.
Dhryn
September 8th, 2006, 08:40 PM
Forget what I said then :stunned:, I dunno know any ways to do it without loading them via code. Well... You could cycle through them with a loop simillar to my code I think... I dunno.
How would I tell the coins where to go on the stage using your code?
DangerousDan
September 8th, 2006, 08:45 PM
Here:
ActionScript Code:
i =0;
function createCoin(num){
for(c=0;c <= num; c++){
i = getNextHighestDepth();
attachMovie("coin","coin"+i,i);
_root["coin"+i].onEnterFrame = function(){
_root["coin"+i]._x = 0; //changed 0 to whatever X you want.
_root["coin"+i]._y = 0; //changed 0 to whatever Y you want.
if(_root["coin"+i].hitTest(_root.guy){
_root.score += 1;
unloadMovie<(_root["coin"+i]);
}
}
}
}
setInterval(createCoin(3),5000);
something like that.
Dhryn
September 8th, 2006, 08:48 PM
would that not set all the coins to the same point
DangerousDan
September 8th, 2006, 09:03 PM
Not if you use a variable or make it random...
_root["coin"+i]._x = i * 50;
_root["coin"+i]._y = i * 20;
LittleFenris
September 9th, 2006, 12:00 PM
How about naming your coins "coin1_mc", "coin2_mc" etc...and using a for loop with a hitTest?
var totalCoins:Number = 12; //Change 12 to however many coins you have on stage.
character_mc.onEnterFrame = function() {
for (i=1; i<=totalCoins; i++) {
if (this.hitTest(coin_mc[i+"_mc"])) {
score += 1;
}
}
}
Dhryn
September 9th, 2006, 05:37 PM
Is there a way to place all the coins in one movie clip, then test to see if the guy hits one of the coins, then to find which coin the guy has hit then to applie code to that coin.
If so can some one please help with the coding or a tutorial that might help
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.