Detection of collision, multiple movieclips, auto collision.

Here is my code to attach several clips on the stage

var humans:Number = 90;
for (i=0; i<humans; i++) {
          mcHuman = attachMovie("mcHuman", "mcHuman"+i, i);
//blah blah
}

Whatever happens, it seems impossible to detect collisions between mcHuman and mcHuman...

Anyone have any ideas?

Thank you.

There is probably more code that you realize that there are, but it is up to you to decide.

Your first not set of loops make sense, just a loop is necessary as you had originally.  This might be a reason for things to go crazy.

As regards the enterframe function goes, you have an excess of braces in this document, which could also be a reason for things to go crazy.  In addition, if you do not move humans at all (your code does nothing to move as it is), the you do not want or need the function enterframe no more...

humans var = 90;

for (i = 0; i

mcHuman = attachMovie ("mcHuman", "mcHuman" + I, i);

mcHuman._x = random (Stage.width);

mcHuman._y = random (Stage.height);

mcHuman.xTarget = random 20-10;

mcHuman.yTarget = random 20-10;

}

for (i = 0; i

for (j = 0; j

If (_root ["mcHuman" + i] .hitTest (_root ["mcHuman" + j])) {}

trace ("hit!");

}

}

}

Tags: Adobe Animate

Similar Questions

  • detect clicks on multiple movieclips

    Hello

    Thanks for your time, read this.  I'm hard-pressed to find ways to detect clicks on various movieclips that I'm generation.   Basically, I am allowing to set a number any instances of the same clip.   I would like each instance A) be clickable and B) pass the reference to the click handler.

    For example, I have something like this:

    function addMissile() {}

    this.attachMovie ("missile", "missile" + mtop, this.getNextHighestDepth (), {_x:startx, _y:starty});})

    activeMissiles [mtop] = _root ["missile" + mtop];

    .focusEnabled activeMissiles [mtop] = true;

    Selection.setFocus (activeMissiles [mtop])

    MTop ++

    }

    I would like to add a reminder on click for each of these instances of clip.   Grateful if someone can point me in the right direction

    Best regards

    Tom

    You can use:

    function addMissile() {}

    var mc:MovieClip = this.attachMovie ("missile", "missile" + mtop, this.getNextHighestDepth (), {_x:startx, _y:starty});})

    activeMissiles.push (this ["missile" + mtop]);

    or better yet:

    activeMissiles.push (mc);

    mc.focusEnabled = true;

    Selection.setFocus (mc);

    mc.missileIndex = activeMissiles.length - 1;

    mc.onRelease = function() {}

    trace (this ._name + ":" + this.missileIndex);

    }

    }

  • Management of multiple detection of collisions between objects (not necessarily circles)

    Hello.

    I´d would like to know if any good West tutorial, or if anyone knows, a way to manage a detection of multiple collision with detection of pixel level, objects, not necessarily circles, irregular objects.

    Any help?

    Try: flash 3 physical collision

  • Detect the collision does not

    I created a class called code battery and he added inside a function named "Clone". Battery is derived from a base class of Components.as with a file name. Here's where I'm adding battery inside a function named "clone":

    var ComponentRef: Class = getDefinitionByName ("Battery") in class;

    var object: instance = new ComponentRef();

    addChild (DisplayObject instance (instance));

    instance.x = 500;

    instance.y = 300;

    instance.compName = "battery."

    instance. AMPERAGE = 20;

    instance.partNumber = "BA12345";

    I have also a clip with an instance name of "negativeProbe" that I add an event listener for:

    negativeProbe.addEventListener (Event.ENTER_FRAME, detectedColl);

    This brings me to the detectedColl function when I'm doing the collision detection:

    battery: battery of var = new Battery();

    If (battery.hitTestObject (negativeProbe)) {}

    trace ("collision!");

    Trace never recorded a success. I don't get an error, but no shot is ever recorded. What I'm doing wrong here?

    Here, you have added the "instance" object to the scene

    addChild (DisplayObject instance (instance));

    but this object not added!

    battery: battery of var = new Battery();

    If you have an object with the name 'battery', but it is not on the stage and he will never hit another object unless you add it.

  • How to detect the Collision layer or the layer is not in the display?

    Hi guys,.

    my script should detect if a portion of a layer is not in the display window.

    I found Dan´s article on the collision, but don t know if it helps with my needs. (sampleImage()? maybe)

    KLICK: Collision

    The second idea I had was ee . sourceRectAtTime() method, but it may possible to give a false positive.

    (for example a textlayer with a huge but bounding box a few letters that are visible in the view).


    You have ideas or advice for me check this behavior?



    I hope that my explanations are comprehensible.

    To detect if part of a layer is not in the window display (by which I assume you mean not within the limits of the model) you can calculate the current dimensions of your layer by first finding its width and its height and multiplying by (scale/100); then using the layer position and anchor point, you could say if the layer has been moved. Such as:

    layerWidthPostScale = layerWidth * (scale.x / 100)

    layerHeightPostScale = layerHeight * (scale.x / 100)

    If (abs (Position.x - AnchorPoint.x) > ((compWidth-layerWidthPostScale) / 2) |)   ABS (Position.y - AnchorPoint.y) > ((compHeight-layerHeightPostScale) / 2)) {}

    Part of the layer is outside the limits of the model

    }

    This will work with solids and the layers of images. Not sure about the text or the shape of fine layers, or a layer hidden unless you can find the bounding boxes. Even in this case it may not be enough if you are looking for a pixel solid being inside or outside.

  • How to drag and drop multiple movieclips at the same time

    Hello world

    I'm a new actonscript 3 and adobe flash CC user and I am building an application but I have been stuck for several days. I have looked everywhere and tried everything I could think of, but I can't yet find a solution to my problem that I thought were pretty basic.

    Basically, let's say I have a rectangle and a circle on the stage. Once I did of the movieclips and assigned an instance name to each of them, I want to be able to perform a drag and drop the Rectangle so that both the rectangle and the circle become movable at the same time.

    For now, I have a mouse down events listener associated with the instance of rectangle, a method startDrag assigned to the rectangle instance and another assigned to the circle. But in this configuration, when I click on and drag the rectangle, only the circle is mobile (only the last line in the code is taken into account).

    I don't know if what I'm trying to achieve is feasible, but any help will be greatly appreciated, thank you!

    The startDrag() method can only work for one object at a time, so in your case the Treaty the last of them, designated the task.  This approach is to temporarily to plant the two objects in a container and then drag the container.

    rectangle.addEventListener (MouseEvent.MOUSE_DOWN, fl_ClickToDrag);  When I click on the rectangle

    var dragMC:MovieClip = new MovieClip();
    addChild (dragMC);

    function fl_ClickToDrag(event:MouseEvent):void

    {
    dragMC.addChild (rectangle);        move objects in the container
    dragMC.addChild (circle);
    dragMC.startDrag ();
    }

    stage.addEventListener (MouseEvent.MOUSE_UP, fl_ReleaseToDrop); When I release the mouse button

    function fl_ReleaseToDrop(event:MouseEvent):void

    {
    dragMC.stopDrag ();
    Rectangle.x += dragMC.x;         Adjust the positions of the objects to their new location
    Rectangle.y += dragMC.y;
    Circle.x += dragMC.x;
    Circle.y += dragMC.y
    addChild (rectangle);                 move back to the scene objects
    addChild (circle);
    dragMC.x = dragMC.y = 0;       reset the benchmark for the dragMC
    }

    All this stuff of repositioning in the Drop function is necessary because when you drag the container, the positions of the content are still on their original coordinates inside the container.  So when you drop them they will resume their x / y positions in their new parent, meaning they go back where they were.  Reposition them where they have been trained to take into account the change in the position of the dragMC.

  • Remove the eventlistener for multiple movieclips in a table

    Hi all

    Game scenario:

    I do a crossing road learning alphabet game. In the first level there are 10 alphabets tell of A J (movieclips) stand on one side of the road in a mixed random way. The player must move the alphabets (using the keyboard commands) in alphabetical order, so that it strikes at the entrance of a kindergarten and is removed from the scene.

    Situation-problem:

    If you click on the correct alphabet the player gets a positive score. And if the wrong alphabet is cliked, a negative score is given. This works well for the first alphabet, but, once the first alphabet hits its target and is removed from the scene, it does not work for the next alphabet since removeEventListener to the case the first alphabet code continues to work for the continuous alphabets.

    For eg., once the first alphabet "letA" is removed from the scene, and then when I click on "Letbs" that gives a score to both positive and negative. Positive note, since it is the next alphabet that must be clicked. Negative note, since the "wrongClick" table contains the movieclip 'Letbs', although it was intended for the situation, when "letA" was also present on the stage.

    Code:

    var wrongClick:Array = new Array (Letbs, THG, letD, summer, fall, letG, letH, letI, letJ);

    for (var i = 0; i < wrongClick.length; i ++) {}

    wrongClick [i] .addEventListener (MouseEvent.MOUSE_DOWN, negScore);

    function negScore(e:MouseEvent):void {}

    gameScore += pointsForMiss; showGameScore();

    wrongClick [i] .removeEventListener (MouseEvent.MOUSE_DOWN, negScore);

    }

    }

    Requested solution:

    Can someone please tell me how I can write code that, once an alphabet is removed from the scene, the method addEventListener for above of movieclips is completely removed and a new addEventListener starts to work for the rest of the alphabet which remain on stage.

    thnx in advance

    Kind regards

    Ginette tabriz

    run this code:

    for (var i = 0; i)

    wrongClick [i]. delete EventListener (MouseEvent.MOUSE_DOWN, negScore);

    }

    When the correct letter is clicked.  That is to say, in the correct letter listener function.

  • Referencing of multiple movieclips that have similar names of instance

    I have a player movieclip and six movieclips obstacle with the name of "obs1',"obs2"instances, up to"obs6 ". If the player hits one of the obstacles, the death of variable is set to true. The code I have so far is:

    If (hitTest (_root.obs1) | hitTest (_root.obs2): (_root.obs3) hitTest | hitTest (_root.obs4) | hitT's (_root.obs5) |) {hitTest (_root.obs6))}

    dead = true

    }

    Since if the statement is bad enough as it is, I wanted to know if it was possible to reference all movieclips with instance name 'obs' followed by a digit. In my view, that it's hooks, something like:

    hitTest (_root.obs [i])

    Can someone give me some advice?

    (I use Flash CS3 and ActionScript 2.0)

    for (var i: Number = 1; i<>

    {if (HitTest (_root ["OBS" + i]))}

    dead = true;

    break;

    }

    }

  • multiple movieclips and mutiple event management.

    Hi all

    I have several movieclips (image sequence), which I want to add (click and drag), mouseevent event (left and right arrows) to the keyboard and the zoom event (mouse wheel).

    I have this function for one mc (image sequence).

    Now, if I use this function, I have some problem with the call of the function of the same frame.

    It's

    If click on an other mc in frame 3 say, click on another function, it should continue to frame 3 only, not from the first image again.


    Help, please.

    Thanks in advance.

    I guess that shows the part where you vortag home discovered in a 360-degree mode?

    If so, do a global var on your root timeline:

    var rotationframe:int = 1; at the beginning of the application, all dirffernt images are sitting on frame 1

    You can access this var to anywhere inside any function of your application by calling:

    root.rotationframe

    now in the service which manages the rotation (lets say its your keyboard function) at the end (after you have moved the playback cursor), you ensure that you always set

    root.rotationframe = garage.currentFrame;

    and in the service that manages switching between images write you sth. as

    gotoAndStop (root.rotationframe);

    Be aware, you cannot simply use this code without adapt it to your specific needs.

  • gotoAndPlay multiple movieclips

    Im making a gotoandplay function. He plays 2 movieclips at the sametime, but after a few clicks his work stoppages.

    is there a way to fix this?

    And is it possible to play/stop an object in a movieclip another?

    sample

    mc_movie. One.addEventListener (MouseEvent.Click, One1);

    function one1(e:MouseEvent)

    {

    mc_movie.gotoAndPlay (100);

    mc_button.gotoAndPlay (100);

    }

    mc_movie. Two.addEventListener (MouseEvent.Click, Two2);

    function two2(e:MouseEvent)

    {

    mc_movie.gotoAndPlay (200);

    mc_button.gotoAndPlay (200);

    }

    mc_movie. Back.addEventListener (MouseEvent.Click, Back);

    function back(e:MouseEvent)

    {

    mc_movie.gotoAndPlay (1);

    mc_button.gotoAndPlay (1);

    }

    If your timeline movement happens to take you far executives where these domestic buttons do not exist, they could lose their event listeners - depends on if they also exist in frameworks where this code does not exist which could be reallocated.

  • Simplify multiple movieclips early frame masking

    I am trying to simplify my code and save space in my control panel actions.  At the beginning of a new mount, I have several hidden clips (._visible = false) until I call so that they become visible later.

    This framework AS I will condense:

    MC1._visible = false;

    MC2._visible = false;

    Mc3._visible = false;

    ... etc.

    I have about 30 MC # movieclips.  Can anyone show how to shorten the code so all 30 mcs are hidden you enter the picture?  A table perhaps?

    Thank you.

    There is no need of an array, but you use array notation:

    for (var i: Number = 1; i<>

    This ["MC" + i] ._visible = false;

    }

  • a multiple movieclips class

    Hello

    Yet once I fell on the dilemma of having a class on a Movieclip a desire then to use the same class on another clip. He put on another library attached does not work. I tried to use a class that extends my class but that extends a class extended movieclip seems misleading in flash.

    Is there a clean way without reprogramming of my class to do? Or I copy my class several times with a different name?

    TIA

    The class for Content1 was Content1 and its basic ContentFader class. Same Content2 is class: Content2 and the Base of the ContentFader class.

    Then just to mix it up a little. I created a Content1.as file, but none for Content2. Everything works as I expect.

    I guess it's as all y ' All thought that the base class meant something other than what I think he means? If that were the case it should say DisplayObject or actually all the object.

  • Detect the direction of += MovieClip?

    Hello

    I want to know how if a movieclip is go += or-= ._y ._y


    Thank you.

    Without providing for it otherwise, you should always check if the current value of the FLF is higher or lower than the previous value of the FLF.

    I probably would prepare something such that I would have a variable assigned to tell me that the status that would put any time that I specify ._y += or ._y =

  • Realistic of the random movements of multiple MovieClips

    Hi here is my code:

    30 images/s

    var human = 30;

    hSpeed var = 4;

    for (i=0; i<humans; i++) {
         //attach movieclip with name "human"
         //give it a random x y position (there are now 30 scattered human MCs)
         _root["human"+i].onEnterFrame = function() {
              this._x += random(hSpeed); //Here is the 'random' movement of the Movieclips
              this._x -= random(hSpeed);
              this._y += random(hSpeed);
              this._y -= random(hSpeed);
                   //some more code
         }
    }
    

    Makes the jitter movieclips on, humans don't do that, I can't find a way to make their random movement still smooth, moving in a random direction for a few seconds (preferably a random amout of seconds), then to change direction - I found a way to do it but it does not work on individuals It works on all 30 movieclips, which means that all move in the same direction.

    Don't forget that the movement must be random for each individual movieclip, not all move in the same direction.

    Does anyone know how? Thank you

    If you want to maintain the movement in a random direction for some time, then you're probably better using code interpolation.  Generate interpolations for each object that moves them from their current position to some random in some random delays.  And have a listener that determines when the Tween is complete so that a new Tween is engaged with the new direction and speed (time).

  • Multiple MovieClips with the same instance name

    I'm a tutorial of defence tower and part of the code is set up so I can build towers in the video for "grass". I wanted to add patches more 'grass' and gave them all the same instance name so I can be able to place towers on them, but I have the problem that I can only place towers on the first point, I placed and bears the name of the instance of the "grass". I can't place towers on the other patches of grass.

    I removed the name of the instance of the first patch of grass and it let me build towers on the second task, but the second patch only.

    I followed this tutorial: http://www.goofballgames.com/2010/01/31/how-to-build-a-tower-defense-flash-game-part-2-pla five-tours /

    Here's the code for what I believe:

    onClipEvent (load)
    {
    Active = 0;
    }

    onClipEvent (enterFrame)
    {
    If (current == 1)
    {
    setProperty ("", _x, int ((_root._xmouse-10) / 20) * 20 + 20 ");
    setProperty ("", FLF, int ((_root._ymouse-10) / 20) * 20 + 20 ");

    hitTestOnGrassMovieClip = _root.grass.hitTest (_x, FLF, 1);

    If {(hitTestOnGrassMovieClip)
    gotoAndStop (1);
    _root.Ranger.gotoAndStop (1);
    }
    else {}
    gotoAndStop (2);
    _root.Ranger.gotoAndStop (2);
    }

    _root.Ranger._x = _x;
    _root.Ranger._y = FLF;
    _root.Ranger._width = donut _root ["tower_" + round] * 2;
    _root.Ranger._height = donut _root ["tower_" + round] * 2;
    }
    }


    (press)
    {
    hitTestOnGrassMovieClip = _root.grass.hitTest (_x, FLF, 1);
    hitTestOnDeSelectMovieClip = _root.deselect.hitTest (_x, FLF, 1);

    If (hitTestOnGrassMovieClip: hitTestOnDeSelectMovieClip) {}
    If {(hitTestOnGrassMovieClip)
    ++ _root.towerCount;
    _root ["tower_" + round] .duplicateMovieClip ("t" + _root.towerCount, 500 + _root.towerCount);
    a = _root ["t" + _root.towerCount];
    a ._x = _x;
    a ._y = FLF;
    a.active = 1;
    }

    Active = 0;
    setProperty ("", _x, 1000);
    _root.selectedTower = "";
    _root.Ranger._x = 1000;
    _root.Ranger._width = 10;
    _root.Ranger._height = 10;
    }
    }

    1. you cannot use the same instance name for different objects and expect more from a being targeted.  You must assign a unique name, or you can store references to them in an array and use the table to target.

    2. you send AS2 code messages in the forum of AS3.  You should repost in the AS2 forum.  http://forums.Adobe.com/community/Flash/flash_actionscript

Maybe you are looking for