PDA

View Full Version : FMX gravity problems



coreyem
July 6th, 2003, 07:17 PM
Hello all, I've pretty much straight copied the gravity tut. script onto a bunch of clips in my movie, with one addition- I put an onMouseDown handler in there so when you click on the little buggers, the _y position gets decremented by 10, and they jiggle a little bit. looks cute.
The problem is, all the clips on the stage that have this script applied to it jiggle when you click on any one. ? I thought
this._y just applied to the clip instance it's applied to? help? here's the script, again, applied to about 20 clips that are nested within 2 clips on the main stage.

onClipEvent (load) {

gravity = 7 ;

// This sets the _y position of the floor
floor = 49 ;

// Bounce is a number < 1 but close to 1
// The closer to 1, the higher the ball will bounce
bounce = 0.7 ;

// We set the speed of the ball when it is released.
speedx = 0 ;
speedy = 0 ;
}

onClipEvent (enterFrame) {

speedy = speedy + gravity ;

this._x += speedx/5 ;
this._y += speedy/5 ;

// If we are beyond the floor, invert the speed
if (this._y > floor) {
this._y = floor ;
speedy *= -bounce ;
}
}
onClipEvent(mouseDown){
this._y = this._y-10;
}


Any suggestions would be greatly appreciated. Thank you.
-Corey

[m]
July 6th, 2003, 07:30 PM
onClipEvent (load) {

gravity = 7 ;

// This sets the _y position of the floor
floor = 49 ;

// Bounce is a number < 1 but close to 1
// The closer to 1, the higher the ball will bounce
bounce = 0.7 ;

// We set the speed of the ball when it is released.
speedx = 0 ;
speedy = 0 ;
}

onClipEvent (enterFrame) {

speedy = speedy + gravity ;

this._x += speedx/5 ;
this._y += speedy/5 ;

// If we are beyond the floor, invert the speed
if (this._y > floor) {
this._y = floor ;
speedy *= -bounce ;
}
}
on(press){
this._y = this._y-10;/* with MX you can use mouse actions too */
}

coreyem
July 6th, 2003, 07:38 PM
Unfortunately, that's just made it so that particular clip doesn't jiggle when you click on any of the other ones. Perhaps I'm doing something else wrong, I've attached the .fla . Thank you.

[m]
July 6th, 2003, 07:46 PM
You wanted that if you click on one instante, that instance would move down, right?

coreyem
July 6th, 2003, 07:47 PM
Actually, up.

this._y -= 10;
should decrease the y position for that specific instance by 10, correct? I can't figure out why it does it for every clip instead of just the one you click.

[m]
July 6th, 2003, 07:52 PM
My code doesn't do that?

coreyem
July 6th, 2003, 07:53 PM
nope, every clip moves up 10 px. when you click on any one. Weird, huh?

[m]
July 6th, 2003, 07:58 PM
Stupid me. I know why. Because buttons are special objects with fixed timelines, the code always refers to the timeline where the button resides, kina like using _parent on everything. But the _parent of the movieclip is the stage, so it moves thestage (and the mc's in it) up.

Just make a button inside of your mc, eith only the hitframe filled. Than paste the on(press) code on that and remove the on(press)from the movieclip.

coreyem
July 6th, 2003, 08:04 PM
Actually, they already were buttons,

{Stage}
-art MC
-accessories MC
-mc1
-mc2
-button
-mc3
-mc4, etc.

if that makes any sense. I applied the on Press action to the button, same problem. Sorry to be such a pain.

Interesting point about button timelines though, I didn't know that.

Thanks for your help.

coreyem
July 6th, 2003, 08:08 PM
hm, my formatting dissapeared.


{stage}
-art MC
-accessories MC
-mc1
-mc2
-button
-mc3
-mc4, etc.


Basically, I have a main movie clip for each menu item, within the movie clip are several movie clips that each contain a button. I want the buttons, when clicked, to move all the clips in the parent clip up 10 pixels. does that make sense?
Thanks again.
-C

[m]
July 6th, 2003, 08:13 PM
Try

on(press){
_parent._y -= 10;
}

This will, in your example, move accessories MC. If you don't want to move accessories MC, but you still want to move the buttons, than the only way is to encapsulate the buttons with mc's and use a loop to address all movieclips.

coreyem
July 6th, 2003, 08:39 PM
Can you take a look at the .fla I uploaded? If I address the parent clip, it will move the entire container clip up 10 px, and since it doesn't have the gravity script applied to it (nor should it), it stays 10px higher. I can't figure out why it moves every clip that contains a button up 10 px when you click on any one clip.
I don't think using a loop would help anyways, because I'd just put the same code in the loop that's attached to each button-containing clip already, and for some reason it seems to be affecting all clips that contain a button.

coreyem
July 6th, 2003, 09:58 PM
Hey, I figured it out. I put this code in the first frame of the accessories movie clip:
acc_1.onPress = function(){
_root.accessories.acc_1._y -=10;
_root.accessories.acc_2._y -=10;
_root.accessories.acc_3._y -=10;
_root.accessories.acc_4._y -=10;
_root.accessories.acc_5._y -=10;
_root.accessories.acc_6._y -=10;
_root.accessories.acc_7._y -=10;
_root.accessories.acc_8._y -=10;
_root.accessories.acc_9._y -=10;
_root.accessories.acc_10._y -=10;
_root.accessories.acc_11._y -=10;
};
where acc_1 is the instance name of the movie clip that contains the button I want to press.
Unfortunately, it looks like I'll have to repeat this for each instance. Seems like there should be a tidier way to do this. hm.

I still don't understand why this worked, but the previous code I had didn't. Perhaps because I gave all the instances names and accessed them by absolute paths? Any comments would be appreciated, I'm a pretty bad actionscripter, but am trying to learn.