Jump to content

asteroids-movement.js:2 Uncaught TypeError: Phaser.Game is not a function  asteroids-movement.js:2


vmars316
 Share

Recommended Posts

Hello & Thanks ,

I am getting error : asteroids-movement.js:2 Uncaught TypeError: Phaser.Game is not a function .

Line 2 of asteroids-movement.js is :

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'asteroids-movement', { preload: preload, create: create, update: update, render: render }); 

followed by :

function preload() { 

Html says :         <script src="C:/Phaser/phaser-2.4.4/DownloadExamples/phaser-examples-master/examples/lib/Phaser.js" type="text/javascript"></script> 

Thanks...vm

Link to comment
Share on other sites

Thanks :
AddressBar=

file:///C:/Phaser/phaser-2.4.4/DownloadExamples/phaser-examples-master/examples/arcade-physics/asteroid-movement/asteroids-movement.html
.html=

Quote

 

<!doctype html>
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>asteroids-movement.html</title>
        <script src="C:/Phaser/phaser-2.4.4/DownloadExamples/phaser-examples-master/examples/lib/pixi.js" type="text/javascript"></script>
        <script src="C:/Phaser/phaser-2.4.4/DownloadExamples/phaser-examples-master/examples/lib/Phaser.js" type="text/javascript"></script>
        </head>
    <body>
<!--          <script src=".../phaser.js" type="text/javascript"></script>
-->
         <script src="asteroids-movement.js" type="text/javascript"></script>
</body>
</html>

 

 

 

 

.js=

Quote

 

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'asteroids-movement', { preload: preload, create: create, update: update, render: render });

function preload() {

    game.load.image('space', 'C:/Phaser/phaser-2.4.4/DownloadExamples/phaser-examples-master/examples/assets/skies/deep-space.jpg');
    game.load.image('bullet', 'C:/Phaser/phaser-2.4.4/DownloadExamples/phaser-examples-master/examples/assets/games/asteroids/bullets.png');
    game.load.image('ship', 'C:/Phaser/phaser-2.4.4/DownloadExamples/phaser-examples-master/examples/assets/games/asteroids/ship.png');

}

var sprite;
var cursors;

var bullet;
var bullets;
var bulletTime = 0;

function create() {

    //  This will run in Canvas mode, so let's gain a little speed and display
    game.renderer.clearBeforeRender = false;
    game.renderer.roundPixels = true;

    //  We need arcade physics
    game.physics.startSystem(Phaser.Physics.ARCADE);

    //  A spacey background
    game.add.tileSprite(0, 0, game.width, game.height, 'space');

    //  Our ships bullets
    bullets = game.add.group();
    bullets.enableBody = true;
    bullets.physicsBodyType = Phaser.Physics.ARCADE;

    //  All 40 of them
    bullets.createMultiple(40, 'bullet');
    bullets.setAll('anchor.x', 0.5);
    bullets.setAll('anchor.y', 0.5);

    //  Our player ship
    sprite = game.add.sprite(300, 300, 'ship');
    sprite.anchor.set(0.5);

    //  and its physics settings
    game.physics.enable(sprite, Phaser.Physics.ARCADE);

    sprite.body.drag.set(100);
    sprite.body.maxVelocity.set(200);

    //  Game input
    cursors = game.input.keyboard.createCursorKeys();
    game.input.keyboard.addKeyCapture([ Phaser.Keyboard.SPACEBAR ]);

}

function update() {

    if (cursors.up.isDown)
    {
        game.physics.arcade.accelerationFromRotation(sprite.rotation, 200, sprite.body.acceleration);
    }
    else
    {
        sprite.body.acceleration.set(0);
    }

    if (cursors.left.isDown)
    {
        sprite.body.angularVelocity = -300;
    }
    else if (cursors.right.isDown)
    {
        sprite.body.angularVelocity = 300;
    }
    else
    {
        sprite.body.angularVelocity = 0;
    }

    if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR))
    {
        fireBullet();
    }

    screenWrap(sprite);

    bullets.forEachExists(screenWrap, this);

}

function fireBullet () {

    if (game.time.now > bulletTime)
    {
        bullet = bullets.getFirstExists(false);

        if (bullet)
        {
            bullet.reset(sprite.body.x + 16, sprite.body.y + 16);
            bullet.lifespan = 2000;
            bullet.rotation = sprite.rotation;
            game.physics.arcade.velocityFromRotation(sprite.rotation, 400, bullet.body.velocity);
            bulletTime = game.time.now + 50;
        }
    }

}

function screenWrap (sprite) {

    if (sprite.x < 0)
    {
        sprite.x = game.width;
    }
    else if (sprite.x > game.width)
    {
        sprite.x = 0;
    }

    if (sprite.y < 0)
    {
        sprite.y = game.height;
    }
    else if (sprite.y > game.height)
    {
        sprite.y = 0;
    }

}

function render() {
}

 

 

 

 

Other programs run fine with CANVAS =
file:///C:/Phaser/phaser-2.4.4/Tutorial-01/part9.html

