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

1 decade ago by TrexKikBut

I want to make the rocket flip when the player is facing the other way, but I can't seem to get it to work. Here is the code:

EntityRocket = ig.Entity.extend({
        size:{x:18, y:7},
        animSheet: new ig.AnimationSheet( 'media/rocket.png', 18, 7),
        maxVel: {x: 200, y: 0},
        type: ig.Entity.TYPE.NONE,
        checkAgainst: ig.Entity.TYPE.B,
        collides: ig.Entity.COLLIDES.PASSIVE,
         init: function( x, y, settings ) {
            this.parent( x + (settings.flip ? -4 : 12) , y+4, settings );
            this.vel.x = this.accel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
            this.addAnim( 'idle', 0.2, [0] );
        },
        handleMovementTrace: function( res ) {
            this.parent( res );
            if( res.collision.x || res.collision.y ){
                this.kill();
            }
        },
        check: function( other ) {
            other.receiveDamage( 10, this );
            this.kill();
        },
         kill: function(){
            for(var i = 0; i < 20; i++)
                ig.game.spawnEntity(EntityGrenadeParticle, this.pos.x, this.pos.y);
            this.parent();
        }
        
        
    });

So how can I get it to flip?

1 decade ago by Joncom

Untested.
EntityRocket = ig.Entity.extend({
    
    ....    

    // You will pass in the player entity when you spawn this entity.
    player: undefined,

    ....

    update: function() {
    
        // Flip only if player is flipped.
        if(this.player.currentAnim.flipX) this.currentAnim.flipX = true;
        else this.currentAnim.flipX = false;

        // Call parent.
        this.parent();
    },

    ....

    kill: function() {

        // Prevent rockets from being kept in memory once killed.
        this.player = undefined;

        ....

        // Call parent.
        this.parent();
    },

    ....

});

1 decade ago by Arantor

How about simply changing that if to:

this.currentAnim.flipX = this.player.currentAnim.flipX;

1 decade ago by TrexKikBut

That doesn't seem to work.

1 decade ago by Joncom

Quote from Brody
That doesn't seem to work.
When you spawn a rocket, it's important that you pass in the player entity. Something like this:

// Needed to pass into rocket.
var playerEntity = ig.game.getEntitiesByType(EntityPlayer)[0];

// Spawn rocket.
ig.game.spawnEntity(
    EntityRocket, 
    playerEntity.pos.x, 
    playerEntity.pos.y, 
    {
        player: playerEntity 
    }
);

1 decade ago by TrexKikBut

I got it working, thanks!
Page 1 of 1
« first « previous next › last »