Jump to content

Can't access sprite methods while using getChildAt()


maxpower
 Share

Recommended Posts

Hey Guys, I'm using typescript in visual studio for web and can't access the sprite methods when using though group call getChildAt()

 

e.g this.objectsGroup.getChildAt(i).destroy();  

 

This is the error I get

Error    1    Build: Property 'destroy' does not exist on type 'DisplayObject'.

 

Not sure if i'm meant to cast this?
 

Any help would be much appreciated

class below.

 

module Test {
    export class Level1 extends Phaser.State {

        background: Phaser.Sprite;
        crossHairGroup: Phaser.Group;
        poleGroup: Phaser.Group;
        objectsGroup: Phaser.Group;
        crossHair: CrossHair;
        pole: Phaser.Sprite;
        object: Phaser.Sprite;

        create() {
 
            this.crossHairGroup = this.game.add.group();
            this.poleGroup = this.game.add.group();
            this.objectsGroup = this.game.add.group();

            this.background = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'background');
            this.background.anchor.setTo(0.5, 0.5);

            this.crossHair = new CrossHair(this.game, this.game.input.x, this.game.input.y, this.crossHairGroup);

            this.initializeObjects();
            this.game.input.onDown.add(this.fire, this);
        }

        fire() {
            var bullet = this.crossHair.fire();

            /* Check if bullet hits object */
            for (var i = 0; i < this.objectsGroup.length; i++) {

                if (this.checkOverlap(this.objectsGroup.getChildAt(i), bullet)) {
                    this.objectsGroup.add(this.object);

 

                    /* this line not working */
                    //this.objectsGroup.getChildAt(i).destroy();               
                    
                }
            }
        }

        checkOverlap(spriteA, spriteB) {

            var boundsA = spriteA.getBounds();
            var boundsB = spriteB.getBounds();

            return Phaser.Rectangle.intersects(boundsA, boundsB);
        }

        initializeObjects() {
            
            /* Initialize  poles and objects */
            for (var i = 0; i < 7; i++) {
                
                /* Top poles */
                this.pole = new Pole(this.game,(i * -200), 235, false);
                this.poleGroup.add(this.pole);

                /* Bottom poles */
                this.pole = new Pole(this.game,((i * 200) + 1024), 45, true);
                this.poleGroup.add(this.pole);

                /* Top objects */
                this.object = new Object(this.game,(i * -200), 300, false);
                this.objectsGroup.add(this.object);
                
                /* Bottom objects */
                this.object = new Object(this.game,((i * 200) + 1024), 100, true);
                this.objectsGroup.add(this.object);

            }

        }
    }
}

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...