开发者

Having constrains object to move X,Y at the same time?

开发者 https://www.devze.com 2022-12-24 08:53 出处:网络
The stage is separated into 4 sections, and I will be moving the camera around the stage. So a开发者_如何转开发t each particular section the camera will have an area of constrain it can move. I mange

The stage is separated into 4 sections, and I will be moving the camera around the stage. So a开发者_如何转开发t each particular section the camera will have an area of constrain it can move. I mange to constrain its X & Y, but it could only navigate either X or Y. How to move in X+Y at the same time?

if (mouseX>sec2maxX) {
                TweenLite.to(vC, 1, {x:sec2maxX});
            } else if (mouseX<sec2minX) {
                TweenLite.to(vC, 1, {x:sec2minX});
            } else {
                TweenLite.to(vC, 1, {x:mouseX});
            }

            if (mouseY<sec2minY) {
                TweenLite.to(vC, 1, {y:sec2minY});
            } else if (mouseY>sec2maxY) {
                TweenLite.to(vC, 1, {y:sec2maxY});
            } else {
                TweenLite.to(vC, 1, {y:mouseY});
            }

if i were to put X & Y in a same line of code it would be a lot of possibilities when the mouse is on top left or right bottom kind of situation, so I need to have it running seperately, but how can I combine it so that it could move X+Y?


Here's a way to fix the range without any conditions :

var x:Number = Math.max(sec2minX,Math.min(sec2maxX,mouseX)));
var y:Number = Math.max(sec2minY,Math.min(sec2maxY,mouseY)));

TweenLite.to(vC,1,{x:x,y:y});

Explained :

var min:Number = 200; // MIN limit
var max:Number = 500; // MAX limit

// A random number between 0 - 1000 without limits
var x:Number = Math.random() * 1000;

trace(x, 'at range',min,max);

// Limits to MIN by applying either X's actual value or MIN if X is smaller than MIN
x = Math.max(x, min);     

// Limits to MAX by applying either X's actual value or MAX if X is greater than MAX
x = Math.min(x, max);

trace('result',x);
0

精彩评论

暂无评论...
验证码 换一张
取 消