Skip navigation
Currently Being Moderated

Hello! Help me please to decide mission.Hello! Help me please to decide mission.

Aug 3, 2012 12:51 AM

Tags: #problem #cs5 #flash #3.0 #action #actionscript #actionscript3

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!

 
Replies
  • kglad
    72,260 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 3, 2012 7:12 AM   in reply to createflash@mail.ru

    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.

     
    |
    Mark as:
  • kglad
    72,260 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 3, 2012 11:31 AM   in reply to createflash@mail.ru

    you're welcome.

     
    |
    Mark as:
  • kglad
    72,260 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 4, 2012 10:04 AM   in reply to createflash@mail.ru

    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
    }
     
    |
    Mark as:
  • kglad
    72,260 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 5, 2012 8:49 AM   in reply to createflash@mail.ru

    you're welcome.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points