A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [mx04]Why won't a varaible assigned through a "for in" loop show up in next function?

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

    [mx04]Why won't a varaible assigned through a "for in" loop show up in next function?

    This is really driving me nuts!

    I want to change direction of a ball based on which clip it is over – I’m using “hitTest()” to check for the change in clips.

    To flag up a change I’m using a homemade property of the clip “num”. It is just a simple number reference so each clip has a number property starting at 1 & increasing with each clip.

    _root.hold_mc.base1_mc.num =12; is a sample of the assignment for the first clip.

    I store a reference to the current clip that the ball is on’s reference number in a variable called “oldHit”

    So to test for a collision I use:

    if (Number(_root.hold_mc[i].num) != oldHit){
    //A change has happened do something else;
    }

    It all seems to work except when I want to assign the new clips variables to the onEnterFrame event.

    if (Number(_root.hold_mc[i].num) != oldHit && asign == 0) {
    newHit = _root.hold_mc[i];
    trace(newHit);
    asign = 1;
    }

    This will give me a reference to the correct clip, but when I try to use “newHit” in the “If” statement that follows a “trace” command tells me that it is “undefined”?

    The full code is set out below together with an attached fla which is commented in the function in question.

    Thank you very much for any help

    code:
     _root.hold_mc.base_mc.angle = -Math.PI/2;
    _root.hold_mc.base_mc.sign = 1;
    _root.hold_mc.base_mc.num = 1;
    _root.hold_mc.base2_mc.angle = -Math.PI;
    _root.hold_mc.base2_mc.sign = 0;
    _root.hold_mc.base2_mc.num = 2;
    done = 0;
    ardone = 0;
    var radius = 100;
    var speed = .5;
    asign = 0;

    _root.onEnterFrame = function() {
    for (i in _root.hold_mc) {
    if (_root.circ_mc.hitTest(_root.hold_mc[i])) {
    if (ardone == 0) {
    oldHit = _root.hold_mc[i].num;
    ardone = 1;
    }
    if (Number(_root.hold_mc[i].num) != oldHit && asign == 0) {
    newHit = _root.hold_mc[i];
    trace(newHit);
    asign = 1;
    }
    if (done == 0) {
    angle = _root.hold_mc[i].angle;
    trace(_root.hold_mc[i]);
    //comes up as "undefined"
    //I would like to use the new clips reference for the following variables
    // but Flash is having none of it!
    //So I would have liked to have used :sign = newHit.sign;

    sign = _root.hold_mc[i].sign;
    centerX = _root.hold_mc[i]._x;
    centerY = _root.hold_mc[i]._y;
    radianSpeed = speed*Math.PI/180;
    done = 1;
    }
    }
    sign == 1 ? angle += radianSpeed : angle -= radianSpeed;
    ax = Math.cos(angle)*radius;
    ay = Math.sin(angle)*radius;
    _root.circ_mc._x = centerX+ax;
    _root.circ_mc._y = centerY+ay;
    //trace(_root.circ_mc._y);
    }
    };

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

  2. #2
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    I've just tried running this and it shows up fine, no undefined.

    Are you nesting this swf in another one? that would change the location of _root.

  3. #3
    Senior Member pup100's Avatar
    Join Date
    Oct 2004
    Location
    Close
    Posts
    575
    Sorry lesli - For some reason notifications of replies are not getting into my Inbox, so it was only by chance that I have realised this morning that you had been kind enough to help me out on that.

    I think I got to nub of the problem as I saw it in a further post:
    http://board.flashkit.com/board/showthread.php?t=763578

    I will be posting up a further reply to daswonk with my take on what is happening.
    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