Jump to content

Character on the grounf


grandpied
 Share

Recommended Posts

Hey Community !


I'm currently using with Friends Phaser for the first time for a school project, and I need your help on 1 point.
I'm using Dude as a character and blocks appear, but my problem is Dude can go out of the game Area and the game never restart :/

Any Idea ?

Thanks a lot and sorry for my poor english

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

<script type="text/javascript">

    var game = new Phaser.Game(500, 500, Phaser.AUTO, '', { preload: preload, create: create, update: update });

    var player;
    var platforms;
    var cursors;
    var stars;
    var score = 0;
    var scoreText;
    var sky;

    function preload() {

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

    function create() {
        game.physics.startSystem(Phaser.Physics.ARCADE);

        sky = game.add.sprite(0,0, 'sky');


        platforms = game.add.group();

        var timer = game.time.events.loop(Phaser.Timer.SECOND, addRowOfPipes, this);

        // The player and its settings
        player = game.add.sprite(250, game.world.height - 200, '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.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);


        cursors = game.input.keyboard.createCursorKeys();

    }

    function update() {
        game.physics.arcade.collide(player, platforms);
        game.physics.arcade.collide(stars, platforms);
        game.physics.arcade.overlap(player, null, this);
		

        player.body.velocity.x = 0;
        if (cursors.left.isDown)
        {
            //  Move to the left
            player.body.velocity.x = -250;
            player.animations.play('left');
        }
        else if (cursors.right.isDown)
        {
            //  Move to the right
            player.body.velocity.x = 250;
            player.animations.play('right');
        }
       /* else if(cursors.down.isDown)
        {
            game.state.start(game.state.current);
        } */
        else
        {
            //  Stand still
            player.animations.stop();
            player.frame = 4;
        }

        //  Allow the player to jump if they are touching the ground.
		console.log(player.body);
        if (cursors.up.isDown || player.body.touching.collideWorldBounds)
        {
            game.state.start(game.state.current);
        }
    }

    function addOnePipe(x,y) {
        // Create a pipe at the position x and y
        var pipe = game.add.sprite(x, y, 'ground');

        // Add the pipe to our previously created group
        this.platforms.add(pipe);

        // Enable physics on the pipe
        game.physics.arcade.enable(pipe);

        // Add velocity to the pipe to make it move down

        pipe.body.velocity.y = 150;

        // Automatically kill the pipe when it's no longer visible
        pipe.checkWorldBounds = true;
        pipe.outOfBoundsKill = true;
        pipe.body.immovable=true;
    }

    function addRowOfPipes() {
        // Randomly pick a number between 1 and 5
        // This will be the hole position
        var hole = Math.floor(Math.random() * 5) + 1;

        // Add the 6 pipes
        // With one big hole at position 'hole' and 'hole + 1'
        for (var i = 0; i < 8; i++)
            if (i !== hole && i !== hole + 1)
            {
                addOnePipe(i * 60 + 10, 0);

            }
    }





</script>

</body>
</html>

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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