Sign in to follow this  
assafsahar123

extending Sprite error (typescript)

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!

Share this post


Link to post
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.

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.