Restricting mouse follow

ok… I have an MC with a small triangle that follows your mouse position only along the x axis. I dont want this to happen all over the entire stage rather only if your mouse is within a 40 pixel high band across the stage. If that band happend to be from
y=0 to y=40 I would write this code:

var speed = 3;

if (_ymouse < 40 && _ymouse > 0) {
triangle.onEnterFrame = function() {
this._x += (this._parent._xmouse-this._x)/speed;
};
};

but it doesn’t work… help any one??

Peace

Like this?

kinda… here is the swf I am working with… the triangle follows the mouse on the x axis anywhere you move… I only want it to do this while the cursor is in the blue area… then if you move out it would just freez even with where you left then move back to your cursor when you reenter.

this is the code I am using just to get the motion:

var speed = 3;
triangle.onEnterFrame = function() {
this._x += (this._parent._xmouse-this._x)/speed;
};

Id like to restrict this to the 40 pixel band of the blue.

whops forgot the swf

This?

It’s normal that it’s not working. If you define an onEnterFrame handler, you have to reset it. Here, you’re saying: if I’m here on the screen, I want you to do that on every frame. But when you get out of that zone, the onEnterFrame is independent, it keeps going and going.

var speed = 3;

triangle.onEnterFrame = function() {
    if (_ymouse < 40 && _ymouse > 0) {
        this._x += (this._parent._xmouse-this._x)/speed;
    }
};

Or if you want to keep the same order

var speed = 3;

if (_ymouse < 40 && _ymouse > 0) {
    triangle.onEnterFrame = function() {
        this._x += (this._parent._xmouse-this._x)/speed;
    };
} else {
    delete triangle.onEnterFrame;
}

pom :asian:

The code in my fla works - i think- but I didn’t have to reset anything.

The problem is that you’re going to have to run that last piece of code in a loop of some sort, an onMouseMove for instance.

Edit: Sorry Flex, I was talking about Ryall’s code. And thanks for the tute, I’m checking it right now :slight_smile:

Not sure if I follow. Try the second fla that I posted. It looks to me like it works the way he wanted… unless I got the wrong idea - check it out and let me know

Oh…

It’s just the first part - so it’s basic…

I guess I just kept putting my if statement on the wrong place!

yeah flex… yours is pretty much it - thanks!

ilyas: thanks for explaining it to me like that… that is the best way I learn code.

Thanks guys!

Peace

ok one more question… I’d like to add something to this existing code:
var speed = 8;
triangle.onEnterFrame = function() {
if (_ymouse > 0 && _ymouse < 40) {
this._x += (this._parent._xmouse-this._x)/speed;
}
};

I’d like to stop when you leave the area between 0y and 40y, but before it stops I’d like it to continue to your last x_position (that was withing the y range obviously).

I am thinking that I’ll need to define a target x_position on leaving the area, but I am unsure of the best way to go about this. Some help/direction would be great.

Peace

Hey Ryall, can you use the code tag please? It’s really easier to read the code.

About your thing, I guess you could try that

var speed = 8;
triangle.onEnterFrame = function() {
    if (_ymouse > 0 && _ymouse < 40) {
        this._x += (this._parent._xmouse-this._x)/speed;
    } else {
        this._x += (40-this._x)/speed;
    }
};

Something like that…

pom :asian:

yeah I’ll use the code tag - sorry bout that!

The code you posted returns the triangle I am using to a specific position no matter at what x value the mouse leaves the area at. I want it to return to the mouses last x position. I am working on… I’ll get it eventually, but if you have more ideas I’d appreciate it.

Peace