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 campbellized

Hello everyone, I'm new to Impact and I'm trying to learn the basics of inputs, entities, sprite sheets and all of that good stuff. I've been following along with some tutorials as well as working through Jesse Freeman's book. Well, to get right down to the problem, I've run into a little issue and I'm trying to figure out the cause.

I've mapped out my controls and my basic movements and jump are working fine. However when I try to use my 'attack', I get no response. This is the code for my entity...

ig.module(
	'game.entities.player'
)
.requires(
	'impact.entity'
)
.defines(function(){
	EntityPlayer = ig.Entity.extend({
        animSheet: new ig.AnimationSheet( 'media/taffy.png', 9, 8 ),
        size: {x: 9, y:8},
        offset: {x: 1, y: 0},
        flip: false,
        maxVel: {x: 75, y: 150},
        friction: {x: 600, y: 0},
        accelGround: 400,
        accelAir: 200,
        jump: 250,
        init: function( x, y, settings ) {
        	this.parent( x, y, settings );
			//Add the animations
			this.addAnim( 'idle', 1, [0] );
			this.addAnim( 'run', 0.09, [0,1,2,3,4,5,6,7,8,9] );
			this.addAnim( 'jump', 1, [10] );
			this.addAnim( 'fall', 1, [11] );
			this.addAnim( 'bat', 10, [12] );
	},
        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;
        	}
        	// jump
        	if( this.standing && ig.input.pressed('jump') ) {
        		this.vel.y = -this.jump;
        	}
		// Attack
		if( this.standing && ig.input.pressed('attack') ) {
        		this.currentAnim = this.anims.bat.rewind();
        	}						
		// set the current animation, based on the player's speed
                if( this.vel.y < 0 ) {
            	     this.currentAnim = this.anims.jump;
                }else if( this.vel.y > 0 ) {
            	     this.currentAnim = this.anims.fall;
                }else if( this.vel.x != 0 ) {
            	     this.currentAnim = this.anims.run;
                }else{
            	     this.currentAnim = this.anims.idle;
                }
                this.currentAnim.flip.x = this.flip;
           	// move!
        	this.parent();
        }
	});
});

The strange thing for me is if I go into the Update function and move the If statement for the Attack below the next If statement, it works (although the animation is jittery).

                if( this.vel.y < 0 ) {
            	     this.currentAnim = this.anims.jump;
                }else if( this.vel.y > 0 ) {
            	     this.currentAnim = this.anims.fall;
                }else if( this.vel.x != 0 ) {
            	     this.currentAnim = this.anims.run;
                }else{
            	     this.currentAnim = this.anims.idle;
                }
		// Attack
		if( this.standing && ig.input.pressed('attack') ) {
        		this.currentAnim = this.anims.bat.rewind();
        	}

I'm sure this is some blatantly silly oversight on my part, but I'm really curious why one works and the other doesn't. I've spent hours trying to figure this out! I would be really grateful for any insight that you guys could provide. Thanks!

1 decade ago by Graphikos

It seems to me you are setting the attack and move animation in the same update. So even if you do set attack, its getting set to a move on the next lines. Do one or the other but not both.

1 decade ago by campbellized

I'm afraid that I'm not sure what you are trying to say. I thought that each entity has only one update function that updates every frame. It was my understanding that it would run through those If statements, check for input, and update the entity accordingly. Is there some sort of priority that I need to pay attention to? Or are you saying that I need to create a new function, separate from the Update function, that handles only the attack?

attack: function() {
        // Attack
        if( this.standing && ig.input.pressed('attack') ) {
                this.currentAnim = this.anims.bat.rewind();
            }
}

I created a new function after the Update function and that didn't work either.

1 decade ago by Graphikos

You are correct in your understanding an entity's update will run once every frame. The problem is you have two if statements that are evaluating as true and it is setting the currentAnim twice in the same update.

If you aren't moving (vel is 0) then its going to set as:

this.currentAnim = this.anims.idle;

Right? This is overwriting your:

this.currentAnim = this.anims.bat.rewind();

...every update so you never see attack animation.

1 decade ago by campbellized

It just clicked... that makes a lot of sense! Thanks for your patience, I'm still new to the game dev scene so I'm still picking up on some of these simple things.
Page 1 of 1
« first « previous next › last »