2012-02-09 97 views
0

我使用的SWF(ACCUEIL)另一个SWF(ranchleblanc_fr)的内部,这将导致错误1009显示:错误#1009,动作3

TypeError: Error #1009: Cannot access a property or method of a null object reference. 
    at accueil_fla::MainTimeline/frame1()" 

第一SWF具有在它和它的颗粒的效果我第一次加载页面时不会启动。如果我点击主页面的任何按钮并返回到accueil页面,效果就起作用。但是如果我再次点击另一个按钮,我会得到另一个错误1009,但这次它不仅会出现一次。它一遍又一遍地出现,直到我关闭SWF。

TypeError: Error #1009: Cannot access a property or method of a null object reference. 
at accueil_fla::MainTimeline/newFlake()" 

我将包含的代码,但我做之前,我必须说,我选中该复选框以允许在发布设置调试,我没有行号查找错误。我用了踪迹,发现null变量是flake。我搜索了互联网,找出如何解决我的问题,我想我需要使用if语句,因此请确保程序以null变量运行。我想我应该检查片状变量是否为null,如果是,则声明某种价值。

的问题是,我不知道如何编写一个if语句...

这里是我的accueil_fla代码,第1帧(所有这FLA的行动可以找到)

for (var i:uint = 0; i < 50; i++) 
{ 
    flake.x = Math.random() * stage.stageWidth; 
    flake.y = Math.random() * stage.stageHeight; 
    flake.alpha = randRange(.3,1); 
    flake.scaleX = flake.scaleY = randRange(.3,1); 
    addChild(flake); 
} 

function randRange(low:Number, high:Number):Number 
{ 
    var randNum:Number = Math.random() * (high - low) + low; 
    return randNum; 
} 

this.addEventListener(Event.ENTER_FRAME, newFlake); 

function newFlake(e:Event):void 
{ 
    var flake:Snowflake = new Snowflake(randRange(-1,1), randRange(2,5)); 
    flake.x = Math.random() * stage.stageWidth; 
    flake.y = -5; 
    flake.alpha = randRange(.3,1); 
    flake.scaleX = flake.scaleY = randRange(.3,1); 
    addChild(flake); 
} 
var flake:Snowflake = new Snowflake(randRange(-1,1), randRange(2,5)); 

我还可以包含主页面的代码,以解决雪粒效应在第一次加载时不起作用的原因。

//----------------initialization---------------\\ 

extLoader_mc.visible = false; 

//--------------------loader------------------------\\ 

var loader:Loader = new Loader; 

loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, preload); 
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, done); 

function preload (e:ProgressEvent):void 
{ 
    extLoader_mc.visible = true; 
    var pct:Number = e.bytesLoaded/e.bytesTotal; 
    extLoader_mc.extLoaderBar_mc.scaleX = pct; 
} 

function done (e:Event):void 
{ 
    extLoader_mc.visible = false; 
    canvas_mc.addChild(loader); 
} 

//-----------------------end loader----------------\\ 


var firstLoad:URLRequest = new URLRequest("accueil.swf"); 
loader.load(firstLoad); 
var currentPage:MovieClip; 
currentPage=accueil_mc; 
currentPage.gotoAndPlay("down"); 
accueilHit_mc.targetmc = accueil_mc; 
aProposHit_mc.targetmc = aPropos_mc; 
animauxHit_mc.targetmc = animaux_mc; 
locationHit_mc.targetmc = location_mc; 
contactHit_mc.targetmc = contact_mc; 

accueilHit_mc.buttonMode = true; 
aProposHit_mc.buttonMode = true; 
animauxHit_mc.buttonMode = true; 
locationHit_mc.buttonMode = true; 
contactHit_mc.buttonMode = true; 

