Jump to content

Phaser + es6


divers
 Share

Recommended Posts

Hello I am trying to write a game in phaser + es6 javascript.

So far I have a game state in which I am making player.

But in game state I have buttons to control my character. So is this correct?

// gameState.js
create() {
 
        this.game.physics.startSystem(Phaser.Physics.P2JS); //activate physics
        this.game.physics.p2.gravity.y = 1200; //realistic gravity
        this.game.world.setBounds(0, 0, this.game.world.width, this.game.world.height);//(x, y, width, height)
        this.game.physics.p2.setBoundsToWorld(true, true, false, true, false); //(left, right, top, bottom, setCollisionGroup)
        this.game.physics.p2.friction = 5; // default friction between ground and player or fireballs
 
        // setup our player
        let player = new Player(this.game, this.game.world.width/2, this.game.world.height - 150, 'player');
 
        // create our virtual game controller buttons
        let buttonjump = this.game.add.button(400, 300, 'buttonjump', null, this, 0, 1, 0, 1); //game, x, y, key, callback, callbackContext, overFrame, outFrame, downFrame, upFrame
        buttonjump.fixedToCamera = true; //our buttons should stay on the same place
        buttonjump.events.onInputDown.add(function(){ player.skill_1=true; });
        buttonjump.events.onInputUp.add(function(){ player.skill_1=false; });
 
        let buttonfire = this.game.add.button(500, 300, 'buttonfire', null, this, 0, 1, 0, 1);
        buttonfire.fixedToCamera = true;
        buttonfire.events.onInputDown.add(function(){ player.fire=true; });
        buttonfire.events.onInputUp.add(function(){ player.fire=false; });
 
        let buttonleft = this.game.add.button(0, 300, 'buttonhorizontal', null, this, 0, 1, 0, 1);
        buttonleft.fixedToCamera = true;
        buttonleft.events.onInputDown.add(function(){ player.moveLeft=true; });
        buttonleft.events.onInputUp.add(function(){ player.moveLeft=false; });
 
        let buttonright = this.game.add.button(160, 300, 'buttonhorizontal', null, this, 0, 1, 0, 1);
        buttonright.fixedToCamera = true;
        buttonright.events.onInputDown.add(function(){ player.moveRight=true; });
        buttonright.events.onInputUp.add(function(){ player.moveRight=false; });
 
}
 
 
//Player constructor
    constructor(game, x, y, key) {
 
super(game, x, y, key);
 
        this.game.physics.p2.enable(this);
        this.body.setCircle(22); // collision circle
        this.body.fixedRotation=true; // do not rotate on collision
        this.body.mass = 4;
 
        // add some animations
        this.animations.add('idle', [0,1,2,3,4,3,2,1,0] , 10, true); // (key, framesarray, fps,repeat)
        this.animations.add('walk', [11,12,13,14,15,16,17,18], 15, true); // (key, framesarray, fps,repeat)
        this.anim = this.animations.add('skill_1', [5,6,7,8,9,10], 15, false); // (key, framesarray, fps,repeat)
        this.anim.onComplete.add(this.shake, this);
 
        this.game.stage.addChild(this);
 
this.skill = false; this.skill_1 = false; this.moveLeft = false; this.moveRight = false;
 
}
Link to comment
Share on other sites

that is fine - there are different ways to handle input. Your are listening to input callbacks, that's one way to do it. You can also do it in the update() method and check everytime if the mouse button is down, example here - https://phaser.io/examples/v2/arcade-physics/shoot-the-pointer

I would also take this opportunity if you want to explore phaser demos in es6 - my various Phaser demo tutorials are all in es6 https://www.programmingmind.com/projects

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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