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 RationalGaze

Hi guys, yeah, i know, its me again, i'll never thought ImpactJS would be that hard to master even if i know i'm noob in programming.

so now here's my problem : i m actually working on a tetris - like game, firstly i did this :

if(counter > 1){
			this.timer.reset();
			this.pos.y = this.pos.y + 25;	
		}

		

		if(this.pos.y >= 266) {
				this.pos.y = 266;
				this.vel.x = 0;
			}


		if(counter > 1 && this.pos.y >= 266) {
				this.timer.pause();
				ig.game.spawnEntity(SecondEntityCubeplayer, 64, 0);

			}
		

	
		this.parent();

	}

i know, its ugly, but its only about working on the logic of the game.

let me explain it : i use a timer to give to my cube entity a "tile based" movement, and when he's down, he can't move anymore to the left/right. The prob is that the velocity is reduced to 0 ONLY when he is at 266 on the y axis. I dont use any collision parameters for my conditions, which is shit for the next cube : if he fall on the first cube, his velocity wont be reduced to 0

So my guess is that i have to use .collideWith but during hours and hours i gave it a try and i just cant.

I would like some explaination about this .collideWith method and about how to use it.

The only thing i did for now is to assign them a type.... -_-

1 decade ago by Heartless49

.collideWith(other, axis) is basically the same as check(other), however by using collideWith you are able to include the axis on which the two entities collide.

This would be perfect for what you're trying to do in that you would be able to do something like...
collideWith: function( other, axis ) {
    if(other instanceof EntityCube && axis == 'x') {
        if (other.pos.x < this.pos.x && this.vel.x < 0) {
            this.vel.x = 0;
        } else if (other.pos.x > this.pos.x && this.vel.x > 0) {
            this.vel.x = 0;
        }
    } else if(other instanceof EntityCube && axis == 'y') {
        if (other.pos.y > this.pos.y) {
            this.vel.y = 0;
        }
    }
}

Naturally, this is just a rough example, but it would be able to tell that you're trying to move left or right, and if you've hit another block it will stop you from move in that direction. Also, if you've hit a block below you, then it will stop you as well.

I'm not big on collision calls, so my knowledge isn't too great at it, but this should do the trick, or at least do it's job to explain how collideWith() works I hope, ^^

1 decade ago by RationalGaze

I'll try all that, thanks a lot bro :) i'll tell you if there's any problem or if it worked

1 decade ago by RationalGaze

It doesnt work...i really dont know why, it should do the job.

I tried also with .touches() and the second cube still can move.

Here's my code :

