Показать сообщение отдельно
Старый 26.12.2011, 17:45
cupid вне форума Посмотреть профиль Отправить личное сообщение для cupid Найти все сообщения от cupid
  № 1  
Ответить с цитированием
cupid

Регистрация: Dec 2011
Сообщений: 2
По умолчанию rotate & drag странно ведет

Доброе всем.
Правлю код, позволяющий вращать стрелку часов arrow вручную. mouseTarget - вспомогательный ролик, помогающий вращать стрелку. На нулевом слое нарисован цифеблат и задний фон.
Код AS1/AS2:
//calculates degrees rotation from y-axis based on target & origin point coordinates
function getCurrentAngle(target_x, target_y, origin_x, origin_y) {
	xChange = target_x-origin_x;
	yChange = target_y-origin_y;
	if (xChange<0) {
		xChange_pos = Number(-1*xChange);
	} else {
		xChange_pos = Number(xChange);
	}
	if (yChange<0) {
		yChange_pos = Number(-1*yChange);
	} else {
		yChange_pos = Number(yChange);
	}
	convert = 57.2957795;
	if (xChange>0 && yChange>0) {
		angle = (convert*Math.atan(xChange_pos/yChange_pos));
	}
	if (xChange>0 && yChange<0) {
		angle = (convert*Math.atan(yChange_pos/xChange_pos))+90;
	} else if (xChange<0 && yChange<0) {
		angle = (convert*Math.atan(xChange_pos/yChange_pos))+180;
	} else if (xChange<0 && yChange>0) {
		angle = (convert*Math.atan(yChange_pos/xChange_pos))+270;
	}
	return angle;
}
//to find the x & y edges of an object, regardless of shape or rotation
function getEdges() {
	leftEdge = _parent._x-Number(_parent._width)/2;
	rightEdge = _parent._x+Number(_parent._width)/2;
	topEdge = _parent._y-Number(_parent._height)/2;
	bottomEdge = _parent._y+Number(_parent._height)/2;
}
//mouseTarget mc gets dragged by the user's cursor, x & y coordinates will be used to calculate rotation
arrow.onRollOver = function() {
	startDrag(_parent.mouseTarget, true);
};
//rotates the mc
arrow.onPress = function() {
	startAngle = getCurrentAngle(_parent.mouseTarget._x, _parent.mouseTarget._y, _x, _y);
	onEnterFrame = function () {
		newAngle = getCurrentAngle(_parent.mouseTarget._x, _parent.mouseTarget._y, _x, _y);
		changeAngle = startAngle-newAngle;
		getEdges();
		if (leftEdge>0 && rightEdge<stageRight && topEdge>0 && bottomEdge<stageBottom) {
			_rotation = _rotation+changeAngle;
		} else {
			if (leftEdge<0 || rightEdge>stageRight || topEdge<0 || bottomEdge>stageBottom) {
				_rotation = _rotation+changeAngle;
			}
		}
		startAngle = Number(newAngle);
	};
};
//stops rotating the mc, and ensures that the edges of the clip are within the limits of the draggable area
arrow.onRelease = allCorners.onReleaseOutside=function () {
	onEnterFrame = null;
	onEnterFrame = function () {
		getEdges();
		if (leftEdge>0 && rightEdge<stageRight && topEdge>0 && bottomEdge<stageBottom) {
			onEnterFrame = null;
		} else {
			_parent._rotation = _parent._rotation-changeAngle;
		}
	};
};
Стрелка действительно вращается мышкой нормально, только иногда странно начинает вращаться всё изображение часов вместе с задним фоном, тот есть весь нулевой слой, вокруг левого верхнего угла с разной скоростью.
Исходный код не мой, я его правлю.
Не подскажете, где глюк?