kittykatattack / hexi

Make games the fun way!
MIT License
551 stars 83 forks source link

Removing chimes sound from 01_treasureHunter demo raises exception #61

Closed filipux closed 6 years ago

filipux commented 6 years ago

I just downloaded hexi and played around with the demos. The first thing I tried was to remove the sound effect from the 01_treasureHunter demo but that somehow created an exception. Can't for my life figure out why.

I made the following changes:

var g = hexi(512, 512, setup, ["sounds/chimes.wav"]);
        ->
var g = hexi(512, 512, setup);
chimes = g.sound("sounds/chimes.wav");
        ->
//chimes = g.sound("sounds/chimes.wav");
chimes.play();
        ->
//chimes.play();

Then I got an error in the console:

core.js:154 Uncaught TypeError: Cannot read property 'x' of undefined
    at Hexi.move (core.js:154)
    at Hexi.play [as state] (game.js:210)
    at Hexi.update (core.js:154)
    at Smoothie.gameLoop (core.js:154)

Latest hexi-master osx Chrome Version 63.0.3239.132 (Official Build) (64-bit)

filipux commented 6 years ago

I tried reducing the treasureHunter demo to it's bare minimum and I can't get anything to work now. It turns out my player object is undefined all but the first time the game loop runs in this example:

<html>
    <head></head>
    <body>
        <script src="hexi-master/bin/hexi.js"></script>
        <script>
            var g = hexi(512, 512, setup);
            g.start();

            var player = undefined,
            gameScene = undefined;

            function setup() {
                gameScene = g.group();
                player = g.rectangle(32, 32, "red");
                gameScene.addChild(player);

                g.state = play;
            }

            function play() {
                console.log(player);
            }
        </script>
    </body>
</html>

What am I missing here?

kittykatattack commented 6 years ago

Does declaring player and gameScene before g make a difference?

filipux commented 6 years ago

Yes it does!

Declaring player and gameScene before g solved it; now player is a Sprite object instead of undefined. Time to make a game :)

kittykatattack commented 6 years ago

Great, glad it works! This is a bug in the initialization process that I don't quite understand yet, but we've found that just declaring the global game variables earlier in the code seems to be an OK workaround for now.