ig.module(
	'game.entities.cubeplayer'
)
.requires(
	'impact.entity'
)
.defines(function(){

EntityCubeplayer = ig.Entity.extend({

	size: {x:36, y:54},
	type:ig.Entity.TYPE.A,
	checkAgainst:ig.Entity.TYPE.B,
	collides: ig.Entity.COLLIDES.FIXED,

    timer: new ig.Timer(), //le timer se déclenche

	animSheet: new ig.AnimationSheet( 'media/femme_blonde_agenoux_gauche.png', 36, 54 ), //sprite meuf

	init: function( x, y, settings ) {
		this.addAnim( 'idle', 1, [0] );
		this.parent( x, y, settings );
		this.pos.x = 64;
		this.pos.y = 0;
		this.ready();

		
	},

	update: function() {

		var counter = this.timer.delta(); //on distribue la valeur du timer à la variable
		


		if( ig.input.state('left') ) {
			this.vel.x = -100; 
		}
		else if( ig.input.state('right') ) {
			this.vel.x = 100;
		}
		else if(ig.input.state('down') ) {
			counter++;
			if(counter >= 1) {
				this.pos.y = this.pos.y + 1;
				counter--;
			}
		}
		else {
			this.vel.x = 0;
		}

	
		if(counter > 1){
			this.timer.reset();
			this.pos.y = this.pos.y + 25;	
		}

		

		if(this.pos.y >= 266) {
				this.pos.y = 266;
				this.vel.x = 0;
			}


		if(counter > 1 && this.pos.y >= 266) {
				this.timer.pause();
				ig.game.spawnEntity(SecondEntityCubeplayer, 64, 0);

			}
		

	
		this.parent();

	}


});	
		

SecondEntityCubeplayer = ig.Entity.extend({

	size: {x:36, y:54},
	type: ig.Entity.TYPE.B,
	checkAgainst: ig.Entity.TYPE.A,
	collides: ig.Entity.COLLIDES.FIXED,
	timer: new ig.Timer(),

	animSheet: new ig.AnimationSheet( 'media/femme_blonde_agenoux_droite.png', 36, 54 ), //sprite meuf

	init: function( x, y, settings ) {
		this.addAnim( 'idle', 1, [0] );
		this.parent( x, y, settings );
		},

	update: function() {

		var counter = this.timer.delta(); //on distribue la valeur du timer à la variable


		if( ig.input.state('left') ) {
			this.vel.x = -100; 
		}
		else if( ig.input.state('right') ) {
			this.vel.x = 100;
		}
		else if(ig.input.state('down') ) {
			counter++;
			if(counter >= 1) {
				this.pos.y = this.pos.y + 1;
				counter--;
			}
		}
		else {
			this.vel.x = 0;
		}

	
		if(counter > 1){
			this.timer.reset();
			this.pos.y = this.pos.y + 25;	
		}

		if(this.pos.y >= 266) {
				this.pos.y = 266;
				this.vel.x = 0;
		}

		this.parent();

	},

	collideWith: function( other, axis ) {
    if(other instanceof SecondEntityCubeplayer && axis == 'x') {
        if (other.pos.x < this.pos.x && this.vel.x < 0) {
            this.vel.x = 0;
        } else if (other.pos.x > this.pos.x && this.vel.x > 0) {
            this.vel.x = 0;
        }
    } else if(other instanceof SecondEntityCubeplayer && axis == 'y') {
        if (other.pos.y > this.pos.y) {
            this.vel.y = 0;
        }
    }
}

	

});	

});

1 decade ago by RationalGaze

I tried all i could possibly do, but for a tetris like game collideWith is the only method i should use to resolve this problem

1 decade ago by RationalGaze

Okay so here's a little update. I restart from the beginning, here's my new code :


ig.module(
	'game.entities.player'
)
.requires(
	'impact.entity'
)
.defines(function(){

EntityPlayer = ig.Entity.extend({

	type: ig.Entity.TYPE.A,
	checkAgainst: ig.Entity.TYPE.B,
	collides: ig.Entity.COLLIDES.NEVER,

	size: {x:36, y:54},

	timer: new ig.Timer(),

	animSheet: new ig.AnimationSheet('media/femme_blonde_agenoux_gauche.png', 36, 54 ),

	init: function( x, y, settings ) {
		this.addAnim( 'idle', 1, [0] );
		this.parent( x, y, settings );
		this.pos.x = 30;
		this.pos.y = 30;

	},

	update: function() {

		

		this.parent();

		if(ig.input.state('left') ) {
			this.vel.x = -100;
		}
		else if( ig.input.state('right') ) {
			this.vel.x = 100;
		}
		else {
			this.vel.x = 0
		}

	},


	collideWith: function( other, axis ) {
    if(other instanceof EntityPlayer && axis == 'x') {
        if (other.pos.x < this.pos.x && this.vel.x < 0) {
            this.kill();
        } else if (other.pos.x > this.pos.x && this.vel.x > 0) {
            this.kill();
        }
    } else if(other instanceof EntityPlayer && axis == 'y') {
        if (other.pos.y > this.pos.y) {
            this.kill();
        }
    }
}
   



});

});


So my idea here is just to test collisions, i put two entities at the same level on the X Axis and if the player touches the other entity, he dies. I tested ALL the collisions types, put NONE/A/B in any order, it just doesnt work. Hope this code will help you to clear it more easily

EDIT : finally i did something, i finally killed my entity. But i go back to my first problem...
Page 1 of 1
« first « previous next › last »