function el() 
{ 
    accueilHit_mc.addEventListener(MouseEvent.CLICK, accueilPage); 
    accueilHit_mc.addEventListener(MouseEvent.MOUSE_OVER, btnOver); 
    accueilHit_mc.addEventListener(MouseEvent.MOUSE_OUT, btnOut); 
    //aProposHit_mc.addEventListener(MouseEvent.CLICK, aProposPage); 
    aProposHit_mc.addEventListener(MouseEvent.MOUSE_OVER, btnOver); 
    aProposHit_mc.addEventListener(MouseEvent.MOUSE_OUT, btnOut); 
    //animauxHit_mc.addEventListener(MouseEvent.CLICK, animauxPage); 
    animauxHit_mc.addEventListener(MouseEvent.MOUSE_OVER, btnOver); 
    animauxHit_mc.addEventListener(MouseEvent.MOUSE_OUT, btnOut); 
    locationHit_mc.addEventListener(MouseEvent.CLICK, locationPage); 
    locationHit_mc.addEventListener(MouseEvent.MOUSE_OVER, btnOver); 
    locationHit_mc.addEventListener(MouseEvent.MOUSE_OUT, btnOut); 
    contactHit_mc.addEventListener(MouseEvent.CLICK, contactPage); 
    contactHit_mc.addEventListener(MouseEvent.MOUSE_OVER, btnOver); 
    contactHit_mc.addEventListener(MouseEvent.MOUSE_OUT,btnOut); 
} 

function btnOver (e:MouseEvent):void 
{ 
    e.currentTarget.targetmc.gotoAndPlay("over"); 
} 

function btnOut (e:MouseEvent):void 
{ 
    e.currentTarget.targetmc.gotoAndPlay("out"); 
} 

function handCursor() 
{ 
    accueilHit_mc.useHandCursor = true; 
    aProposHit_mc.useHandCursor = true; 
    animauxHit_mc.useHandCursor = true; 
    locationHit_mc.useHandCursor = true; 
    contactHit_mc.useHandCursor = true; 
    accueilHit_mc.enabled = true; 
    aProposHit_mc.enabled = true; 
    animauxHit_mc.enabled = true; 
    locationHit_mc.enabled = true; 
    contactHit_mc.enabled = true; 
} 
el(); 

accueilHit_mc.removeEventListener(MouseEvent.CLICK, accueilPage); 
accueilHit_mc.removeEventListener(MouseEvent.MOUSE_OVER, btnOver); 
accueilHit_mc.removeEventListener(MouseEvent.MOUSE_OUT, btnOut); 

handCursor(); 

accueilHit_mc.useHandCursor = false; 
accueilHit_mc.enabled = false; 

function accueilPage (e:MouseEvent):void 
{  
    currentPage.gotoAndPlay("out");    
    var req:URLRequest = new URLRequest("accueil.swf"); 
    loader.load(req); 
    el(); 
    accueilHit_mc.removeEventListener(MouseEvent.CLICK, accueilPage); 
    handCursor(); 
    accueilHit_mc.useHandCursor = false; 
    accueilHit_mc.enabled = false; 
    accueilHit_mc.removeEventListener(MouseEvent.MOUSE_OVER, btnOver); 
    accueilHit_mc.removeEventListener(MouseEvent.MOUSE_OUT, btnOut); 
    accueil_mc.gotoAndStop("down"); 
    currentPage=accueil_mc; 
} 

function aProposPage (e:MouseEvent):void 
{ 
    currentPage.gotoAndPlay("out");  
    var req:URLRequest = new URLRequest("aPropos.swf"); 
    loader.load(req); 
    el(); 
    aProposHit_mc.removeEventListener(MouseEvent.CLICK, aProposPage); 
    handCursor(); 
    aProposHit_mc.useHandCursor = false; 
    aProposHit_mc.enabled = false; 
    aProposHit_mc.removeEventListener(MouseEvent.MOUSE_OVER, btnOver); 
    aProposHit_mc.removeEventListener(MouseEvent.MOUSE_OUT, btnOut); 
    aPropos_mc.gotoAndStop("down"); 
    currentPage=aPropos_mc; 
} 

