Cxsquared
Cxsquared

Reputation: 95

Accessing a function from a MovieClip in an Array - AS3

I'm making a 2d shooter and trying to have different attacks have different effects on certain enemy types. I have my bullets and Enemies put into two arrays and then use a for loop which does a hitTestObject on the bullets and enemies. This all works, but then I have a public static function inside of the Bullet class which returns it's type as a String and a public static function inside of the Enemy class that takes a String then uses a switch to deiced what damage the enemy receives. When I run my program I keep getting Error #1069: Property getType not found on obj.QuickBullet and there is no default value.

The code for the Enemy.

package char {

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

public class Enemy1 extends MovieClip {

    private var _type:String;
    private static var _health:Number;
    private var _vx:Number;
    private var _vy:Number;
    private var _stage:Stage;

    private static var _instance:Enemy1;

    public function Enemy1() {
        init();
    }

    private function init():void {
        //Vars
        _vx = -10;
        _vy = Math.random()*10;
        _health = 1;
        _stage = Main.getStage();
        _instance = this;

        //Listeners
        addEventListener(Event.ADDED_TO_STAGE, onAdded);
        addEventListener(Event.ENTER_FRAME, enemyLoop);
        addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);

        //Add Enemy
        Main.getInstance().addChild(this);
    }

    //When Added
    private function onAdded(event:Event):void{
        //Set position
        this.x = _stage.stageWidth;
        this.y = Math.random() * _stage.stageHeight;
        //trace("Enemy created");

        dispatchEvent(new Event("enemyCreated", true));
    }

    //Loop
    private function enemyLoop(event:Event):void {
        //Move
        x += _vx;
        y += _vy;

        //Boundaries
        if ( this.y <= 0 + this.height/2){
            this.y = this.height/2;
            _vy *= -1;
        }
        if ( this.y >= _stage.stageHeight - this.width/2){
            this.y = _stage.stageHeight - this.width/2;
            _vy *= -1;
        }

        //Health cheack
        if ( _health <= 0){
            if (this.parent) {
                this.parent.removeChild(this);
            }
            trace(this + " is dead.");
        }
        //Leaves screen
        if (this.x <= -this.width){
            Main.getInstance().removeChild(this);
        }
    }

    public static function isHit(type:String):void{
        switch(type){
            case "power" :
                _health -= 1;
                break;
            case "quick" :
                _health -= 1;
                break;
            case "strong" :
                _health -= 1;
                break;
            default:
                    _health -= 1;
            }
        }

        public static function getEnemy1():Enemy1{
            return _instance;
        }

        //When Removed
        private function onRemoved(event:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, onAdded);
            removeEventListener(Event.ENTER_FRAME, enemyLoop);
            removeEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
            //trace("enemy removed");
        }
    }

}

Code for the Bullet.

package obj {

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


public class PowerBullet extends MovieClip {
    //const
    private static const TYPE:String = "power";

    //Varibles
    private var _vx:Number;
    private var _vy:Number;
    private var _startX:Number;
    private var _startY:Number;
    private var _stage:Stage;

    public function PowerBullet(startX:Number, startY:Number) {
        _startX = startX;
        _startY = startY;

        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

        Main.getInstance().addChild(this);
    }

    //When created
    private function onAddedToStage(event:Event):void{
        //Initialize vars
        this.x = _startX;
        this.y = _startY;
        _vx = 20;
        _vy = 0;
        _stage = Main.getStage();

        dispatchEvent(new Event("bulletCreated", true));

        //Event Handlers
        addEventListener(Event.ENTER_FRAME, bulletLoop);
        addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
    }

    //Bullet loop
    private function bulletLoop(event:Event):void {
        //Move bullet
        x += _vx;
        y += _vy;

        if (this.x - this.width > _stage.stageWidth)
        {
            parent.removeChild(this);
        }
    }

    public static function getType():String{
        return TYPE;
    }

    //When Removed
    private function onRemoved(event:Event):void {
        removeEventListener(Event.ENTER_FRAME, bulletLoop);
        removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        removeEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
    }
}

}

The for loop I'm using.

for (var i:int = 0; i < _enemies.length; i++){
for(var j:int = 0; j < _bullets.length; j++){
    if(_bullets[j].hitTestObject(_enemies[i])){
        if (_bullets[j].parent) {
                            _bullets[j].parent.removeChild(_bullets[j]);
                    }

                    _enemies[i].isHit(_bullets[j].getType());

                    _enemies.splice(i, 1);
                    _bullets.splice(j, 1);

                    i--;
                    j--;

                    Main.setScore(10);
                }
            }

        }

Upvotes: 1

Views: 201

Answers (2)

Marty
Marty

Reputation: 39466

Note: Your error is saying there is no property getType() on QuickBullet but you've only supplied code for PowerBullet.

You have the static namespace applied to getType(). This means you can only call this method via the class itself rather than an instance of that class:

PowerBullet.getType();

If you remove static, you'll be able to access that method on each instance like expected.

Additional note: It looks like you're using static for a few class members where it doesn't seem logical like health and isHit(). I suggest you read up on the static namespace to better understand its purpose.

Upvotes: 2

strah
strah

Reputation: 6742

Remove static from this code

public static function getType():String{
    return TYPE;
}

Upvotes: 2

Related Questions