View Full Version : Swapping x +,- coordinants with hittest.
cdrake
06-22-2005, 05:16 AM
I can use code like this it will only work for the first time it comes in contact with it. I am wanting to know how I can make it self aware of what direction it is going and automatically go the opposite direction when it hits something.
onClipEvent (enterFrame) {
if (hitTest(_root.sbg) == true) {
_x += _root.sbgspeed;
}
}
My code just assumes that the clip is moving to the left at all times which would not be true after it hits the sbg clip and starts going right.
cdrake
06-27-2005, 06:27 AM
I know it can be done. I just need a little push :p
Billystyx
06-27-2005, 12:44 PM
onClipEvent(load){
//initial direction
dir="left";
}
onClipEvent (enterFrame) {
if (this.hitTest(_root.sbg)) {
if(dir=="left")_x += _root.sbgspeed;
if(dir=="right") _x -=_root.sbgspeed;
}
}
and change 'dir' to match when you move the clip's direction.
cdrake
06-27-2005, 05:48 PM
That wasnt exacly what I was looking for but I did realize something when trying out your code though.
onClipEvent (enterFrame) {
if (hitTest(_root.block2) == true) {
_root.speed = -_root.speed;
}
if (hitTest(_root.block3) == true) {
_root.speed = -_root.speed;
}
}
This is what I was wanting but I was trying to make it more complicated than it really was. The only problem with the code is that I can only use it for one instance of a movie clip. If I put the code in any other ones they all just start shaking back and forth.
Is there a way I can make it work without giving each movie clip instance its own speed variable?
I tried adding code like this but it didnt work.
onClipEvent (load) {
_root.block1 = _root.speed;
_x += _root.block1;
}
thanks.
I have this in Clip 1
onClipEvent (load) {
_root.block1 = _root.speed;
}
onClipEvent (enterFrame) {
_x+=_root.block1
if (hitTest(_root.block2) == true) {
_root.block1 = -_root.block1;
}
if (hitTest(_root.block3) == true) {
_root.block1 = -_root.block1 ;
}
}
and this in clip 2
onClipEvent (load) {
_root.block2 = _root.speed;
}
onClipEvent (enterFrame) {
_x-= _root.block2;
if (hitTest(_root.block1) == true) {
_root.block2 = -_root.block2;
}
if (hitTest(_root.block3) == true) {
_root.block2 = -_root.block2;
}
}
The two movie clips just glide on past each other for some reason.
Dylan Marvin
06-28-2005, 02:29 AM
A few things are odd with your code. I attached something that works.
Dylan Marvin
06-28-2005, 02:32 AM
By the way, you really don't need to path to the _root if you're coding properties of the parent clip. Meaning, use "_x" or "this._x" instead of "_root.myClip._x".
cdrake
06-28-2005, 10:47 PM
ahh. I see. thank you for the help.
vBulletin® v3.7.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.