Jump to content

New noob


Antoine553
 Share

Recommended Posts

Hello

Happy New Year everyone

I'm a french student which has a homework to make with phaser. I have then started to work and learn to use it and finally tried to make little game thanks the help of the many tutorials and examples on the web but i'm quite bad and i'm finally stuck at this point.

http://195.83.128.55/~mmi15c02/int/phaser.php

For now the problem is to create many little alien (it will be change later) at the right of the screen and make them go to the left in a repetitive way but i tried one thousand method and always failed as you can see my last attempt XD if someone know how to do pls help (sry for the english).

 

the actual code :

 

<script type="text/javascript" src="js/phaser.js"></script>
<script type="text/javascript">

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

function preload() {
	game.load.image('alien', 'assets/alien.png');
    game.load.image('background', 'assets/background.png');
    game.load.image('bullet', 'assets/bullet.png');
    game.load.spritesheet('vaisseau', 'assets/vaisseau.png', 32, 48);
}

var sprites;
var vaisseau;
var alien;
var background;
var cursors;
var bullets;
var bulletTime = 0;

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

	background = game.add.tileSprite(0, 0, 1000, 400, 'background');


    bullets = game.add.group();
    bullets.enableBody = true;
    bullets.physicsBodyType = Phaser.Physics.ARCADE;
    bullets.createMultiple(30, 'bullet');
    bullets.setAll('anchor.x', 0.5);
    bullets.setAll('anchor.y', 0.75);
    bullets.setAll('outOfBoundsKill', true);
    bullets.setAll('checkWorldBounds', true);



	vaisseau = game.add.sprite(100, 200, 'vaisseau');
	vaisseau.anchor.setTo(0.5, 0.5);
	game.physics.enable(vaisseau, Phaser.Physics.ARCADE);
    vaisseau.body.collideWorldBounds = true;

    cursors = game.input.keyboard.createCursorKeys();
    fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);


    sprites = game.add.group();
    game.time.events.loop(300, createSprite, this);
}

function createSprite() {
    var alien = sprites.create(1000, game.world.randomY, 'alien');
}

function fireBullet () {
    if (game.time.now > bulletTime){
        bullet = bullets.getFirstExists(false);
        if (bullet){
            bullet.reset(vaisseau.x, vaisseau.y + 8);
            bullet.body.velocity.x = 400;
            bulletTime = game.time.now + 400;
        }
    }

}

function update() {
	background.tilePosition.x -= 2;

	if (vaisseau.alive){
        vaisseau.body.velocity.setTo(0, 0);
        if (cursors.down.isDown){
            vaisseau.body.velocity.y = 200;
        }else if (cursors.up.isDown){
            vaisseau.body.velocity.y = -200;
        }else if (cursors.left.isDown){
            vaisseau.body.velocity.x = -200;
        }else if (cursors.right.isDown){
            vaisseau.body.velocity.x = 200;
        }if (fireButton.isDown){
            fireBullet();
        }
	}
    
    sprites.x -= 5;

}

</script>

 

Link to comment
Share on other sites

ok i randomly tried something and it work

 

<script type="text/javascript" src="js/phaser.js"></script>
<script type="text/javascript">

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

function preload() {
	game.load.image('alien', 'assets/alien.png');
    game.load.image('background', 'assets/background.png');
    game.load.image('bullet', 'assets/bullet.png');
    game.load.spritesheet('vaisseau', 'assets/vaisseau.png', 32, 48);
}

var sprites;
var vaisseau;
var alien;
var background;
var cursors;
var bullets;
var bulletTime = 0;

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

	background = game.add.tileSprite(0, 0, 1000, 400, 'background');


    bullets = game.add.group();
    bullets.enableBody = true;
    bullets.physicsBodyType = Phaser.Physics.ARCADE;
    bullets.createMultiple(30, 'bullet');
    bullets.setAll('anchor.x', 0.5);
    bullets.setAll('anchor.y', 0.75);
    bullets.setAll('outOfBoundsKill', true);
    bullets.setAll('checkWorldBounds', true);



	vaisseau = game.add.sprite(100, 200, 'vaisseau');
	vaisseau.anchor.setTo(0.5, 0.5);
	game.physics.enable(vaisseau, Phaser.Physics.ARCADE);
    vaisseau.body.collideWorldBounds = true;

    cursors = game.input.keyboard.createCursorKeys();
    fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);


    sprites = game.add.group();
    game.time.events.loop(300, createSprite, this);
}

function createSprite() {
    var alien = sprites.create(1000, game.world.randomY, 'alien');
    //this.sprites.add(alien);
    game.physics.arcade.enable(alien);
    alien.body.velocity.x = -200;
    alien.checkWorldBounds = true;
    alien.outOfBoundsKill = true;
}


function fireBullet () {
    if (game.time.now > bulletTime){
        bullet = bullets.getFirstExists(false);
        if (bullet){
            bullet.reset(vaisseau.x, vaisseau.y + 8);
            bullet.body.velocity.x = 400;
            bulletTime = game.time.now + 400;
        }
    }

}

function update() {
	background.tilePosition.x -= 2;

	if (vaisseau.alive){
        vaisseau.body.velocity.setTo(0, 0);
        if (cursors.down.isDown){
            vaisseau.body.velocity.y = 200;
        }else if (cursors.up.isDown){
            vaisseau.body.velocity.y = -200;
        }else if (cursors.left.isDown){
            vaisseau.body.velocity.x = -200;
        }else if (cursors.right.isDown){
            vaisseau.body.velocity.x = 200;
        }if (fireButton.isDown){
            fireBullet();
        }
	}

}

</script>

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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