# Type error on every mouse event

**URL:** <https://phaser.discourse.group/t/type-error-on-every-mouse-event/3563>\
**Category:** Phaser 3\
**Created:** [August 24, 2019, 8:23pm UTC](https://phaser.discourse.group/t/type-error-on-every-mouse-event/3563 "2019-08-24T20:23:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![IceExplosive](https://avatars.discourse-cdn.com/v4/letter/i/dec6dc/32.png) [@IceExplosive](https://phaser.discourse.group/u/IceExplosive)\
**Post date:** [August 24, 2019, 8:23pm UTC](https://phaser.discourse.group/t/type-error-on-every-mouse-event/3563/1 "2019-08-24T20:23:10Z")

</div>

Hello,  
I’ve started with a Phaser 3 today and run into a brick wall right at the start.  
Even after reducing the actual code to literaly nothing:

```
const game = new Phaser.Game({
    type: Phaser.AUTO,
    title: 'FortRise',
    scene: {
        create: create,
        preload: preload,
    },
});

function preload() {}

function create() {}

```

After including phaser.js and this .js file into a html file and loading it up in a browser, it throws an error on every mouse event (Move, click, release, drag…)

```
TypeError: this.sprite is undefined

```

Please, does anyone have an idea why is this happening? I can’t figure out.  
Have a good day and thanks for any leads.

---

<div class="post-metadata">

**Author:** ![Jackolantern](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jackolantern/32/450_2.png) [@Jackolantern](https://phaser.discourse.group/u/Jackolantern)\
**Post date:** [August 24, 2019, 8:55pm UTC](https://phaser.discourse.group/t/type-error-on-every-mouse-event/3563/2 "2019-08-24T20:55:32Z")

</div>

We would need to see more code to understand why this is happening. I am not sure where or how the sprite variable is being set.

---

<div class="post-metadata">

**Author:** ![IceExplosive](https://avatars.discourse-cdn.com/v4/letter/i/dec6dc/32.png) [@IceExplosive](https://phaser.discourse.group/u/IceExplosive)\
**Post date:** [August 25, 2019, 5:53am UTC](https://phaser.discourse.group/t/type-error-on-every-mouse-event/3563/3 "2019-08-25T05:53:45Z")

</div>

Well… there isn’t any more code. Thats all. I can add .html (when I get home) but thats just loading Phaser.js and .js from above.  
I’ll add error trace from that event as well.

---

<div class="post-metadata">

**Author:** ![IceExplosive](https://avatars.discourse-cdn.com/v4/letter/i/dec6dc/32.png) [@IceExplosive](https://phaser.discourse.group/u/IceExplosive)\
**Post date:** [August 25, 2019, 8:16am UTC](https://phaser.discourse.group/t/type-error-on-every-mouse-event/3563/4 "2019-08-25T08:16:21Z")

</div>

Ok, so there’s the whole code:

main.js:

> ```
> const game = new Phaser.Game({
> type: Phaser.AUTO,
> title: 'FortRise',
> scene: {
> create: create,
> preload: preload,
> },
> });
> 
> function preload() {}
> function create() {}
> 
> ```

Then index.html:

> ```
> <!DOCTYPE html>
> <html lang="en">
> <head>
> <meta charset="UTF-8">
> </head>
> <body>
> <script src="./lib/phaser.js"></script>
> <script src="./main.js"></script>
> </body>
> </html>
> 
> ```

And those typeErrors I’m getting:

> ```
> TypeError: this.sprite is undefined
> [phaser.js:69523:13](http://localhost:8000/lib/phaser.js)
> get http://localhost:8000/lib/phaser.js:69523
> getCamerasBelowPointer http://localhost:8000/lib/phaser.js:127168
> hitTestPointer http://localhost:8000/lib/phaser.js:157705
> update http://localhost:8000/lib/phaser.js:157489
> updateInputPlugins http://localhost:8000/lib/phaser.js:66828
> onMouseMove http://localhost:8000/lib/phaser.js:67028
> onMouseMove http://localhost:8000/lib/phaser.js:68127
> 
> TypeError: this.sprite is undefined
> [phaser.js:69523:13](http://localhost:8000/lib/phaser.js)
> get http://localhost:8000/lib/phaser.js:69523
> down http://localhost:8000/lib/phaser.js:68967
> onMouseDown http://localhost:8000/lib/phaser.js:67006
> onMouseDown http://localhost:8000/lib/phaser.js:68145
> 
> ```

Phaser version loaded up:  
Phaser v3.19.0 (WebGL | Web Audio)

---

<div class="post-metadata">

**Author:** ![Jackolantern](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jackolantern/32/450_2.png) [@Jackolantern](https://phaser.discourse.group/u/Jackolantern)\
**Post date:** [August 25, 2019, 1:09pm UTC](https://phaser.discourse.group/t/type-error-on-every-mouse-event/3563/5 "2019-08-25T13:09:01Z")

</div>

Huh…that is really strange. Because that should just be a basic wire-up of Phaser, and you should get a black game view. Is another file possibly getting picked-up somehow? That stack trace is showing an error in onMouseMove, which you aren’t calling.

Move to another empty folder, redownload Phaser and do another setup. While I don’t think it is the problem, remove the leading dot on your src attributes in the script tags. Those are relative URLs, not local references. Follow the setup in [this tutorial](https://phaser.io/tutorials/making-your-first-phaser-3-game/part1) because I know that one works. I don’t see any problems in your code, but it may be better to follow along a known working example.

EDIT: Actually, looking at the title, it sounded like you were saying you were having an error on a mouse event. You sure don’t have any more code anywhere else? Or did you just mention the mouse event because it was in the stack trace?

---

<div class="post-metadata">

**Author:** ![IceExplosive](https://avatars.discourse-cdn.com/v4/letter/i/dec6dc/32.png) [@IceExplosive](https://phaser.discourse.group/u/IceExplosive)\
**Post date:** [August 25, 2019, 3:50pm UTC](https://phaser.discourse.group/t/type-error-on-every-mouse-event/3563/6 "2019-08-25T15:50:43Z")

</div>

Yes. I’ve mentioned the mouse event only because of the error trace, haven’t implemented any event myself, yet.  
It was just a black empty view and error showed up after I’ve moved or clicked with a mouse inside that view.  
But downloading a new phaser.js solved the issue. It still writes version 3.19.0 into console, but there are some \*changes.

Like method get (The x position of this Pointer) phaser.js:69523  
It used to be:

```
x: {

    get: function ()
    {
        return this.sprite.x;
    },

    set: function (value)
    {
        this.sprite.x = value;
    }

},

```

And have been changed to:

```
x: {

    get: function ()
    {
        return this.position.x;
    },

    set: function (value)
    {
        this.position.x = value;
    }

},

```

So I just \*messed up… 🙂 Now it’s working perfectly fine.  
Thanks a lot for quick response and bye.

---

<div class="post-metadata">

**Author:** ![Jackolantern](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jackolantern/32/450_2.png) [@Jackolantern](https://phaser.discourse.group/u/Jackolantern)\
**Post date:** [August 25, 2019, 4:11pm UTC](https://phaser.discourse.group/t/type-error-on-every-mouse-event/3563/7 "2019-08-25T16:11:00Z")

</div>

Interesting. That seems pretty crazy to me because a simple wire-up file like that probably would have meant every project using that build would have failed. Crazy!

Regardless, glad you are back up and running now! Take care!

---

<div class="post-metadata">

**Author:** ![IceExplosive](https://avatars.discourse-cdn.com/v4/letter/i/dec6dc/32.png) [@IceExplosive](https://phaser.discourse.group/u/IceExplosive)\
**Post date:** [August 25, 2019, 4:37pm UTC](https://phaser.discourse.group/t/type-error-on-every-mouse-event/3563/8 "2019-08-25T16:37:14Z")

</div>

Right… That made me wondering.  
I’ve loooked into history and it truly was just my dumb mistake. Somewhere while toying with Phaser’s objects, I must have hit rename (refactor) and WebStorm trying to look for all usage messed up even that phaser.js outside of src folder. (I’ve had a method using this.sprite at the begining and forgot about it)  
Damn… being used to different languages I rely on refactor without a second though a lot and forget how dangerous it is in javascript. 😊  
My apologies.

---

<div class="post-metadata">

**Author:** ![Jackolantern](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jackolantern/32/450_2.png) [@Jackolantern](https://phaser.discourse.group/u/Jackolantern)\
**Post date:** [August 25, 2019, 5:02pm UTC](https://phaser.discourse.group/t/type-error-on-every-mouse-event/3563/9 "2019-08-25T17:02:03Z")

</div>

Ahh, that makes a lot of sense!

By the way, if you are used to different languages (which I assume are probably strongly-typed like C#, C++ or Java if you use refactoring tools often), you may enjoy checking out Typescript. I use it for pretty much everything but the smallest JS demos. It adds a nice sheen of C#-like strict-typing to JS and it really makes refactoring much more powerful since you are explaining to the editor how everything is connected. I use it with Webstorm and it makes a lot of WS’s tools 10x more powerful. Plus, it gives you the slick import/export that makes it so much easier to organize your code. Phaser supports it very well with well-made type definition files.
