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 Cadence96

Let's say I have a weapon entity with an offset.
offset: {x:-65, y:2},

And this is the init condition that defines the flip of my player entity.
 this.vel.x = this.accel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);

If my player flips I want to change the offset of the weapon, I'm trying this way:
  this.vel.x = this.accel.x = (settings.flip ? -this.offset.x = 65 : this.offset.x = -65);

But my text editor shows me an error in that syntax.

This is how the code is moduled:
EntityBullet = ig.Entity.extend({
	offset: {x:-65, y:2},
        init: function( x, y, settings ){
	  this.parent( x + (settings.flip ? -4 : 8), y+8, settings );
	  this.vel.x = this.accel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
     }
})

¿Does anyone know the syntax how to do change the offset when flipping? Or a better way than I am trying?

Thanks


PD: when I scroll to the bottom of the 'class-reference/entity page' the fixed menu stops scrolling and I can not see the complete list of entities.
My browser is Firefox, SO Windows 7.
http://impactjs.com/documentation/class-reference/entity

1 decade ago by Joncom

Perhaps something like this...
faceDirection = function(direction) {
    switch(direction) {
        case "left":
            this.currentAnim.flip.x = false;
            this.offset = { x: -65, y: 2 }; // adjust offset to your liking
            break;
        case "right":
            this.currentAnim.flip.x = true;
            this.offset = { x: -65, y: 2 }; // adjust offset to your liking
            break;
    }
}

1 decade ago by Jerczu

EntityBullet = ig.Entity.extend({
    offset: {x:-65, y:2},
        init: function( x, y, settings ){
      this.parent( x + (settings.flip ? -4 : 8), y+8, settings );
      this.vel.x = this.accel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
     }
})

It probably gets funny about
this.parent( x + (settings.flip ? -4 : 8), y+8, settings );

plus inline conditional is wrong
this.vel.x = this.accel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x); 

Your conditional should be notice parenthesis
(settings.flip)?-this.maxVel:this.maxVel.x;

This conditional is also wrong
this.parent( x + (settings.flip ? -4 : 8), y+8, settings );

should be
this.parent( x + (settings.flip) ? -4 : 8, y+8, settings );

instead fiddling with
this.parent( x + (settings.flip ? -4 : 8), y+8, settings );

just do similar conditionals for offsets like @Joncom did as it seems you are struggling with one liners.

1 decade ago by Cadence96

Thanks for answers, sorry for not replying earlier, for job reasons I hadn't time to do.

I'm sure this question sounds noobish, but where do I get the 'direction' parameter?
I've placed the faceDirection block inside the EntityBullet, and I understand the logic of this switch condition, but I don't know how to set a direction left/right.

faceDirection: function(direction) {
    switch(direction) {
        case "left":
            this.currentAnim.flip.x = false;
            this.offset = { x: -65, y: 2 }; // adjust offset to your liking
            break;
        case "right":
            this.currentAnim.flip.x = true;
            this.offset = { x: 65, y: 2 }; // adjust offset to your liking
            break;
    }
}

This is all the player code, I've removed not necessary code (animations, etc)

ig.module(
    'game.entities.player'
)
.requires(
    'impact.entity'
)
.defines(function(){
    EntityPlayer = ig.Entity.extend({
      
		animSheet: new ig.AnimationSheet( 'media/mysprite.png', 300, 240 ), // yeah, character is big
        size: {x: 300, y: 240},
        offset: {x: 0, y: 0},
        flip: false,
	    weapon: 0,
	    totalWeapons: 2,
	    activeWeapon: "EntityBullet",
	    init: function(x, y, settings) {
            this.parent(x, y, settings);
	        this.setupAnimation(this.weapon);	  
	    },
		setupAnimation: function(offset){
			offset = offset * 4;
			this.addAnim('idle', 1, [0+offset]);
			
		},
        update: function() {
              // move left or right
        	var accel = this.standing ? this.accelGround : this.accelAir;
        	if( ig.input.state('left') ) {
        		this.accel.x = -accel;
        		this.flip = true;
        	}else if( ig.input.state('right') ) {
        		this.accel.x = accel;
        		this.flip = false;
        	}else{
        		this.accel.x = 0;
        	}
        	
			this.parent();
            // shoot
            if( ig.input.pressed('shoot') ) {
                ig.game.spawnEntity( this.activeWeapon, this.pos.x, this.pos.y, {flip:this.flip} );
            }
			
            this.currentAnim.flip.x = this.flip;
        	// move!
        	this.parent();
        },
    });
    EntityBullet = ig.Entity.extend({
        size: {x: 5, y: 3},
        offset: {x:5, y:2},
	    animSheet: new ig.AnimationSheet( 'media/giant-bullet.png', 53, 45 ),
        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 : 8) , y+8, settings );
            this.vel.x = this.accel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
            this.addAnim( 'idle', 0.2, [0] );
			this.currentAnim.flip.x = settings.flip;
	
        },
		faceDirection: function(direction) {
        switch(direction) {
        case "left":
            this.currentAnim.flip.x = false;
            this.offset = { x: -65, y: 2 }; // adjust offset to your liking
            break;
        case "right":
            this.currentAnim.flip.x = true;
            this.offset = { x: 65, y: 12 }; // adjust offset to your liking
            break;
         }
       },
		
        handleMovementTrace: function( res ) {
            this.parent( res );
            if( res.collision.x || res.collision.y ){
                this.kill();
            }
        },
        check: function( other ) {
            other.receiveDamage( 3, this );
            this.kill();
        }
		
    });
    
});

Page 1 of 1
« first « previous next › last »