rjat

game.create.texture gives error: Cannot read property 'texture' of undefined

Recommended Posts

When i try to use game.create.texture from the Phaser examples, I get an error: Cannot read property 'texture' of undefined.

For example, if I copy the example code from the "Generate Sprite" example, or the "More Sprites" example and try to run them locally, I get the error.

The "game" variable is declared globally, for example (from the "More Sprites" example):

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create });

And then within the create function, game.create.texture is used, for example:

game.create.texture('chick', chick, pixelWidth, pixelHeight);

I assume the error means that the "create" property doesn't exist for the "game" object.

I can't spot what I'm doing wrong.  Any help would be useful, thanks.

Share this post


Link to post
Share on other sites

Everything looks good to me. It sounds like game might be out of scope wherever you are calling texture. Double check that you are in fact defining game as a global variable.

If you posted more code, that might reveal a different issue.

Share this post


Link to post
Share on other sites

Thanks for the response Zeterain.  I have created a very cut down version of the "More Sprites" example, to make posting the complete code easier.  In the cut down version, I have just included the "chick" sprite.  I have pasted the complete code I have used below.  When I edit the "More Sprites" example to do the same in the online code editor on the "More Sprites" page, the code runs successfully and I see just the chick.  When I run the code below locally (using the python SimpleHTTPServer), I see nothing apart from a black window.  When I look in the Developer Tools in Chrome, I see the error:  Cannot read property 'texture' of undefined.

It could easily be something very basic I am doing wrong, as this is the first time I have looked at Phaser.  The complete code I am trying to run is pasted below:

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8" />
    <title>Testing</title>
    <script type="text/javascript" src="js/phaser.min.js"></script>
    <style type="text/css">
        body {
            margin: 0;
            background-color: #0F0F0F;
        }
    </style>
</head>
<body>

<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create });

function create() {

    var pixelWidth = 6;
    var pixelHeight = 6;

    var chick = [
        '...55.......',
        '.....5......',
        '...7888887..',
        '..788888887.',
        '..888088808.',
        '..888886666.',
        '..8888644444',
        '..8888645555',
        '888888644444',
        '88788776555.',
        '78788788876.',
        '56655677776.',
        '456777777654',
        '.4........4.'
    ];

    game.create.texture('chick', chick, pixelWidth, pixelHeight);

    
    game.add.sprite(150, 200, 'chick').anchor.y = 1;

}

</script>

</body>
</html>

 

Share this post


Link to post
Share on other sites

Your code works just fine for me using the python SimpleHTTPServer.

The only thing I can think would be causing issues is if the game object was created incorrectly or incompletely. If the phaser.min.js file was outdated or altered, that could affect something. 

Try using the attached phaser.min.js file and let me know if the problem persists and if there are any other console errors or warnings.

phaser.min.js

Share this post


Link to post
Share on other sites

Thanks, you've solved the problem.  For the game i started trying to make, I had copied the folder structure from the "Making your first game" tutorial and then I started editing and changing things from there.  So I was using the phaser.min.js file from that tutorial.  I didn't consider the possibility of that phaser.min.js being old and not including the functionality of the game.create.texture method.  I have placed the latest downloadable phaser.min.js file in my js folder and now it works.  Lesson learnt, make sure you are using the latest phaser framework file !  Thanks for the help.

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...

  • Recently Browsing   0 members

    No registered users viewing this page.