Copy link to clipboard
Copied
Hello! Help me please to decide mission. It's difficult for me yet:(
Mission: Object includes 9 elements (box1, box2...). Can I make so that when some element from object approach to centre stage, it stopped (magnetic, takers) to centre? And what function I can use?
I'm using this code:
var ms:MouseSpeed = new MouseSpeed();
var xSpeed:Number = 0;
var ySpeed:Number = 0;
var friction:Number = 0.85;
var offsetX:Number = 0;
var offsetY:Number = 0;
object.buttonMode = true;
object.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
object.addEventListener(Event.ENTER_FRAME, throwobject);
park_mc.height=stage.stageHeight*3;
park_mc.width= stage.stageWidth*3;
function mouseDownHandler(e:MouseEvent):void
{
stage.addEventListener(Event.ENTER_FRAME, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
offsetX = mouseX - object.x;
offsetY = mouseY - object.y;
}
function mouseUpHandler(e:MouseEvent):void
{
stage.removeEventListener(Event.ENTER_FRAME, drag);
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
xSpeed = ms.getXSpeed();
ySpeed = ms.getYSpeed();
}
function drag(e:Event):void
{
object.x = mouseX - offsetX;
object.y = mouseY - offsetY;
}
function throwobject(e:Event)
{
object.x += xSpeed;
object.y += ySpeed;
object1.x -= (object1.x - mouseX) /16;
object1.y -= (object1.y - mouseY) /16;
xSpeed *= friction;
ySpeed *= friction;
if(object.x > park_mc.width - object.width/9)
{
object.x = park_mc.width - object.width/9 ;
xSpeed *= -1;
}
if(object.x < object.width/9 )
{
object.x = object.width/9 ;
xSpeed *= -1;
}
if(object.y > park_mc.height - object.height/9 )
{
object.y = park_mc.height - object.height/9 ;
ySpeed *= -1;
}
if(object.y < object.height/9 )
{
object.y = object.height/9;
ySpeed *= -1;
}
}
Thank you!
1 you should terminate your throwobject loop when you're dragging object and restart when object is dropped.
2. have both throwobject and drag call a new function where you loop through object's children checking for stage-center proximity. if a child is "close", reparent it so it's no longer being affected by object's movement.
Copy link to clipboard
Copied
1 you should terminate your throwobject loop when you're dragging object and restart when object is dropped.
2. have both throwobject and drag call a new function where you loop through object's children checking for stage-center proximity. if a child is "close", reparent it so it's no longer being affected by object's movement.
Copy link to clipboard
Copied
Thank you very much, I'll try to do it.
Copy link to clipboard
Copied
you're welcome.
Copy link to clipboard
Copied
Hello again! I'm so sorry for troubling you. I try decide it so:
addListeners(object.box1, object.box2, object.box3, object.box4, object.box5,object.box6,object.box7,object.box8,object.box9);
function addListeners(... objects):void
{
for (var i:int = 0; i < objects.length; i++)
{
objects.addEventListener(MouseEvent.MOUSE_UP, stopDragObject);
}
}
function stopDragObject(e:MouseEvent):void
{
if (e.target.hitTestObject(bin))
{
object.removeEventListener(Event.ENTER_FRAME, throwobject)
object.x = bin.x;
object.y = bin.y;
}
else
{
object.addEventListener(Event.ENTER_FRAME, throwobject)
}
}
And I understand that it's not correctly, but I don't know how it set right. Please, don't leave me in my problem, can you help me?
Now always fixed only box9 and speed of movement is disappear.
Thank you.
Copy link to clipboard
Copied
var ms:MouseSpeed = new MouseSpeed();
var xSpeed:Number = 0;
var ySpeed:Number = 0;
var friction:Number = 0.85;
var offsetX:Number = 0;
var offsetY:Number = 0;
object.buttonMode = true;
object.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
park_mc.height=stage.stageHeight*3;
park_mc.width= stage.stageWidth*3;
function mouseDownHandler(e:MouseEvent):void
{
stage.addEventListener(Event.ENTER_FRAME, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
offsetX = mouseX - object.x;
offsetY = mouseY - object.y;
object.removeEventListener(Event.ENTER_FRAME, throwobject);
}
function mouseUpHandler(e:MouseEvent):void
{
stage.removeEventListener(Event.ENTER_FRAME, drag);
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
xSpeed = ms.getXSpeed();
ySpeed = ms.getYSpeed();
object.addEventListener(Event.ENTER_FRAME, throwobject);
}
function drag(e:Event):void
{
object.x = mouseX - offsetX;
object.y = mouseY - offsetY;
checkChildrenF();
}
function throwobject(e:Event)
{
object.x += xSpeed;
object.y += ySpeed;
object1.x -= (object1.x - mouseX) /16;
object1.y -= (object1.y - mouseY) /16;
xSpeed *= friction;
ySpeed *= friction;
if(object.x > park_mc.width - object.width/9)
{
object.x = park_mc.width - object.width/9 ;
xSpeed *= -1;
}
if(object.x < object.width/9 )
{
object.x = object.width/9 ;
xSpeed *= -1;
}
if(object.y > park_mc.height - object.height/9 )
{
object.y = park_mc.height - object.height/9 ;
ySpeed *= -1;
}
if(object.y < object.height/9 )
{
object.y = object.height/9;
ySpeed *= -1;
}
checkChildrenF();
}
function checkChildrenF():void{
for(var i:int=0;i<object.numChildren;i++){
var c:DisplayObject=object.getChildAt(i);
if(distF(c,stage.stageWidth/2,stage.stageHeight/2)<400){
stage.addChild(c);
}
}
}
function distF(dobj:DisplayObject,x:Number,y:Number):Number{
return (dobj.x-x)*(dobj.x-x)+(dobj.y-y)*(dobj.y-y); // actually square of distance
}
Copy link to clipboard
Copied
I'm very thankful!!! You're a genius!!! I haven't ever cope without you!!! Thank you very much!!!
Copy link to clipboard
Copied
you're welcome.