function animauxPage (e:MouseEvent):void 
{ 
    currentPage.gotoAndPlay("out"); 
    var req:URLRequest = new URLRequest("animaux_fr.swf"); 
    loader.load(req); 
    el(); 
    animauxHit_mc.removeEventListener(MouseEvent.CLICK, animauxPage); 
    handCursor(); 
    animauxHit_mc.useHandCursor = false; 
    animauxHit_mc.enabled = false; 
    animauxHit_mc.removeEventListener(MouseEvent.MOUSE_OVER, btnOver); 
    animauxHit_mc.removeEventListener(MouseEvent.MOUSE_OUT, btnOut); 
    animaux_mc.gotoAndStop("down"); 
    currentPage=animaux_mc; 
} 

function locationPage (e:MouseEvent):void 
{ 
    currentPage.gotoAndPlay("out"); 
    var req:URLRequest = new URLRequest("location.swf"); 
    loader.load(req); 
    el(); 
    locationHit_mc.removeEventListener(MouseEvent.CLICK, locationPage); 
    handCursor(); 
    locationHit_mc.useHandCursor = false; 
    locationHit_mc.enabled = false; 
    locationHit_mc.removeEventListener(MouseEvent.MOUSE_OVER, btnOver); 
    locationHit_mc.removeEventListener(MouseEvent.MOUSE_OUT, btnOut); 
    location_mc.gotoAndStop("down"); 
    currentPage=location_mc; 
} 

function contactPage (e:MouseEvent):void 
{ 
    currentPage.gotoAndPlay("out");   
    var req:URLRequest = new URLRequest("contact.swf"); 
    loader.load(req); 
    el(); 
    contactHit_mc.removeEventListener(MouseEvent.CLICK, contactPage); 
    handCursor(); 
    contactHit_mc.useHandCursor = false; 
    contactHit_mc.enabled = false; 
    contactHit_mc.removeEventListener(MouseEvent.MOUSE_OVER, btnOver); 
    contactHit_mc.removeEventListener(MouseEvent.MOUSE_OUT, btnOut); 
    contact_mc.gotoAndStop("down"); 
    currentPage=contact_mc; 
} 

再次感谢您的帮助,非常感谢。

+0

当你将一个.swf加载到第二个时,那么第一个swf不应该有stage的引用。如果你有阶段引用,则会抛出错误。 – 2012-02-09 06:13:09

+0

嗨,欢迎来到Stack Overflow。首先,虽然它确实让你发布整个代码的意图很好,但它不利于你的问题的可读性 - 这是一个**很多代码可读的内容。此外,重新格式化它是一个痛苦:请注意,SO上的代码编辑器预计空白字符是空格字符,而不是标签... – weltraumpirat 2012-02-09 06:26:19

+0

请在执行stage.stageWidth调用之前跟踪(阶段)。你的舞台可能为空。否则看起来不错! – 2012-02-09 14:20:10

回答

2

您的错误很可能不是真的存在于您的代码中,而是存在于您的项目设置中:您似乎引用了当您调用它时不在舞台上的影片剪辑。

您必须将您的时间表视为实际时间表:这些帧代表时间!如果您的按钮动作在另一个影片剪辑中触发了功能,那么它必须在的舞台上- 它不足以让它出现在较晚的帧(或另一场景等等)中。

你应该在你的程序中包含空检查,是的,但它可能无法按预期工作,即使它不再崩溃 - 没有任何东西可以执行命令,实际上没有机制做这项工作......

此外,一旦空对象引用错误已被抛出(或任何其他错误未被捕获,就此而言),你不应该再使用你的程序 - 它被破坏了,你需要修复它,然后才能继续!你当然不能相信第一个错误信息。

把它想象成一个发条:如果任何一个齿轮停止转动,其他轮子都不能转动 - 但只有其中一个会损坏,导致其他所有其他轮子也发生故障。只有在修复第一个错误之后,才能确定确实存在另一个问题。

+0

非常明确的方式来解释时间轴的概念。我已经忘记了这一点。自从我使用flash以来已经有一段时间了。 非常感谢。 – jpl 2012-02-10 02:49:33

1

Here是一篇文章,解释了为什么一些AS3 SWF独立工作,但无法加载到其他SWF中。我希望这会对你有所帮助:

+0

非常感谢。这篇文章真的很有帮助。它帮助我理解可怕的错误1009的概念。 – jpl 2012-02-10 02:48:34