Skip to main content
Inspiring
December 10, 2013
Question

Remove background and create a new one

  • December 10, 2013
  • 1 reply
  • 696 views

Hi,

I'm making an AS3 game, using the ALAPCA Engine source.

I want to change the background of the scene when the timer is complete.

Normaly, you must click on the exit arrow in order to change the background.

The code is defined in the Background.as and it is like that :

private function leaveRoom(e:MouseEvent):void{

                              player = Engine.player;

                              if (Engine.playerControl){

                                        stageRef.dispatchEvent(new Event("itemClicked"));

                                        var thisExit = e.currentTarget;

                                        //targetBuffer = player.returnTargetBuffer();

                                        var underscore = thisExit.name.search("_");

                                        var nextRoom:String = thisExit.name.substring(underscore + 1);

                                        Engine.newBack = nextRoom;

                                        exitPoint = "startPoint_"+nextRoom;

                                        exitLoc[0] = currentBack[exitPoint].x;

                                        exitLoc[1] = currentBack[exitPoint].y;

 

                                        player.addEventListener("reachedPoint", reachedExit, false, 0, true);

 

                                        // Make sure the player isn't already at the exit point

                                        if (checkForExit()){

                                                  player.dispatchEvent(new Event("reachedPoint"));

                                        } else {

                                                  player.startWalking(exitLoc[0], exitLoc[1]);

                                        }

                              }

 

                    }

 

                    private function reachedExit(e:Event):void{

                              if (checkForExit()){

                                        Engine.lastLocation = currentBack.name;

                                        stageRef.dispatchEvent(new Event("changeBackground"));

                              }

I've made in my puzzle.as class (where all the puzzles are) that :

public function shoot(e:MouseEvent):void{

                              var cocoUn;

                              for (var i in Engine.usableItems){ // Blurgh

                                                            if (Engine.usableItems.displayName == "COCOUN")

                                                                      cocoUn = Engine.usableItems;

                                                  }

                              cocoUn.gotoAndStop("fall");

                              timeCoco.start();

                              timeCoco.addEventListener(TimerEvent.TIMER_COMPLETE, backToJardin);

                    }

So when it’s finished I want to call a function name “backToJardin” that make the player return to the previous background (named "jardin"):

I've put this :

public function backToJardin(e:Event):void{

Engine.newBack = “jardin”;

stageRef.dispatchEvent(new Event(“changeBackground”));

But it is not working... Here the error :

TypeError: Error #1009: Cannot access a property or method of a null object reference.

at com.laserdragonuniversity.alpaca::Engine/createBackground()

at com.laserdragonuniversity.alpaca::Engine/changeBackground()

at flash.events::EventDispatcher/dispatchEventFunction()

at flash.events::EventDispatcher/dispatchEvent()

at flash.display::Stage/dispatchEvent()

at com.laserdragonuniversity.alpaca::Puzzle/backToJardin()

at flash.events::EventDispatcher/dispatchEventFunction()

at flash.events::EventDispatcher/dispatchEvent()

at flash.utils::Timer/tick()

Anyone know what could be the problem ?

Here is my puzzle.as and my background.as files if it's necessary.

Thank you for your help,

Stephan

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
December 10, 2013

stageRef is probably null.  use the trace function to confirm:

trace(stageRef);

Inspiring
December 10, 2013

I've put trace(stageRef) to the backToJardin function. is that right ?

Here's the result :

[object Stage]

Creating new background: jardin

TypeError: Error #1009: Cannot access a property or method of a null object reference.

kglad
Community Expert
Community Expert
December 10, 2013

that's too bad because stageRef is not the problem and it's therefore probably Engine.newBack that's triggering the problem.  and that means there's probably no easy solution because you'll need to edit Engine.as

but what's line 2117 in Puzzle.as?  that's the line that triggers the error in Engine.as