Search code examples
actionscript-3actionscriptflash-cs5type-coercion

Type coercion in AS3 Flash


I am making a simple jumper game in AS3 in Flash, and at this moment everything works but I get this note:

Error #1034: Type Coercion failed: cannot convert 2 to flash.display.Scene. at scratch_theGame_kat_fla::MainTimeline/startkeyPressed()

I understand there must be a type of an instance that is incorrect when this note appears, but as I search around in my code to find the perpetrator, I realized the "2" that it must be referring to is this:

function startkeyPressed(event:KeyboardEvent):void
{
    if (event.keyCode,Keyboard.ENTER)
    {
        trace("new player");
        startGame = true;
        gotoAndPlay(( 1, Scene (2)));
    }
};

This part of the code is what makes it go (when ENTER is pressed) from scene 1 to scene 2, where the actual game begins. Does anyone have any idea what I should change?


Solution

  • That line makes absolutely no sense in terms of AS3 logic:

    gotoAndPlay(( 1, Scene (2)));
    

    If you look into "Example" section of the official gotoAndPlay(...) help, there are two possible uses of this method:

    1. You pass a single "frame" argument: it is either a 1-based int frame index, or it is a String label of the desired frame.
    2. Two arguments where the second one is the String name of a Scene that is a portion of the main timeline, and the first is, again, the frame index or frame label inside the said Scene.

    Now let's decipher what you have there. You are passing as gotoAndPlay(...) arguments. Feel the difference:

    // Your version: one argument grouped with additional brackets.
    gotoAndPlay(( 1, Scene (2)));
    
    // Alternate version: no additional brackets, two arguments.
    gotoAndPlay(1, Scene (2));
    

    Then, what is Scene (2) expression, you might probably ask? It is called typecasting, an operation where you:

    1. Explicitly state the exact class of something you are working with, because there are moments where compiler does not know exactly, like what is the exact class of Event.target object. There's a nice example, well explained: AS3: cast or "as"?
    2. You want to explicitly convert data from one type to another (the said type coercion), mostly used between int or Number and String types.

    Lets see:

    function onEvent(e:Event):void
    {
        // Explicit typecasting.
        MovieClip(e.target).nextFrame();
    
        // Explicit typecasting through "as" operator.
        var aFrame:int = (e.target as MovieClip).currentFrame;
    
        // Explicit type conversion.
        trace("The current frame is", String(aFrame));
    

    So, you are trying to convince Flash Player that integer value 2 is somehow a Scene object, but there's no way it is, hence the exception thrown by the Flash Player because it does not understand what do you want it to do.

    The correct notation of what you are trying to achieve (moving the playhead to the second Scene) would be probably:

    gotoAndPlay(1, "Scene2");
    

    The "Scene2" argument is a string literal that should represent the exact name of that second scene of yours, I couldn't know what it is but it should probably be named "Scene2" or "Scene 2" by default.