Цитата:
Сообщение от Dr_Bra
не могу догадаться по какой формуле рассчитывать расстояние от центра сцены до положения курсора... подскажите?
|
Недавно делал что то подобное. Вобщем ложим спрайт на сцену верхним слоем. И считаем координаты от его центра. А ниже этого спрайта уже ложется фон.
Для понятности мой код:

Код AS3:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
/**
* ...
* @author Fillini
*/
public class Map extends MovieClip
{
private var speedX:Number = 1;
private var speedY:Number = 1;
private var timer:Timer = new Timer(10);
private var DirectionX:int = 0;
private var DirectionY:int = 0;
public static var X:Number;
public static var Y:Number;
public function Map()
{
addEventListener(Event.ADDED_TO_STAGE, init);
rect.width = 1024;
rect.height = 768;
}
private function init(e:Event):void
{
rect.width = 1024;
rect.height = 768;
removeEventListener(Event.ADDED_TO_STAGE, init);
rect.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
}
private function onMove(e:MouseEvent):void
{
DirectionX = 0;
DirectionY = 0;
if (e.localX<rect.width/2-100)
{
DirectionX = 1;
}
if (e.localX>rect.width/2+100)
{
DirectionX = -1;
}
if (e.localX==rect.width/2)
{
DirectionX = 0;
}
if (e.localY<rect.height/2-100)
{
DirectionY = 1;
}
if (e.localY>rect.height/2+100)
{
DirectionY = -1;
}
if (e.localY==rect.height/2)
{
DirectionY = 0;
}
var xCenter:int = rect.width / 2;
var yCenter:int = rect.height / 2;
speedX = Math.abs(Math.round((xCenter - e.localX) / 20));
speedY = Math.abs(Math.round((yCenter - e.localY) / 20));
}
private function onTimer(e:TimerEvent):void
{
map.x += 1*DirectionX*(speedX/10);
map.y += 1 * DirectionY * (speedY / 10);
if (map.x >= rect.x)
{
map.x -= 1*DirectionX*(speedX/10);
}
if (map.y >= rect.y)
{
map.y -= 1 * DirectionY * (speedY / 10);
}
if (map.x+map.width <= rect.x+rect.width) {
map.x -= 1*DirectionX*(speedX/10);
}
if (map.y+map.height <= rect.y+rect.height) {
map.y -= 1 * DirectionY * (speedY / 10);
}
X = map.x;
Y = map.y;
dispatchEvent(new Event("MAP", true));
}
}
}
rect - Спрайт.
map - фон.