|
-
Say hello to Bob
[help] hit testing two different duplicated movieclips
sory to post this but the search keeps timing out.
basically i want to hit test two different duplicated movie clips, one mc is a bullet with the depth i and the other a zombie with a depth of d, i am creating them both inside a movie clip which uses counters and both of them run properly but i want to do the hit test in one if statement inside the movie clip, i have tried doing hit testing in one of the movie clips but cant seem to access the other duplited movie clip, can someone help me with this or come up with a better way of doing it as this has been bugging me for ages.
oh one other problem, the bullets are created on mouse down and not on enterframe, would that make a difference at all?
heres the code in the counter mc, its quite bad. ive taken out the hittest.
code:
onClipEvent (load) {
var count;
var maxcount=25;
var setspeed=3;
var setsize=50;
var sethealth=5;
gun=1;
d = d+100000;
}
onClipEvent (enterFrame) {
count+=1;
if (count==maxcount) {
count=0;
i = i+1;
duplicateMovieClip(_root.comet, "comet"+i, i);
_root["comet"+i]._y=-30;
_root["comet"+i]._x=random(300)+100;
_root["comet"+i].speed=random(setspeed)+1;
scale=random(setsize)+50;
_root["comet"+i]._xscale=scale;
_root["comet"+i]._yscale=scale;
_root["comet"+i].health=sethealth;
}
}
onClipEvent (mouseDown) {
if (gun==1) {
d = d+100000;
duplicateMovieClip(_root.pistol_fire, "pistol_fire"+d, d);
_root["pistol_fire"+d]._x = _root.char._x;
_root["pistol_fire"+d]._y = _root.char._y;
}
}
cheers.
Last edited by Kakihara; 12-06-2004 at 12:47 PM.
If our body is a clock ticking away and if while we experience all that this world has to offer time has still continued to pass, are we living or just experiencing a slow death?
-
Didn't do it.
Hey, Kakihara.
You did not put down what code you currently have for the hitTest; could you please post it?
I think the problem lies with the way you are using your duplication code: you are using those two variables (i and d) not only for a unique depth to duplicate the MCs to, but also as part of their instance names (i.e., the 'comet' on depth 100 would be 'comet100'). So, when you perform the hitTest, you will need to cycle through all possible names of both 'comet' and 'pistol_fire' to see if they hit.
The problem with this as I see it is that you have no upper limit set to the variable 'i'; each frame, i is increased by one. Which means two things: eventually there is a possibility of conflict with the depth assigned by d, and the loop to test for hits with instances using 'i' in their names will require to great of a range to be speed-efficient.
My suggestion is as follows: create a new array called 'target_comets' and append the newly created comet MC's instance name to it as it is created. As each comet is destroyed or moves off of the playing area, remove its name from the array. What you will be left with is a 25 item array of only the 'comets' currently on the screen.
Then to run the hitTest, simply have each 'pistol_fire' MC (or bullet, or whatever you are trying to have hit the comet) read through the array and perform the hitTest. When a hit is detected, remove that 'comet' MC's instance name as above.
I would also recommend putting a limit on the value of i, say a hundred or thousand or so. You should also be aware that you are incrementing d by 100,000 each click, not by one. It'd probably be better to assign d a value of 100,000 in the load event and then simply use the d++ incremental.
hth
Last edited by japangreg; 12-06-2004 at 01:34 PM.
Hush child. japangreg can do what he wants. - PAlexC
That was Zen - this is Tao.
-
Farmer
are you sure i and d are coming out as numbers? it just looks like you;re adding to undefined vars. btw, you don't need to add 100000 each time you're creating a bullet, as flash has a depth maximum. you only need to incretiment by one each time a bullet is spawned. when you create a comet, add it to a comet array that keeps track of all the comets on screen,code:
onClipEvent (load) {
var count;
var maxcount=25;
var setspeed=3;
var setsize=50;
var sethealth=5;
gun=1;
d = d+100000;
cometArray = [];
}
onClipEvent (enterFrame) {
count+=1;
if (count==maxcount) {
count=0;
i = i+1;
duplicateMovieClip(_root.comet, "comet"+i, i);
_root["comet"+i]._y=-30;
_root["comet"+i]._x=random(300)+100;
_root["comet"+i].speed=random(setspeed)+1;
scale=random(setsize)+50;
_root["comet"+i]._xscale=scale;
_root["comet"+i]._yscale=scale;
_root["comet"+i].health=sethealth;
//add to the array
cometArray.push(_root["comet"+i]);
}
}
then try this code when you spawn a bullet
code:
onClipEvent (mouseDown) {
if (gun==1) {
d = d+100000;
duplicateMovieClip(_root.pistol_fire, "pistol_fire"+d, d);
_root["pistol_fire"+d]._x = _root.char._x;
_root["pistol_fire"+d]._y = _root.char._y;
_root["pistol_fire"+d].onEnterFrame = function(){
for(var j=0; j<cometArray.length; ++j){
if(this.hitTest(cometArray[j])){
//blow comet up
//take the comet out of the array - check this code as i can't remember
//if it's right or not
cometArray.splice(j);
}
}
}
}
sorry, it's all messy, but see if it works
-
Say hello to Bob
thanks for the replies, i would post my hit test but it is pointless as it is crap, i can do hit tests normally but this just threw me off, ive tried divilly's way but the hit test doesnt work, everything else works though, i guess it must be the reading form the array thats the problem but i havent had any experience with reading arrays before (never really bothered with them), anyone know whats wrong, thanks for the help so far though.
If our body is a clock ticking away and if while we experience all that this world has to offer time has still continued to pass, are we living or just experiencing a slow death?
-
Didn't do it.
If you haven't done so, spend as much time learning about arrays as you need to to master them; they are incredibly useful for game programming.
In order to get the above to work (using divillysausages' code as a base) you need to do a few things: first, declare a new array on a keyframe at _root ( cometArray = new Array(); ). Second, you'll need to modify the linecode: cometArray.push(_root["comet"+i]);
tocode: _root.cometArray.push("_root.comet"+i);
For the hitTest, rather than establish the pistol_fire MC's EnterFrame code in the keydown event, I'd put it in the MC you are duplicating and wrap it in a conditional so it only runs when it is duplicated, like so:code: //in the original pistol_fire MC on _root
onClipEvent(enterFrame){
if (this._name != "pistol_fire"){
// will only run in duplicated clips
for (i = 0; i < _root.cometArray.length; i++){
if (this.hitTest(_root.cometArray[i])){
// a hit occurs, run necessary code here
}else{
// what you want the MC to do when not hitting a comet
}
}
}
}
That should do it; let us know what happens.
Last edited by japangreg; 12-06-2004 at 02:50 PM.
Hush child. japangreg can do what he wants. - PAlexC
That was Zen - this is Tao.
-
Say hello to Bob
hit test still doesnt work, its difficult for me to sort any small problems as i dont know arrays (only the basics), i will learn arrays properly, funny thing is i can create maps with arrays but just not the whole push and splice thing although i have read through stuff about those commands, ill show you the code i have done for you to see where i have gone wrong.
actually it might be easier to post the fla file, cheers for the help so far.
If our body is a clock ticking away and if while we experience all that this world has to offer time has still continued to pass, are we living or just experiencing a slow death?
-
Didn't do it.
Okay; first off, in the frame on _root, you havecode: _root(cometArray=new Array());
This should just becode: cometArray=new Array();
Then, in your cometconter MC, you need to add the path (_root) to the name pushed to the array as follows:code: onClipEvent (enterFrame) {
count += 1;
if (count == maxcount) {
count = 0;
i = i+1;
duplicateMovieClip(_root.comet, "comet"+i, i);
_root["comet"+i]._y = -30;
_root["comet"+i]._x = random(300)+100;
_root["comet"+i].speed = random(setspeed)+1;
scale = random(setsize)+50;
_root["comet"+i]._xscale = scale;
_root["comet"+i]._yscale = scale;
_root["comet"+i].health = sethealth;
// add to the array
_root.cometArray.push("_root.comet"+i);
}
}
Make those changes, and you'll notice that now your bullets are registering the hitTest (they should disappear, as that is what you currently have in the hitTest block). To remove the incoming comet, just alter the hitTest as follows:code: for (i=0; i < _root.cometArray.length; i++) {
if (this.hitTest(_root.cometArray[i])) {
removeMovieClip(_root.cometArray[i]);
_root.cometArray.splice(i, 1);
removeMovieClip(this);
}
}
hth
Last edited by japangreg; 12-06-2004 at 03:43 PM.
Hush child. japangreg can do what he wants. - PAlexC
That was Zen - this is Tao.
-
Say hello to Bob
thank you very much, you both been a big help and i do understand most of the code, ill read up on this stuff over the next few days, that was the only thing stopping me from making this game fully so expect to see a wip very soon.
again cheers.
If our body is a clock ticking away and if while we experience all that this world has to offer time has still continued to pass, are we living or just experiencing a slow death?
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
|