0
votes

The goal is to get a background to loop constantly (a side scrolling-type of game). I found some code online, it works in the 'Actions' panel in Flash, however I've been told I cannot have anything in my 'Actions' panel and that all my code has to be in an ActionScript File (.as). Is there a way to execute this code in the actionscript file rather than in the actions window? How? -Thanks so much, I really appreciate it!

//The speed of the scroll movement.
var scrollSpeed:uint = 2;

//This adds two instances of the movie clip onto the stage.
var s1:ScrollBg = new ScrollBg();
var s2:ScrollBg = new ScrollBg();
addChild(s1); 
addChild(s2);

//This positions the second movieclip next to the first one.
s1.x = 0;
s2.x = s1.width;

//Adds an event listener to the stage.
stage.addEventListener(Event.ENTER_FRAME, moveScroll); 

//This function moves both the images to left. If the first and second 
//images goes pass the left stage boundary then it gets moved to 
//the other side of the stage. 
function moveScroll(e:Event):void{
s1.x -= scrollSpeed;  
s2.x -= scrollSpeed;  

if(s1.x < -s1.width){
s1.x = s1.width;
}else if(s2.x < -s2.width){
s2.x = s2.width;
}
}
1

1 Answers

0
votes
package {
  import flash.display.MovieClip;
  import flash.events.Event;

  public class Main extends MovieClip{

    //The speed of the scroll movement.
    var scrollSpeed:uint = 2;

    //This adds two instances of the movie clip onto the stage.
    var s1:ScrollBg = new ScrollBg();
    var s2:ScrollBg = new ScrollBg();
    addChild(s1); 
    addChild(s2);

    //This positions the second movieclip next to the first one.
    s1.x = 0;
    s2.x = s1.width;

    //Adds an event listener to the stage.
    stage.addEventListener(Event.ENTER_FRAME, moveScroll); 

    //This function moves both the images to left. If the first and second 
    //images goes pass the left stage boundary then it gets moved to 
    //the other side of the stage. 
    function moveScroll(e:Event):void{
      s1.x -= scrollSpeed;  
      s2.x -= scrollSpeed;  

      if(s1.x < -s1.width){
        s1.x = s1.width;
      }else if(s2.x < -s2.width){
        s2.x = s2.width;
      }
    }
  }
}

change what you have to this and save it as a .as file to the same folder your main .fla is in.

EDIT Also, make sure in your main .fla file that the main class is set a Main.as, if you don't know where that is then just look it up.