(AS3 / Flash的新手,如果我对某些东西一无所知,那就轻松一点......)
尝试利用外部类文件创建连续滚动背景图像。我把它放在文档类文件中,但是尝试将它放在自己的外部类文件中并从文档类文件中调用它会在我的标题中出现错误。
文档类文件:
package {
import flash.display.MovieClip;
import org.masteringmoneybasics.piggy._class_BG
public class Main extends MovieClip {
public function Main() {
//Create instance of background class
new _class_BG();
}
}
}
外部类文件:
package org.masteringmoneybasics.piggy {
import flash.display.*
import flash.events.Event
import flash.display.Bitmap;
import flash.display.BitmapData;
public class _class_BG{
//BG Variables
var scrollSpeed:uint = 6;
var bgLeft:Bitmap
var bgRight:Bitmap
[Embed(source="../../../assets/side_of_mountain.png")]
private var bgImage:Class;
public function _class_BG() {
//This adds two instances of the background to the stage
bgLeft = new bgImage();
bgRight = new bgImage();
bgLeft.height = 500;
bgRight.height = bgLeft.height;
bgLeft.width = 1300;
bgRight.width = bgLeft.width;
bgLeft.x = 0;
bgRight.x = bgLeft.width;
addChild(bgLeft);
addChild(bgRight);
//Adds an event lsitener to the stage
stage.addEventListener(Event.ENTER_FRAME, moveScroll); //<<<<<< ERROR HERE
}
public function moveScroll(e:Event):void{
bgLeft.x -= scrollSpeed;
bgRight.x -= scrollSpeed;
if(bgLeft.x < -bgLeft.width){
bgLeft.x = bgRight.x + bgRight.width;
}else if(bgRight.x < -bgRight.width){
bgRight.x = bgLeft.x + bgLeft.width;
}
}
}
}
如果我删除舞台。在事件监听器中的引用,它运行没有错误,但图像不会像它们应该的那样出现在舞台上。
我做错了什么?
答案 0 :(得分:0)
只有顶级Displayable可以访问场景。更重要的是,它是只读的,这意味着你无法通过原始阶段属性传递它。
最简单的方法是......我不知道,也许将舞台传递给你的构造函数?构造函数的相关部分是:
public function _class_BG(myStage : Stage) {
// SNIP
//Adds an event lsitener to the stage
myStage.addEventListener(Event.ENTER_FRAME, moveScroll);
}
在Main中(你可以访问舞台):
public class Main extends MovieClip {
public function Main() {
//Create instance of background class
addChild(new _class_BG(stage));
}
}
你应该考虑建立你的逻辑的其他方法,通过阶段将很快变得毛茸茸。但它应该有用。
修改强>
阶段 - &gt; myStage;另外,在Main()中添加addChild。
答案 1 :(得分:0)
您尝试在阶段初始化的Main类外部类中。事实上,阶段没有到来。看到你的_class_BG,addChild()有点不对劲。因为你没有检查Main class,完全添加了阶段。
FlashBuilder这个问题一定要小心。在外部类(相关的DisplayObject)完全加载或初始化之后,首先在Main类上添加阶段。
参考以下代码。
在_class_BG Class中谨慎addEventListener(Event.ADDED_TO_STAGE,init);
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
var sp:_class_BG = new _class_BG();
addChild(sp);
}
}
}
package {
import flash.display.*;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
public class _class_BG extends Sprite {
//BG Variables
private var scrollSpeed:uint = 6;
private var bgLeft:Bitmap
private var bgRight:Bitmap
[Embed(source="../asset/myTestImage.png")]
private var bgImage:Class;
public function _class_BG()
{
if(!stage)
addEventListener(Event.ADDED_TO_STAGE, init);
else
init();
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//This adds two instances of the background to the stage
bgLeft = new bgImage();
bgRight = new bgImage();
bgLeft.height = 500;
bgRight.height = bgLeft.height;
bgLeft.width = 1300;
bgRight.width = bgLeft.width;
bgLeft.x = 0;
bgRight.x = bgLeft.width;
addChild(bgLeft);
addChild(bgRight);
//Adds an event lsitener to the stage
stage.addEventListener(Event.ENTER_FRAME, moveScroll);
}
public function moveScroll(e:Event):void{
bgLeft.x -= scrollSpeed;
bgRight.x -= scrollSpeed;
if(bgLeft.x < -bgLeft.width){
bgLeft.x = bgRight.x + bgRight.width;
}else if(bgRight.x < -bgRight.width){
bgRight.x = bgLeft.x + bgLeft.width;
}
}
}
}