Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

8 years ago by peterdragontail

I am pretty new to Impact so I may have missed something obvious here.

I am working on a fairly straightforward Mario-esque sidescroller and I am having some trouble with "patrolling" enemies. As it stands right now, only one entity is "patrolling" (turning around after 1 second) while all of the others move forward and only turn around when they hit a collision block. If I kill the properly behaving entity then one of the other ones starts behaving properly. I am pretty sure that only one entity responds to the timer and then it gets reset, preventing the other ones from passing the check, but shouldn't they all just turn around at once?

Here is the code for the entity
ig.module(
        'game.entities.flyingbug'
    )
    .requires(
        'impact.entity'
    )
    .defines(function () {

        EntityFlyingbug = ig.Entity.extend({
            size: {
                x: 16,
                y: 16
            },
            offset: {
                x: 2,
                y: 2
            },
            maxVel: {
                x: 100,
                y: 100
            },
            friction: {
                x: 150,
                y: 0
            },

            type: ig.Entity.TYPE.B, // Evil enemy group
            checkAgainst: ig.Entity.TYPE.A, // Check against friendly
            collides: ig.Entity.COLLIDES.PASSIVE,
            gravityFactor: 0,

            health: 2,


            speed: 46,
            flip: false,

            animSheet: new ig.AnimationSheet('media/bug.png', 20, 20),


            init: function (x, y, settings) {
                this.parent(x, y, settings);

                this.addAnim('crawl', 0.2, [0]);
                this.addAnim('dead', 1, [0]);


            },


            update: function () {

                if (typeof timer === 'undefined') {
                    timer = new ig.Timer();
                }
                if (timer.delta() >= .5) {
                    this.flip = !this.flip;

                    // We have to move the offset.x around a bit when going
                    // in reverse direction, otherwise the blob's hitbox will
                    // be at the tail end.
                    //      this.offset.x = this.flip ? 0 : 0;

                    timer = new ig.Timer();
                }

                var xdir = this.flip ? -1 : 1;
                this.vel.x = this.speed * xdir;
                this.currentAnim.flip.x = !this.flip;

                this.parent();
            },



            kill: function () {
                this.parent();

            },

            handleMovementTrace: function (res) {
                this.parent(res);

                // Collision with a wall? return!
                if (res.collision.x) {
                    this.flip = !this.flip;
                    this.offset.x = this.flip ? 0 : 0;
                }
            },

            check: function (other) {
                other.receiveDamage(1, this);
            }
        });

    });

8 years ago by lTyl

ig.module(
        'game.entities.flyingbug'
    )
    .requires(
        'impact.entity'
    )
    .defines(function () {

        EntityFlyingbug = ig.Entity.extend({
            size: {
                x: 16,
                y: 16
            },
            offset: {
                x: 2,
                y: 2
            },
            maxVel: {
                x: 100,
                y: 100
            },
            friction: {
                x: 150,
                y: 0
            },

            type: ig.Entity.TYPE.B, // Evil enemy group
            checkAgainst: ig.Entity.TYPE.A, // Check against friendly
            collides: ig.Entity.COLLIDES.PASSIVE,
            gravityFactor: 0,

            health: 2,


            speed: 46,
            flip: false,
timer: null,

            animSheet: new ig.AnimationSheet('media/bug.png', 20, 20),


            init: function (x, y, settings) {
                this.parent(x, y, settings);

                this.addAnim('crawl', 0.2, [0]);
                this.addAnim('dead', 1, [0]);
this.timer = new ig.Timer();


            },


            update: function () {

                if ( this.timer.delta() >= .5) {
                    this.flip = !this.flip;

                    // We have to move the offset.x around a bit when going
                    // in reverse direction, otherwise the blob's hitbox will
                    // be at the tail end.
                    //      this.offset.x = this.flip ? 0 : 0;
this.timer.reset();
                }

                var xdir = this.flip ? -1 : 1;
                this.vel.x = this.speed * xdir;
                this.currentAnim.flip.x = !this.flip;

                this.parent();
            },



            kill: function () {
                this.parent();

            },

            handleMovementTrace: function (res) {
                this.parent(res);

                // Collision with a wall? return!
                if (res.collision.x) {
                    this.flip = !this.flip;
                    this.offset.x = this.flip ? 0 : 0;
                }
            },

            check: function (other) {
                other.receiveDamage(1, this);
            }
        });

    });

Does this work?

8 years ago by stahlmanDesign

Make sure you are accessing this.timer as @lTyl shows in his code. The original code is setting a global variable timer outside of the entity. This would lead to unexpected behaviour.

8 years ago by Joncom

Quote from stahlmanDesign
The original code is setting a global variable timer outside of the entity. This would lead to unexpected behaviour.
Ya. Basically the first entity resets the timer before the others get their chance.

8 years ago by peterdragontail

That works, thanks for the help!
Page 1 of 1
« first « previous next › last »