[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.
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.
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.
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.
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.
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.