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 there !

I'm making a tetris clone game and i need to trigger a second entity as soon as my first entity finishes to fall down, but i need to know if its possible to load a sprite randomly for one same entity.

I have really no idea if a line code is able to do that, like if it is possible for example to use an array for this...

Thanks :)

EDIT = i found this here but its a bit confusing :
http://impactjs.com/forums/help/the-way-to-define-a-sprite-on-an-entity

1 decade ago by UteEffacht

If you really only want to change the sprite that is shown, you could do it like that:

animSheet: ig.Image('media/allYourSprites.png'),

In your init:
this.addAnim('animA', 1, [0]);
this.addAnim('animB', 1, [1]);
...

And finally, when the first entity fall down:
this.currentAnim = this.anims[Math.floor(Math.random() * 7)];

where 7 is the number of animations you have. I am not sure though if you can access the animations via this index. If not, you need to map your animation names. If that all does not work, you could use a dirty switch:
switch (Math.floor(Math.random() * 7) {
    case 0:
        this.currentAnim = this.anims.animA;
        break;
    case 1:
        this.currentAnim = this.anims.animB;
        break;
...
}

Thats all untested and I am not sure about the Math.random() right now (I always need to look that up again...), but that should be roughly what you need.

1 decade ago by RationalGaze

Wow, looks great ! Thanks :)

1 decade ago by jswart

Wouldn't you already have all the entities set up? Like each tetroid would be its own entity with its animations and sprites defined.

Then in the main game loop you would just randomly generate a new entity and add it into the game?

Is there some reason you can't / don't want to do this?

1 decade ago by RationalGaze

Wouldn't you already have all the entities set up? Like each tetroid would be its own entity with its animations and sprites defined.

Then in the main game loop you would just randomly generate a new entity and add it into the game?

Is there some reason you can't / don't want to do this?


I'll go that way i think. But right now i'm confronted to a new problem, i can't call my new entity, here's the code :

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

EntityCubeplayer = ig.Entity.extend({

	size: {x:35, y:54},
	collides: ig.Entity.COLLIDES.ACTIVE,

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

	animSheet: new ig.AnimationSheet( 'media/cube.png', 35, 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;

		
	},

	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.pressed('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 + 27;	
		}
		if(this.pos.y >= 266) {
				this.pos.y = 266;
				this.vel.x = 0;
				this.time.pause();
			}
		
	
		this.parent();

	}
	

});	
		

SecondEntityCubeplayer = ig.Entity.extend({

	size: {x:64, y:64},
	collides: ig.Entity.COLLIDES.ACTIVE,
	timer: new ig.Timer(),

	animSheet: new ig.AnimationSheet( 'media/cube2.png', 64, 64 ), //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 + 27;	
		}
		if(this.pos.y >= 266) {
				this.pos.y = 266;
				this.vel.x = 0;
		}
		

		this.parent();

	}
	

});	

});


I dont find any way to put the following line

ig.game.spawnEntity(SecondCubePlayer, 64, 0)

i tried all kind of condition, i think i must create a function but what condition could trigger that second entity ?

1 decade ago by RationalGaze

Its okay i can spawn my entity now, just a condition problem :)
Page 1 of 1
« first « previous next › last »