A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [MX04]Problems getting a clip to recognise & respond to which clip it is over

  1. #1
    Senior Member pup100's Avatar
    Join Date
    Oct 2004
    Location
    Close
    Posts
    575

    [MX04]Problems getting a clip to recognise & respond to which clip it is over

    What I’m trying to do is get the ball clip to detect which clip it is over & modify it’s behaviour dependant upon that.

    I’m using a variable “num” for each clip to designate an individual “id” number & a “for in” loop within an onEnterFrame event to test for a hitTest.

    The way I’m testing for a change in clip is:

    “if (numHit != _root.hold_mc[i].num)”

    This is the code as it now stands:

    code:
     _root.hold_mc.trackOne_mc.num = 1;
    _root.hold_mc.trackTwo_mc.num = 2;
    done = 0;
    _root.ball_mc.onEnterFrame = function() {
    this._x += 2;
    for (i in _root.hold_mc) {
    if (_root.ball_mc.hitTest(_root.hold_mc[i]) && done == 0) {
    numHit = _root.hold_mc[i].num;
    trace(numHit);
    done = 1;
    }
    if (numHit != _root.hold_mc[i].num) {
    trace("diff");
    _root.ball_mc.onEnterFrame = null;
    _root.ball_mc.onEnterFrame = function() {
    _root.ball_mc._x -= 2;
    };
    }
    }
    };



    The weird thing is (apart from the fact it does’nt work!) that the “trace” command, contained within the “if (numHit != _root.hold_mc[i].num)” condition fires at the start of the movie?


    fla attached.
    Last edited by pup100; 09-22-2008 at 12:08 AM.
    You will know everything when you know you never will.

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    _root.hold_mc.trackOne_mc.num = 1;
    _root.hold_mc.trackTwo_mc.num = 2;
    done = 0;
    _root.ball_mc.onEnterFrame = function() {
    	this._x += 2;
    	for (i in _root.hold_mc) {
    		trace(i);
    		if (_root.ball_mc.hitTest(_root.hold_mc[i]) && done == 0) {
    			numHit = _root.hold_mc[i].num;
    			trace(numHit);
    			done = 1;
    		}
    		trace(numHit)
    		if (numHit != _root.hold_mc[i].num) {
    			trace("diff");
    			_root.ball_mc.onEnterFrame = null;
    			_root.ball_mc.onEnterFrame = function() {
    				_root.ball_mc._x -= 2;
    			};
    		}
    	}
    };
    On entering the frame the first time,
    1 ) moves ball to the right by 2 pixels
    2 ) starts FOR loop
    3 ) first IF checks to see if ball is hitting trackTwo, it is not.
    4 ) second IF checks to see if numHit (undefined) is not equal to trackTwo num, this is true
    5 ) within the IF, sets new function, to replace this one, to move ball to left
    note: this will happen the next frame interval, so FOR loop continues
    6 ) first IF checks to see if ball is hitting trackOne, it is and done is zero, it is.
    7 ) within the first IF, numHit is now set to 1, done is set to 1
    8 ) second IF checks to see if numHit is not equal to trackOne num, this is false - they are equal
    9 ) that ends the FOR loop
    On entering the frame the second and subsequent times,
    1 ) Using the new onEnterFrame function (see step 5 above), ball moves left by 2 pixels

  3. #3
    Senior Member pup100's Avatar
    Join Date
    Oct 2004
    Location
    Close
    Posts
    575
    Many thanks for that dawsonk, I appreciate that you have put quite a bit of time into helping me out on that.

    From an initial read through it sort of makes sense. I will try it out in Flash to try & get a better handle on it.

    Many thanks again

    Pup100
    You will know everything when you know you never will.

  4. #4
    Senior Member pup100's Avatar
    Join Date
    Oct 2004
    Location
    Close
    Posts
    575
    Hi dasonK,

    Sorry to take a while getting back to you on this, but I have spent the weekend looking at it on & off to try & get a better understanding of things.

    My understanding of a “for(in)” loop is that every frame it loops through ALL the items in a given object. So the piece of code that follows was designed to go through every clip in the holding object & if there was a positive hitTest result, as there would be for the first iteration of the loop as the ball was already on “trackOne_mc,” then get that clip’s “.num” variable value (in effect the starting point to test for future collisions with different clips) to test against other collisions as the ball clip moved down the track.:

    for (i in _root.hold_mc) {
    trace(i);
    if (_root.ball_mc.hitTest(_root.hold_mc[i]) && done == 0) {
    numHit = _root.hold_mc[i].num;
    trace(numHit);
    done = 1;
    }

    Could it be that I have got the wrong handle of what a loop does in as much as it doesn’t loop through all the clips each frame just one iteration per frame & that iteration starts with the highest numbered clip & works down? That’s the only way how I can understand your mentioning that:

    3 ) first IF checks to see if ball is hitting trackTwo, it is not.

    as that would be the clip at the top of the pile as opposed to trackOne which I thought?

    Basically the whole thing is still a bit of a mystery to me as it all seems watertight. To save you going into explanation of it, although that would always be appreciated, if you could just rehash the code so as that it works that would be great.

    many thanks again

    pup100
    You will know everything when you know you never will.

  5. #5
    Senior Member pup100's Avatar
    Join Date
    Oct 2004
    Location
    Close
    Posts
    575
    Hi dawsonK,

    A light finally went on above my head when I realised, for the first time, that the loop starts with the highest number & works down. I was thinking it started from "0" position & worked up. After taking that on board the whole explanation made sense.
    You will know everything when you know you never will.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center