Quote

 

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8" />
    <title>Phaser - Making your first game, part 9</title>
    <script type="text/javascript" src="js/phaser.min.js"></script>
    <style type="text/css">
        body {
            margin: 0;
        }
    </style>
</head>
<body>

<script type="text/javascript">

var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update });

function preload() {

    game.load.image('sky', 'assets/sky.png');
    game.load.image('ground', 'assets/platform.png');
    game.load.image('star', 'assets/star.png');
    game.load.spritesheet('dude', 'assets/dude.png', 32, 48);

}

var player;
var platforms;
var cursors;

var stars;
var score = 0;
var scoreText;

function create() {

    //  We're going to be using physics, so enable the Arcade Physics system
    game.physics.startSystem(Phaser.Physics.ARCADE);

    //  A simple background for our game
    game.add.sprite(0, 0, 'sky');

    //  The platforms group contains the ground and the 2 ledges we can jump on
    platforms = game.add.group();

    //  We will enable physics for any object that is created in this group
    platforms.enableBody = true;

    // Here we create the ground.
    var ground = platforms.create(0, game.world.height - 64, 'ground');

    //  Scale it to fit the width of the game (the original sprite is 400x32 in size)
    ground.scale.setTo(2, 2);

    //  This stops it from falling away when you jump on it
    ground.body.immovable = true;

    //  Now let's create two ledges
    var ledge = platforms.create(400, 400, 'ground');
    ledge.body.immovable = true;

    ledge = platforms.create(-150, 250, 'ground');
    ledge.body.immovable = true;

    // The player and its settings
    player = game.add.sprite(32, game.world.height - 150, 'dude');

    //  We need to enable physics on the player
    game.physics.arcade.enable(player);

    //  Player physics properties. Give the little guy a slight bounce.
    player.body.bounce.y = 0.2;
    player.body.gravity.y = 300;
    player.body.collideWorldBounds = true;

    //  Our two animations, walking left and right.
    player.animations.add('left', [0, 1, 2, 3], 10, true);
    player.animations.add('right', [5, 6, 7, 8], 10, true);

    //  Finally some stars to collect
    stars = game.add.group();

    //  We will enable physics for any star that is created in this group
    stars.enableBody = true;

    //  Here we'll create 12 of them evenly spaced apart
    for (var i = 0; i < 12; i++)
    {
        //  Create a star inside of the 'stars' group
        var star = stars.create(i * 70, 0, 'star');

        //  Let gravity do its thing
        star.body.gravity.y = 300;

        //  This just gives each star a slightly random bounce value
        star.body.bounce.y = 0.7 + Math.random() * 0.2;
    }

    //  The score
    scoreText = game.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#000' });

    //  Our controls.
    cursors = game.input.keyboard.createCursorKeys();
    
}

function update() {

    //  Collide the player and the stars with the platforms
    game.physics.arcade.collide(player, platforms);
    game.physics.arcade.collide(stars, platforms);

    //  Checks to see if the player overlaps with any of the stars, if he does call the collectStar function
    game.physics.arcade.overlap(player, stars, collectStar, null, this);

    //  Reset the players velocity (movement)
    player.body.velocity.x = 0;

    if (cursors.left.isDown)
    {
        //  Move to the left
        player.body.velocity.x = -150;

        player.animations.play('left');
    }
    else if (cursors.right.isDown)
    {
        //  Move to the right
        player.body.velocity.x = 150;

        player.animations.play('right');
    }
    else
    {
        //  Stand still
        player.animations.stop();

        player.frame = 4;
    }
    
    //  Allow the player to jump if they are touching the ground.
    if (cursors.up.isDown && player.body.touching.down)
    {
        player.body.velocity.y = -350;
    }

}

function collectStar (player, star) {
    
    // Removes the star from the screen
    star.kill();

    //  Add and update the score
    score += 10;
    scoreText.text = 'Score: ' + score;

}

</script>

</body>
</html>

Thanks..

 

 

Link to comment
Share on other sites

10 hours ago, drhayes said:

It looks like you're trying to serve your game using the file system instead of through a local web server. That won't work.

Check out the getting started guide to help you... uh, get started: http://phaser.io/tutorials/getting-started/part2

Thanks ,

If that's true , then why does :  file:///C:/Phaser/phaser-2.4.4/Tutorial-01/part9.html

above work ?

ALSO , when I run it thru Brackets , I get the same error  . ?  

Link to comment
Share on other sites

It might serve that single page, but any resources that page requires probably won't load correctly (as in... it can't find phaser.js so no Phaser object, like the error says).

Also, you don't need to also load pixi as well as Phaser. Phaser, at that version, has pixi in it already.

Use a web server. Your paths won't look like file paths, they'll look like web paths: <script src="/lib/phaser.js"></script>

Link to comment
Share on other sites

Aha! : 

Turns out there are several versions of phaser.js here : C:\Phaser\phaser-2.4.4

 6k, 2,953kb, 2,317kb, 2,859kb, 2,181kb, 1,077kb . 

When I run with the 2,953kb version , everything runs fine :

stand alone , brackets , mongoose .

Whew! , That's a relief .

Thanks

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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