Jump to content

extending Sprite error (typescript)


assafsahar123
 Share

Recommended Posts

Hi, I'm probably doing something wrong here, but I can't find it for hours now... please help!

I'm working with Typescript on a football game.

I have a Game state and a ball sprite.

I'm trying to create a class for the ball, which extends Phaser.Sprite.

I'm getting no errors in the editor, but I get console error.

This is the code for the Game state:

// reference to typescript files
/// <reference path="../lib/phaser.d.ts" />
    /// <reference path="../Objects/Ball.ts" />
module Football{
    export class Game extends Phaser.State{

        ball: Football.Ball;

        preload(){
            this.game.load.spritesheet("ball","Graphics/3d-ball.png", 229, 229, 4, 9, 6);
        }
        create(){
            this.ball = new Ball(this.game,100, 100);
        }
        update(){ 
        }
    }
}

And this is the Ball class:

    /// <reference path="../lib/phaser.d.ts" />
module Football{
    export class Ball extends Phaser.Sprite{
        constructor (game: Phaser.Game,x:number,y:number){
            super(game,x,y,"ballShadow");
        }

        update(){

        }
    }
}

When running, I get the following error in the console: "Game.ts:17 Uncaught TypeError: Football.Ball is not a constructor".

Can someone tell me what's wrong with my code?

Thanks!

Link to comment
Share on other sites

Hi, I do not see anything wrong with your code. I can run it without problems.

Just make sure, that if your classes are in separate script files, you added both scripts to your html page. You can also try to put temporarily both classes into the same source file. Some time ago I wrote tutorial on siple Phaser game (http://sbcgamesdev.blogspot.cz/2015/05/phaser-tutorial-dronshooter-simple-game.html) and structure I use is very similar to your code, while all classes are in single file. If above fails you can take it as basic working skeleton.

In past I had similar problems, but I usually do not need to use "reference path" - I manually sort files in .csproj file (only when new file is added) ... if you are using Visual Studio.

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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