Enemy entity movement issue

37 views
Skip to first unread message

fereshta...@gmail.com

unread,
Nov 4, 2016, 10:18:39 PM11/4/16
to melonJS - A lightweight HTML5 game engine
This is probably not a bug in the melonjs tutorial but lack of my understanding of it, I was lucky to find a sprite animation for an enemy entity that seemed interesting to me (http://opengameart.org/content/frog-player-spritesheets) however the tutorial animation direction is the opposite and my enemy entity kept moving in one direction while the animation represented movement in opposite direction. I finally got it figured out i think but then when the enemy reaches the end of its path it does not flip around and go the other way. It is probably something simple that I've overlooked and after staring at it for a long time I just need a break and figured I should post it here incase someone spots my mistake easily and could point out the fix... thanks in advance.

/**
 * Enemy Entity
 */
game.EnemyEntity = me.Entity.extend(
{    
    init: function (x, y, settings)
    {
        // define this here instead of tiled
        settings.image = "frog";
          
        // save the area size defined in Tiled
        var width = settings.width;
        var height = settings.height;

        // adjust the size setting information to match the sprite size
        // so that the entity object is created with the right size
        settings.framewidth = settings.width = 64;
        settings.frameheight = settings.height = 64;

        // redefine the default shape (used to define path) with a shape matching the renderable
        settings.shapes[0] = new me.Rect(0, 0, settings.framewidth, settings.frameheight);
        
        // call the parent constructor
        this._super(me.Entity, 'init', [x, y , settings]);
        
        // set start/end position based on the initial area size
        x = this.pos.x;
        this.startX = x;
        this.endX   = x + width - settings.framewidth
        this.pos.x  = x + width - settings.framewidth;

        // to remember which side we were walking
        this.walkLeft = false;

        // walking & jumping speed
        this.body.setVelocity(4, 6);
    },
    
    // manage the enemy movement
    update : function (dt)
    {            
        if (this.alive)
        {
            if (this.walkLeft && this.pos.x >= this.endX)
            {
                this.walkLeft = true;
            }
            else if (!this.walkLeft && this.pos.x <= this.startX)
            {
                this.walkLeft = false;
            }
            
            this.renderable.flipX(this.walkLeft);
            this.body.vel.x += (this.walkLeft) ? this.body.accel.x * me.timer.tick : -this.body.accel.x * me.timer.tick;

        }
        else
        {
            this.body.vel.x = 0;
        }
        // check & update movement
        this.body.update(dt);
            
        // handle collisions against other shapes
        me.collision.check(this);
            
        // return true if we moved or if the renderable was updated
        return (this._super(me.Entity, 'update', [dt]) || this.body.vel.x !== 0 || this.body.vel.y !== 0);
    },
    
    /**
     * colision handler
     * (called when colliding with other objects)
     */
    onCollision : function (response, other) {
        if (response.b.body.collisionType !== me.collision.types.WORLD_SHAPE) {
            // res.y >0 means touched by something on the bottom
            // which mean at top position for this one
            if (this.alive && (response.overlapV.y > 0) && response.a.body.falling) {
                this.renderable.flicker(750);
            }
            return false;
        }
        // Make all other objects solid
        return true;
    }
});

Jay Oster

unread,
Nov 4, 2016, 10:34:57 PM11/4/16
to mel...@googlegroups.com
I think it would be easier to just invert the value passed to the `flip` helper method. Copy-pasta from the tutorial, and make just this one change:

this.renderable.flipX(!this.walkLeft);

The exclamation point ("logical NOT operator") will invert the value of `this.walkLeft`, and cause the sprite to face the opposite direction. You'll have to undo the other changes you did with the position comparisons. But this one-liner is all you really need!

--
You received this message because you are subscribed to the Google Groups "melonJS - A lightweight HTML5 game engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to melonjs+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

fereshta...@gmail.com

unread,
Nov 8, 2016, 2:14:59 PM11/8/16
to melonJS - A lightweight HTML5 game engine
Thanks! I will have to make the change to see if this works!
To unsubscribe from this group and stop receiving emails from it, send an email to melonjs+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages