dcsimg
A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: [MX04]Code not firing when comparing two _x positions

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

    [MX04]Code not firing when comparing two _x positions

    Hi,

    Just for a bit of practice i have developed this simple game where you dynamically add sections of track to a movie & then get a ball to move down them.

    It all works great, but I was having a bit of a problem getting the ball to move along the center of a track when it went from say a straight to a vertical piece of track, as I was using hitTest to detect a collision & that was firing when the ball hit the edge of a track & not the center causing the ball to run along the outside of the track.


    To get round this I used this simple piece of code:

    code:
    if (holdNum == 2) {
    trace(xFactor);
    if (_root.ballOne._x>=xFactor) {
    xSpeed = 0;
    ySpeed = -2;
    }



    Which basically tests the tracks _x position against that of the ball & acts accordingly. "holdNum" tells Flash what type of section it is eg. straight or vertical - problem is that it does not fire?

    Anyone any ideas

    Thank you

    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
    Has Returned
    Join Date
    Mar 2001
    Posts
    269
    Here ya go mate, i got it working.

    I tried not to change it too much cause i hate it when people get into ya code and go optiomising it all and ya look at it and go OWWW MY GAWD!!!!11! What is this? (Not that i could)

    Hope it helps.

    You seemed to be checking if xFactor was less than _x where i just set the MC._x to the tracks _x. I also alligned the tracks so they are the center of their own stage, cause i believe it _x comes from this point. (make sense?).

    I added code for one of the tracks which was missing.

    I also set a global counter for your tracks depth. You had different counters for each type of track, you only need to make sure they have different depths so 1 counter is best then you increment it each time you dupe a clip.
    Attached Files Attached Files
    Last edited by Turbs; 05-04-2008 at 07:50 AM.

  3. #3
    Has Returned
    Join Date
    Mar 2001
    Posts
    269
    Oh and i used flash 8, i saved as old format, i hope you can open it, if not i will past the code for you.

  4. #4
    Senior Member pup100's Avatar
    Join Date
    Oct 2004
    Location
    Close
    Posts
    575
    Many thanks Turbs - that was over & above the call of duty!

    I will download the file now & try to take a look at it over the rest of the weekend, just to make sure I can get my feeble mind round it!

    With regards to it's rather "spaghetti" nature I was having major problems (never resolved) finding a way to get Flash to react to a clip dependant on it's type. I was trying to allocate it a simple variable that was set when a clip was hit, but all i was getting was the last & present clips variables which I could not use to allocate the variable. The only way round it I could find was to delete the reference to the clip from the array so Flash stopped using it & only gave me the variable of the clip it had just hit - if any of that makes any sense!

    Any thanks again mate.

    Pup100
    Last edited by pup100; 05-04-2008 at 12:48 PM.
    You will know everything when you know you never will.

  5. #5
    Has Returned
    Join Date
    Mar 2001
    Posts
    269
    Oh yes, i seem to remeber commenting out some code that was deleting elements in the array.

    Glad to help, it still breaks if the ball hits 2 tracks at the same time but give them a lot of space and it's fine

  6. #6
    Senior Member pup100's Avatar
    Join Date
    Oct 2004
    Location
    Close
    Posts
    575
    No joy opening the file Turbs

    If it is not possible to save it for MX2004 perhaps you could be kind enough to just post the code up for me?

    Thanks again

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

  7. #7
    Has Returned
    Join Date
    Mar 2001
    Posts
    269
    PHP Code:
    count 1;
    0;
    xSpeed 2;
    ySpeed 0;
    hold_array = new Array();
    ballBut_mc.onPress = function() {
        
    _root.attachMovie("ball","ballOne",100000);
        
    _root.ballOne._x 250;
        
    _root.ballOne._y 300;
        
    ballOne.onPress = function() {
            
    this.startDrag();
        };
        
    ballOne.onRelease = function() {
            
    this.stopDrag();
        };
    };
    attachLeftBut_mc.onPress = function() {
        
    _root.attachMovie("leftTrack","leftTrack"+count,count);
        
    _root["leftTrack"+count]._x 250;
        
    _root["leftTrack"+count]._y 200;
        
    _root["leftTrack"+count].num 3;
        
    _root["leftTrack"+count].onPress = function() {
            
    this.startDrag();
        };
        
    _root["leftTrack"+count].onRelease = function() {
            
    this.stopDrag();
        };
        
    hold_array.push(_root["leftTrack"+count]);
        
    count++;
    };
    attachStraighBut_mc.onPress = function() {
        
    _root.attachMovie("STtrack","STtrack"+count,count);
        
    _root["STtrack"+count]._x 200;
        
    _root["STtrack"+count]._y 200;
        
    _root["STtrack"+count].num 2;
        
    _root["STtrack"+count].onPress = function() {
            
    this.startDrag();
        };
        
    _root["STtrack"+count].onRelease = function() {
            
    this.stopDrag();
        };
        
    hold_array.push(_root["STtrack"+count]);
        
    count++;
    };
    attachBut_mc.onPress = function() {
        
    _root.attachMovie("track","track"+count,count);
        
    _root["track"+count]._x 150;
        
    _root["track"+count]._y 200;
        
    _root["track"+count].num 1;
        
    _root["track"+count].onPress = function() {
            
    this.startDrag();
        };
        
    _root["track"+count].onRelease = function() {
            
    this.stopDrag();
        };
        
    hold_array.push(_root["track"+count]);
        
    count++;
    };

    //DONT NEED THIS AS BALL DEPTH IS SO HIGH IT's 100000
    //_root.onEnterFrame = function() {
    //for (i in _root.hold_array) {
    //if (_root.hold_array[i].hitTest(_root.ball_mc)) {
    //if (_root.ball_mc.getDepth()<_root.hold_array[i].getDepth()) {
    //_root.ball_mc.swapDepths(_root.hold_array[i]);
    //}
    //}
    //}
    //};
    startBut_mc.onPress = function() {
        
    _root.ballOne.onEnterFrame = function() {
            
    this._x += xSpeed;
            
    this._y += ySpeed;
            for (var 
    i in _root.hold_array) {
                if (
    _root.hold_array[i].hitTest(_root.ballOne)) {
                    
    holdNum _root.hold_array[i].num;
                    
    xFactor _root.hold_array[i]._x;
                    
    yFactor _root.hold_array[i]._y;
                    
    //dont know what this is meant for.
                    //_root.hold_array.shift();
                    
    if (holdNum == 2) {
                        
    _root.ballOne._x xFactor;
                        
    xSpeed 0;
                        
    ySpeed = -2;
                    }
                    if (
    holdNum == 3) {
                        
    _root.ballOne._y yFactor;
                        
    xSpeed = -2;
                        
    ySpeed 0;
                    }
                    if (
    holdNum == 1) {
                        
    _root.ballOne._y yFactor;
                        
    xSpeed 2;
                        
    ySpeed 0;
                    }
                }
            }
        };
    }; 

  8. #8
    Senior Member pup100's Avatar
    Join Date
    Oct 2004
    Location
    Close
    Posts
    575
    Hi Turbs - thanks very much for doing that for me i will try & get the code married up with the movie tomorrow & hopefully get the whole thing working as intended.

    Not being able to see a working version of you adaptation, I was a little bit concerned by your mention of "it still breaks if the ball hits 2 tracks at the same time but give them a lot of space and it's fine" as this has been causing me major headaches for about 2 weeks now.

    When you say "2 tracks" do you mean that the tracks cannot be joined up as they would in a real rail simulation, because if you do that was the reason why I was using arrays to add & delete track clips. For the life of me I could not find a way for Flash to react to one track at a time when the ball was partly on one track & also hitting another. If you echoed the "holdNum" type variable it kept giving me both clips numbers which was useless.

    As I say I have'nt had a chance to see your version in action yet & you may well have cracked that. If not I may have to revert back to arrays

    Thanks again

    P100
    Last edited by pup100; 05-07-2008 at 09:49 AM.
    You will know everything when you know you never will.

  9. #9
    Has Returned
    Join Date
    Mar 2001
    Posts
    269
    Oh right, i see what ya was doing with that now. Well it wasnt working at all really, it would hit 1 track and work, but every other track after was breaking. I fixed it so it works providing you give the track enough space so that the ball doesnt touch 2 at once. I didnt know that the main problem was the 2 tracks touching.

    How about this:
    ball hits first bit of track, ball sets off down the track, trackinstance gets added to an array, code then ignores this track (for now).
    Ball hits second piece of track, ball changes direction, track instance gets added to the new array and is also ignored.
    Ball hits third piece of track, ball changes direction, third piece of track gets added to the array and is ingnore....

    now at this point the ball should not be by the first piece of track and so can be removed from the ignore array, this would mean that if you looped the track back onto itself it would still work when it gets back to the beginning.

    make sence?

  10. #10
    Senior Member pup100's Avatar
    Join Date
    Oct 2004
    Location
    Close
    Posts
    575
    I had come to the same thought process myself Turbs, last night!

    Mine would have been slightly different with the track the ball was on just getting added to a new array that I was then, somehow going to get the ball to pick up on if it came round again.

    Your version seems much better & more succinct

    Thank you for your help again

    Pup100
    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