【问题标题】:Why does this code work in the ActionScript Panel and not in the ActionScript file? How can I solve this?为什么此代码在 ActionScript 面板中有效,而在 ActionScript 文件中无效?我该如何解决这个问题?
【发布时间】:2013-11-29 09:54:36
【问题描述】:

目标是让背景不断循环(横向滚动类型的游戏)。我在网上找到了一些代码,它可以在 Flash 的“操作”面板中运行,但是有人告诉我,我的“操作”面板中不能有任何内容,并且我的所有代码都必须在 ActionScript 文件 (.as) 中。有没有办法在 actionscript 文件中而不是在操作窗口中执行此代码?如何? - 非常感谢,我真的很感激!

//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;
}
}

【问题讨论】:

    标签: actionscript-3 flash actionscript


    【解决方案1】:
    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;
          }
        }
      }
    }
    

    更改您所拥有的内容并将其作为 .as 文件保存到您的主 .fla 所在的同一文件夹中。

    编辑 另外,请确保在您的主 .fla 文件中将主类设置为 Main.as,如果您不知道它在哪里,那么只需查找它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 2011-05-25
      • 2013-12-17
      • 2022-10-07
      • 2021-07-05
      • 2011-06-05
      相关资源
      最近更新 更多