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

9 years ago by BlairH

I have tried to implement this in my last two games, with utter failure as a result. I don't even have any code to share because none even came close to working.

So i am hoping someone here can help me.

The title says it all, I am trying to get a player entity to zig and zag as it moves across the screen.

I have searched the forums and there does not seem to be any examples.

Thanks

9 years ago by Joncom

Untested:

EntityPlayer = ig.Entity.extend({
    timer: null,
    speed: 64,
    init: function(x, y, settings) {
        this.parent();
        this.timer = new ig.Timer();
    },
    update: function() {
        var angle = Math.sin(this.timer.delta());
        this.setVelocityByAngle(angle, this.speed);
       
        // Disallow leftward movement (prevents running in a circle)
        this.vel.x = Math.abs(this.vel.x);

        this.parent();
    },
    setVelocityByAngle: function(angle, velocity) {
        this.vel.x = Math.cos(angle) * velocity;
        this.vel.y = Math.sin(angle) * velocity;
    }
});

9 years ago by BlairH

Thanks, it worked.

I was able to modify it for my needs.

You have no idea how long I worked at trying get this to work.

9 years ago by Joncom

Glad to help.
Page 1 of 1
« first « previous next › last »