Tom
Tom

Reputation: 13

Inherit MovieClip From Another Class

Hey I seem to be having some problems inheriting Movie-clip from a class, I'm fairly new to as3, I've had a look around and can't tell if I'm doing something fundamentally wrong or not.

Let me show you

So I have a class I want to use to move EVERYTHING but the player sprite. So I want everything but the player to extend it. (or so I'm assuming.)

So I declare my class

package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;

public class everythingContainer extends MovieClip {

function brackets and so on... (I'm just importing everything in an attempt to avoid errors)

I then have a class I want to inherit everythingContainer and Movieclip

package code {

import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
import code.everythingContainer;

public class Tree1 extends everythingContainer {

Yet when I run this I get the error: Line 1 5000: The class 'code.Tree1' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.

Why am I getting this error?

Any help would be greatly appreciated!

I haven't got the full code to run yet so there may still be other obvious bugs laying about.

everythingContainer

Full code:

    package code {
    import flash.display.*;
    import flash.events.*;
    import flash.ui.*;
    import code.Main;

    public class everythingContainer extends MovieClip {
        var speed: Number = 4;
        var wpressed: Boolean = false;
        var apressed: Boolean = false;
        var spressed: Boolean = false;
        var dpressed: Boolean = false;
        var xprev:int = 0;
        var yprev:int = 0;

        public function everythingContainer() {
            // constructor code
            trace ('Container started');
            if(stage) init();
            else
            this.addEventListener(Event.ADDED_TO_STAGE, init);
        }


        function init(eventInfo:Event = null):void
        {
            if(eventInfo != null)
            {
                this.removeEventListener(Event.ADDED_TO_STAGE, init);
                trace ('Container init removed');
            }
            // constructor code


            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
            this.addEventListener( Event.ENTER_FRAME, containerEveryFrame);
        }

        public function containerEveryFrame (Event): void {
            if (stage.contains(Main.player)) {

                xprev = this.x;
                yprev = this.y;
                checkPlayerMovement();
            }
        }
        // check the set keypress variables
        public function checkPlayerMovement () : void {
            if (wpressed) {
                this.y -= this.speed;

            }
            if (spressed) {
                this.y += this.speed;
            }
            if (apressed){
                this.x -= this.speed;
            }
            if (dpressed) {
                this.x += this.speed;
            }

        }

        //assign key presses to variables
        public function onKeyPress (event:KeyboardEvent):void {
            //up
            if (event.keyCode == 87){
                wpressed = true;

            }
            //down
            if (event.keyCode == 83) {
                spressed = true;

            }
            //left
            if (event.keyCode == 65){
                apressed = true;

            }
            //right
            if (event.keyCode == 68) {
                dpressed = true;

            }
        }

        //reset key press variables
        public function onKeyRelease (event:KeyboardEvent) : void {

            //up
            if (event.keyCode == 87){
                wpressed = false;
            }

            //down
            if (event.keyCode == 83) {
                spressed = false;
            }

            //left
            if (event.keyCode == 65){
                apressed = false;
            }

            //right
            if (event.keyCode == 68) {
                dpressed = false;
            }
        }
    }
}

Main (there's some other stuff going on in here, but at the minute I'm just trying to get the trees working with my other class.)

    package code
{
    import flash.display.*;
    import flash.events.*;
    import flash.ui.*;
    import flash.geom.Rectangle;
    import flashx.textLayout.container.ContainerController;

    public class Main extends MovieClip
    {
        //public static var main
        public static var player:PC;
        //public static var firstenemy: WolfEnemy;
        public static var MainContainer:everythingContainer;

        public function Main()
        {
            // constructor code
            trace('main started');
            if (stage)
            {
                init();
            }
            else
            {
                this.addEventListener(Event.ADDED_TO_STAGE, init);
            }
        }

        function init(eventInfo:Event = null):void
        {
            if (eventInfo != null)
            {
                this.removeEventListener(Event.ADDED_TO_STAGE, init);
                trace('Main init removed');
            }

            MainContainer = new everythingContainer  ;
            MainContainer.x = stage.stageWidth / 2;
            MainContainer.y = stage.stageHeight / 2;
            stage.addChild(MainContainer);

            player = new PC();
            player.x = stage.stageWidth / 2;
            player.y = stage.stageHeight / 2;
            stage.addChild(player);

            //firstenemy = new WolfEnemy();
            //firstenemy.x = 100;
            //firstenemy.y = 100;
            //stage.addChild(firstenemy);

            stage.addEventListener( Event.ENTER_FRAME, everyFrameMain);
        }

        // check if an enemy hits the player.
        /*public function enemycollison(): void {
        if(firstenemy.hitTestObject(player)){
        trace ('hit enemy');
        player.health--;
        firstenemy.kill();
        }
        }*/

        // manage events that need to haapen globally for every frame
        public function everyFrameMain(Event):void
        {
            /*if (stage.contains(firstenemy)){
            enemycollison();
            } */

            //root.scrollRect = new Rectangle(player.x - 400, player.y - 300, 800, 600);

        }

        // finish and close game
        public function endgame():void
        {

        }

    }
}

and finally my tree class

    package code {

    import flash.display.*;
    import flash.events.*;
    import flash.ui.*;
    import code.Main;
    import code.everythingContainer;

    public class Tree1 extends everythingContainer {


        public function Tree1() 
        {
            // constructor code
            trace ('Tree1 started');
            if(stage) init();
            else
            this.addEventListener(Event.ADDED_TO_STAGE, init);
        }

        function init(eventInfo:Event = null):void
        {
            if(eventInfo != null)
            {
                this.removeEventListener(Event.ADDED_TO_STAGE, init);
                trace ('Tree1 init removed');
            }
            this.addEventListener( Event.ENTER_FRAME, tree1EveryFrame);
        }

        public function tree1EveryFrame (Event): void {
            playercollision();
        }

        public function playercollision(): void {
            if(this.hitTestObject(Main.player)){
                trace ('hit wall');
                Main.player.x = Main.player.xprev;
                Main.player.y = Main.player.yprev;

            }
        }
    }

}

Upvotes: 0

Views: 451

Answers (1)

Daniel MesSer
Daniel MesSer

Reputation: 1181

Probably due to linkage errors inside Fla-file.

  • RMB on library-item: Tree1
  • Export for ActionScript + Export in frame 1 + "class: code.Tree1"
  • File/Publish Settings/Actionscript Settings (the small wrench, right of Script-dropdown)
  • Source Path (add the linkage to where the compiler can find your package-folder), usually for me I crate a folder next to the fla file called src or something like that so the code-file would be found at "/MyProject/src/code/Tree1.as", in that case I add "./src/" inside Source path inside Advanced ActionScript 3.0 settings

Added an example project in Flash CS6 found at url: https://user.iter.org/filesharing/?uid=927205f7-cdfe-4915-a175-bc87f64af444 that is available for ~40 days.

Project structure in that file:

  • "/MyProject/DeepInheritage.fla"
  • "/MyProject/src/code/Foobar.as"
  • "/MyProject/src/code/Tree1.as"
  • Foobar.as which extends MovieClip
  • Tree1 library item which extends Foobar

That should be the exact same thing that you described in your issue, meaning that there is nothing wrong with that approach, it is just a matter of finding what is wrong. Most likely that is due to errors inside FLA-file, but might be something else.


code files:

package code {
    import flash.display.MovieClip;

    public class Foobar extends MovieClip {

        public function Foobar() {
            trace("foobar ctor()");
        }
    }
}

package code {
    import flash.display.MovieClip;

    public class Tree1 extends Foobar {

        public function Tree1() {
            trace("Tree1 ctor()");
        }
    }
}

Upvotes: 1

Related Questions