Jump to content

Implementing knockback into my game?


Ansis100
 Share

Recommended Posts

So this is gonna be a really long snippet, because I can't really shorten it well enough to show the problem. Working example - https://testarea.krizucentrs.lv.

SideScroller.Game.prototype = {

    preload: function () {

        this.game.time.advancedTiming = true;

    },

    create: function () {

        // CREATE ASSETS
        this.map = this.game.add.tilemap( 'level1' );
        this.map.addTilesetImage( 'platformer_tiles', 'gameTiles' );
        this.blockLayer = this.map.createLayer( 'blockLayer' );

        this.player = this.game.add.sprite( 100, 10, 'player' );
        this.dummy = this.game.add.sprite( 700, 10, 'player' );
        this.sword = this.game.make.sprite( 40, 40, 'sword' );
        this.player.addChild( this.sword );

        this.damageActive = false;
        this.damaging = false;
        this.sword.alpha = 0;

        this.dummy.setHealth( 100 );

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

        // PHYSICS
        this.game.physics.arcade.enable( this.blockLayer );
        this.game.physics.arcade.enable( this.player );
        this.game.physics.arcade.enable( this.dummy );

        this.map.setCollisionBetween( 1, 10000, true, this.blockLayer );
        this.game.world.setBounds( 0, 0, 3000, 1000 );

        this.player.body.gravity.y = 1500;
        this.dummy.body.gravity.y = 1500;
        // this.dummy.body.immovable = true;
        // this.dummy.moves = false;

        // MISC
        this.game.camera.follow( this.player );

    },

    update: function () {
        // COLLISIONS
        this.game.physics.arcade.collide( this.player, this.blockLayer );
        this.game.physics.arcade.collide( this.dummy, this.blockLayer );
        this.game.physics.arcade.collide( this.dummy, this.player );

        // PLAYER MOVEMENT
        this.player.body.velocity.x = 0; 

        if ( this.cursors.up.justDown ) {
            if ( this.player.body.onFloor() ) {
                this.player.body.velocity.y = -500;
            }
        }

        if ( this.cursors.left.isDown ) {
            this.player.body.velocity.x = -200;
        } else if ( this.cursors.right.isDown ) {
            this.player.body.velocity.x = 200;
        }

        // SWORD DISPLAY MECHANICS
        if ( this.cursors.left.justDown ) {
            this.sword.position.setTo( 0, 40 );
            if ( this.sword.scale.x > 0 ) {
                this.sword.scale.x *= -1;
            }
        } else if ( this.cursors.right.justDown ) {
            this.sword.position.setTo( 40, 40 );
            if ( this.sword.scale.x < 0 ) {
                this.sword.scale.x *= -1;
            }
        }

        // HITBOX DETECTION
        if ( this.spaceKey.justDown ) {
            this.sword.alpha = 1;
            if ( this.sword.overlap( this.dummy ) && this.dummy.alive ) {
                // this.damaging = true;
                // this.dummy.body.velocity.x = 0;
                this.dummy.damage( 10 );
                // this.dummy.body.drag.x = 70;
                // this.dummy.body.velocity.x = 200;
                // this.dummy.body.velocity.y = -200;
                console.log( 'damage!' );
                this.game.time.events.add( Phaser.Timer.SECOND / 10, function () {
                    this.sword.alpha = 0;
                }, this );
                // this.game.time.events.add( Phaser.Timer.SECOND / 2, function () {
                //     this.damaging = false;
                //     this.dummy.body.drag.x = 0;
                // }, this );
            } else {
                this.game.time.events.add( Phaser.Timer.SECOND / 10, function () {
                    this.sword.alpha = 0;
                }, this );
            }
        }

        // DEBUG
        if ( this.damaging ) {
            console.log( true );
        } else {
            console.log( false );
        }

        // ENEMY AI
        if ( !this.damaging ) {
            if ( this.player.position.x < this.dummy.position.x ) {
                this.game.time.events.add( Phaser.Timer.QUARTER, function () {
                    this.dummy.body.velocity.x = -50;
                }, this );
            } else if ( this.player.position.x > this.dummy.position.x ) {
                this.game.time.events.add( Phaser.Timer.QUARTER, function () {
                    this.dummy.body.velocity.x = 50;
                }, this );
            }

            if ( ( this.dummy.body.blocked.left || this.dummy.body.blocked.right ) && this.dummy.body.blocked.down ) {
                this.dummy.body.velocity.y = -500;
            }
        }
    },
};

I want to implement knockback on every overlap of this.sword and this.dummy. The large amount of commented lines is how I managed to do it in a hacky way, but it started lagging and glitching and didn't provide x knockback, only y. The x velocity physics are so confusing and frustrating! Have you guys played Terraria? I'd like this sword mechanic to be similar to the shortsword - it appears, enemy gets knocked back and continues walking my way. However, I'm having way too much trouble making it work like that. My main problem is that dummy keeps the x velocity it was assigned by the ENEMY AI lines. I don't know how to "pause" it long enough for the knockback to work. Even then, theoretically, it should've worked when I uncommented the lines, but then it gets all buggy - the x doesn't work, the player starts randomly getting stuck with 1-direction velocity with no control over it etc. I think you can uncomment those lines in inspect element, just search for Game.js. I hope the code is understandable enough, I don't really know how to shorten it for such a problem.

I'm eternally grateful, if you've gotten this far, understand my code and are willing to help.

Also, I've no idea how to define a new function, this prototype object thing is weird.

Link to comment
Share on other sites

It's possible you need extra checks in the timer event calls, because circumstances changed after they were scheduled.

{
  dummyWalkLeft: function () {
    if (this.damaging || (!this.dummy.body.blocked.down)) {
      return;
    }

    this.dummy.body.velocity.x = -50;
  },

  dummyWalkRight: function () {
    if (this.damaging || (!this.dummy.body.blocked.down)) {
      return;
    }

    this.dummy.body.velocity.x = 50;
  },

  update: function () {
    // […]
    
    // ENEMY AI
    if ( !this.damaging ) {
      if ( this.player.position.x < this.dummy.position.x ) {
        this.game.time.events.add( Phaser.Timer.QUARTER, this.dummyWalkLeft, this );
      } else if ( this.player.position.x > this.dummy.position.x ) {
        this.game.time.events.add( Phaser.Timer.QUARTER, this.dummyWalkRight, this );
      }
      
      if ( ( this.dummy.body.blocked.left || this.dummy.body.blocked.right ) && this.dummy.body.blocked.down ) {
        this.dummy.body.velocity.y = -500;
      }
    }
    
    // […]
  }
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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