Skip to main content
Known Participant
December 9, 2022
Answered

Action on a list of objects

  • December 9, 2022
  • 1 reply
  • 714 views

Hi there

I was used to play with Flash several years ago and today i'm trying to create some funny staff in order to learn AS3 and Animate basics.

 

I have a "truck" movie clip on my scene. This movie clip has a stop(); action on the first frame and a gotoAndPlay(2); on the last frame of its timeline.

 

When the global timeline plays i wrote this code in order to get the truck bigger and to get it play if the truck's x is greater than 300.

 

I have 3 questions:

1 -  is it possible to simplify my code specify x > 300 and < 900

2 - is it possible to scale x and y in the same time (rather than scaleX and scaleY)

3 - I would like to apply this code to several other movie clips, is there a way to for example list my objects and then apply my code to all this objects

 

 

addEventListener(Event.ENTER_FRAME, transform_objects);

function transform_objects(event:Event):void
{

	truck.x += 5;
	if(truck.x >= 1400){
		truck.x = -150;
	}
if(truck.x >= 300){
		truck.play();
	truck.scaleX = 1.4;
	truck.scaleY = 1.4;
	truck.alpha = 1;
}
if(truck.x >= 900){
		truck.gotoAndStop(1);
	truck.scaleX = 1;
	truck.scaleY = 1;
	truck.alpha = 0.16;
}
}

 

 

Thank you very much for your help

 

Regards

Miran

    This topic has been closed for replies.
    Correct answer kglad

    if there are more than say 3 object, and they're going to be treated as a group in code (ie, when one undergoes a property change, the others will too), it's easier to use and maintain if they're listed in an array:

     

     

    var objectA:Array = [A,B,C,D,E,...]; 

     

    addEventListener(Event.ENTER_FRAME, transform_objects);

     

    function transform_objects(event:Event):void {

    for(var i:int=0;i<objectA.length;i++){

    objectA[i].x += 5;

    if(objectA[i].x >= 1400){
    objectA[i].x = -150;
    }

    if(objectA[i].x>=300 && objectA[i[.x <=900){

    objectA[i].play();

    objectA[i].scaleX = 1.4;

    objectA[i].scaleY = 1.4

    }

    }

    }

    1 reply

    kglad
    Community Expert
    Community Expert
    December 9, 2022

    you could use:

     

    addEventListener(Event.ENTER_FRAME, transform_objects);

    function transform_objects(event:Event):void
    {

    truck.x += 5;
    if(truck.x >= 1400){
    truck.x = -150;
    }
    if(truck.x >= 300 && truck.x <= 900){  // note: your previous code was incorrect (or your q 1 was)
    truck.play();
    truck.scaleX = 1.4;  // see below
    truck.scaleY = 1.4;  // you could use truck.scaleX = truck.scaleY = 1.4, but that's not really a simplification
    truck.alpha = 1;
    }
    }

    lemiranAuthor
    Known Participant
    December 9, 2022

    Hi kglad

    Thank you very much for your reply.

    I coorrected the error, you are right!

    The simplification is indeed very usefull as i can change in the future this value for both (x and y) at the same time.

     

    So is there a trick to apply this code (transform the object if its x is between 300&900) to several objects.

    I tought that it would be possible to say to animate :

    Here is my list of objects : A,B,C,D,E...)

    If any of my objects is in a particular x position, then transform it

    kglad
    Community Expert
    kgladCommunity ExpertCorrect answer
    Community Expert
    December 9, 2022

    if there are more than say 3 object, and they're going to be treated as a group in code (ie, when one undergoes a property change, the others will too), it's easier to use and maintain if they're listed in an array:

     

     

    var objectA:Array = [A,B,C,D,E,...]; 

     

    addEventListener(Event.ENTER_FRAME, transform_objects);

     

    function transform_objects(event:Event):void {

    for(var i:int=0;i<objectA.length;i++){

    objectA[i].x += 5;

    if(objectA[i].x >= 1400){
    objectA[i].x = -150;
    }

    if(objectA[i].x>=300 && objectA[i[.x <=900){

    objectA[i].play();

    objectA[i].scaleX = 1.4;

    objectA[i].scaleY = 1.4

    }

    }

    }