Search code examples
flashflash-cs6

Flash Error Code 1046: Type was not found etc


I'm relatively new to the programming world, so bear with me. I am trying to make a program tutorial work where I am supposed to make a little dude on my screen move around but i keep getting error messages concerning different things. The issue I am stuck on now is error code 1046: Type was not found or was not compile-time constant: player

I tried looking up previous errors but I found none similar to my own. I might be a shallow researcher but i'd rather just straight up ask what is wrong and fix the issue.

I am using Flash CS6, and here is the code:


package {
    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import flash.events.Event;

    public class Main_Character extends MovieClip {
        var vx: int;
        var vy: int;

        public

        function Main_Character() {
            init();
        }

        function init(): void {
            vx = 0;
            vy = 0;

            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        function onKeyDown(event: KeyboardEvent): void {
            if (event.keyCode == Keyboard.LEFT) {
                vx = -5;
            } else if (event.keyCode == Keyboard.RIGHT) {
                vx = 5;
            } else if (event.keyCode == Keyboard.UP) {
                vy = -5;
            } else if (event.keyCode == Keyboard.DOWN) {
                vy = 5;
            }
        }

        function onKeyUp(event: KeyboardEvent): void {
            if (event.keyCode == Keyboard.LEFT)(event.keyCode == Keyboard.RIGHT) {
                vx = 0;
            }
            if (event.keyCode == Keyboard.DOWN)(event.keyCode == Keyboard.UP) {
                vy = 0;
            }
        }

        function onEnterFrame(event: Event): void {
            player.x += vx;
            player.y += vy;
        }
    }
}

I tried to fix a few points before but it didn't go well. ANy help would be fantastic! --C


Solution

  • Your class is referencing player - which I assume is the graphic element you are wanting to move on screen - but it doesn't know what it actually is.

    If this class is linked to the player item in the library then you could try removing the reference and simply having:

    function onEnterFrame(event: Event): void{
        x += vx;
        y += vy;
    }
    

    This will apply the x and y properties to the linked instance, as if you had specified this.x and this.y.

    If this class is separate to the player instance then you need to tell it what player is. You could do this by passing a reference in when you set up your instance of Main_Character

    Hope this makes some sense. It's hard to be more specific without knowing exactly how the rest of your build is structured and how the elements are set up in relation to one another. If you have any more details then I will try to answer in a more coherent manner after I've drunk some more coffee.