Skip to main content
Participant
July 31, 2022
Question

Moving object

  • July 31, 2022
  • 3 replies
  • 263 views

I have two objects. I want to move object1 to the right, if object1 is clicked. Then, I want to move object2 to the right, if object2 is clicked, with the condition: object2 cannot move before object1 moved.

This topic has been closed for replies.

3 replies

JoãoCésar17023019
Community Expert
Community Expert
August 3, 2022

Hi.

 

A tween approach could be like this:

import flash.events.MouseEvent;
import fl.motion.easing.Cubic;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;

function enableFirstInstance():void
{
	instance1.addEventListener(MouseEvent.CLICK, moveFirstInstance);
}

function enableSecondInstance(e:TweenEvent):void
{
	instance2.addEventListener(MouseEvent.CLICK, moveSecondInstance);
}

function moveFirstInstance(e:MouseEvent):void
{
	var tween:Tween = new Tween(e.currentTarget, "x", Cubic.easeInOut, e.currentTarget.x, e.currentTarget.x + 100, 1, true);
	tween.addEventListener(TweenEvent.MOTION_FINISH, enableSecondInstance);
}

function moveSecondInstance(e:MouseEvent):void
{
	var tween:Tween = new Tween(e.currentTarget, "x", Cubic.easeInOut, e.currentTarget.x, e.currentTarget.x + 100, 1, true);
}

enableFirstInstance();

 

I hope it helps.

 

Regards,

JC

Participant
August 3, 2022
thank you, it works.
And morequestion, what kind of script should I write if I just wanna move
the object up to a certain point on the x?
JoãoCésar17023019
Community Expert
Community Expert
August 3, 2022

You can use Math.min in the 5th argument (destination value) when a new Tween is created. Like this:

// in this way the instance will move 100 pixels to the right from the current position
// but it will never go beyond 500 pixels in the parent's coordinate system
var tween:Tween = new Tween(e.currentTarget, "x", Cubic.easeInOut, e.currentTarget.x, Math.min(e.currentTarget.x + 100, 500), 1, true);

 

Community Expert
August 3, 2022

I don't know if this code will help but you can try this tutorial. https://www.youtube.com/watch?v=yVB85hKohmY

Participant
August 3, 2022

Ok, thank you, i will try that

Participant
July 31, 2022

How to write actionscript for that?