Skip to main content
Participant
Jul 31, 2022 3:07 PM UTC
Question

Moving object

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 is closed to new replies. Start a new post to keep the conversation going.

7 replies

JoãoCésar17023019
Community Expert
Community Expert
Aug 3, 2022 11:23 AM UTC

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

Translate
Participant
Aug 3, 2022 7:29 PM UTC
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?
Translate
JoãoCésar17023019
Community Expert
Community Expert
Aug 3, 2022 9:29 PM UTC

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);

 

Translate
Community Expert
Aug 3, 2022 12:27 AM UTC

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

Translate
Participant
Aug 3, 2022 1:32 AM UTC

Ok, thank you, i will try that

Translate
Participant
Jul 31, 2022 3:09 PM UTC

How to write actionscript for that?

Translate