0

This is my custom event class:

package{
 import flash.events.Event;

 public class PetEvent extends Event{
      public static const ON_CRASH:String = "onCrash";

      public function PetEvent(type:String, bubbles:Boolean=true, cancelable:Boolean=false):void{
           super(type, bubbles, cancelable);
      }

      override public function clone():Event {
           return new PetEvent(type, bubbles, cancelable);
      }
 }
}

This is my game handler. I create a new instance of the class Surf from which I want to listen from.

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

public class GameHandler extends MovieClip {
    public var newGame:Surf;

    public function GameHandler() {
        newGame = new Surf();
        newGame.addEventListener(PetEvent.ON_CRASH, onCrash);
        addChild(newGame);
    }

    public function onCrash(petEvent:PetEvent):void{
        trace("MPAM");
        var gameOver:GameOver = new GameOver(stage.stageWidth, stage.stageHeight);
        addChild(gameOver);

        newGame = null;
    }
}
}

And the relevant lines from the Surf class:

public function startSurfing(timerEvent:TimerEvent):void
{
    moveCatandDog();
    for each ( var boat:Boat in armada)
    {
        boat.moveBoat(boatSpeed);
        if ( cat.hitTestObject(boat) || dog.hitTestObject(boat) )
        {
            dispatchEvent( new PetEvent(PetEvent.ON_CRASH) );
            gameTimer.stop();
        }
    }
}

So when Surf detects a crash I want it to send the event to GameHandler and GameHandler will create a GameOver instance.

I have tried everything and I don't even get a trace. I normally don't ask questions but this is for a uni project and I'm running out of time. I would really appreciate any feedback. Thanks!

Sunil D.
  • 17,983
  • 6
  • 53
  • 65
  • Everything you've got here looks good, I would suggest the issue lies within the method dispatching that event within the Surf class. Can you post the full Surf class (or at least the method within it dispatching the event)? – Marty Feb 25 '13 at 22:57
  • 1
    Also, you don't need a custom Event Class here, since you're not adding anything custom. You might want to consider just creating a PetEventKind enumeration Class with your pet event type constants, or just making ON_CRASH a static property of Surf. – Amy Blankenship Feb 26 '13 at 00:02
  • Here is the method dispatching the event. The rest of the class is too big and irrelevant. I haven't declared any variables or made any specific imports regarding this event. public function startSurfing(timerEvent:TimerEvent):void { moveCatandDog(); for each ( var boat:Boat in armada){ boat.moveBoat(boatSpeed); if ( cat.hitTestObject(boat) || dog.hitTestObject(boat) ){ dispatchEvent( new PetEvent(PetEvent.ON_CRASH) ); gameTimer.stop(); } } } I get the timer stop but i never get a trace or the gameover screen. – user2109044 Feb 26 '13 at 00:44
  • sorry for the bad format i dont know how to do it in the comments – user2109044 Feb 26 '13 at 00:55
  • @AmyBlankenship Could you please tell me how to do that? Sorry if it's a stupid question I just started learning flash 5 days ago :) – user2109044 Feb 26 '13 at 00:55
  • @user2109044 I've edited your question and added your `startSurfing()` method to it ... you can edit the question if necessary. So now the obvious question is are you sure that the condition in the `if` statement in the `startSurfing()` method is ever true? That is, are those hit tests working. If not, that would be one reason no event gets dispatched. You can add a `trace("hit test succeeded")` statement inside that if statement and check your console for the output to see if that is working. – Sunil D. Feb 26 '13 at 01:11
  • @SunilD. Hi, thanks for editing the question. I know the ifs become true because I move the characters and make them crash onto the boats and then the screen freezes which means the gametimer stops. But then I am supposed to get the gameover screen or at least the trace but i dont get either, so i suppose the dispatch doesnt work. – user2109044 Feb 26 '13 at 01:35

1 Answers1

0

Problem solved!

I had to change my Document Class to GameHandler and make a public static variable of stage.

Previously I had Surf as my Document Class because I had some keyboard listeners set to the stage.

So the PetEvent and the dispatch in Surf were correct. I changed the GameHandler as shown below, with another solution I found in StackOverflow.

Inside the constructor of GameHandler, if the stage is ready (not null) it sets it to the public static variable STAGE (via the init function), otherwise it adds a listener and when the stage is ready it does the same thing and removes the listener.

package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;

public class GameHandler extends MovieClip {
    public var newGame:Surf;

    public static var STAGE:Stage;

    public function GameHandler() {
        if (stage){
            init();
        } else {
            addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
        }

        newGame = new Surf();
        newGame.addEventListener(PetEvent.ON_CRASH, onCrash);
        addChild(newGame);
    }

    private function init(e:Event=null):void{
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // store stage reference when stage ready  
        STAGE=stage;
    }

    public function onCrash(petEvent:PetEvent):void{
        var gameOver:GameOver = new GameOver(stage.stageWidth, stage.stageHeight);
        addChild(gameOver);

        newGame = null;
    }
}
} 

and I imported GameHandler into Surf with:

import GameHandler;    

so I can set the listeners in Surf to GameHandler.STAGE.addEventListener (...)

Thanks everyone for the suggestions!