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 paulh

Cant believe i cant get this to work :-(

update: function(){
		if (ig.input.pressed('click')) {

		 ig.game.spawnEntity('EntityInput', ig.input.mouse.x,ig.input.mouse.y);
			console.log("spawn");        
			}
			else if (ig.input.released('click')) {
				var cursor = this.getEntitiesByType( 'EntityInput' );
			
// WHAT DO I DO HERE TO KILL THE SPWNED ENTITY INPUT

					console.log("release");
			}
		},


Tried these :-/

				cursor.kill();
				this.cursor.kill();
				ig.game.removeEntity('EntityInput' );
				removeEntity('EntityInput' );
				

1 decade ago by jswart

Isn't this code going to spawn AND kill every time?

You will always press and release the mouse given this cycle.

Try a flag on the entity? Spawn the entity on click -> on next update set a flag making it killable -> next click == death

1 decade ago by paulh

Yes it should die when the mouse is released .. i also tried a :

ig.game.state = false;

flag and then this in the entity .. but no joy, it's like the update isnt called in the entity?


	update: function() {
		
	console.log (ig.game.state);
	if ( ig.game.state = false){
	this.kill();
        this.parent();	
	}

and this

	update: function() {
		 if (ig.input.released('click')) {
		console.log("release");
		this.kill();
		}	
console.log("nearer");        

		this.parent();
	}


i dont even get the console log???

full entity:


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

EntityInput = ig.Entity.extend({

	size: {x: 32, y:32},

	animSheet: new ig.AnimationSheet('media/touch.png#alpha:ff00ff',32,32),
	

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



	update: function() {
		 if (ig.input.released('click')) {
		console.log("release");
		this.kill();
		}	
console.log("nearer");        

		this.parent();
	}
	
});
});

1 decade ago by alexandre

I'm not clear about your intent. What does this entity look like, what are its states, and how is one supposed to interact with it to bring about changes in that state? e.g., a pushbutton, a toggle button, a text field, etc.

1 decade ago by paulh

Its a mouse cursor essentially, i want to spawn it on mouse down and remove it on mouse up ...

after that, i want to make it find the distance to player, display a different cursor type dependent upon the distance, and then if within a certain distance draw a line between the two entities and spawn some entities between the mouse cursor and the player, following the line .. failed at step one! I'd then pass some variables to playerentity on release if certain conditions are met.


the code im displaying is main.js .. funnily alexandre i was just going to have a play with your gravity plugin (instead of doing what i should be, lol)

1 decade ago by paulh

driving me insane, im not sure why any of those things dont work .. or this:


this.getEntitiesByType('EntityInput').kill();

1 decade ago by alexandre

instead of doing what i should be [doing]
Ah, but one should always switch activities when they're stuck on something. So what you're doing is right. :)

Cursor...
So, a lot of clicking = a lot of spawning/killing... Why don't you just show/hide it on mouse down/up?

EDIT: some revisions; moved cursor-specific behaviour to cursor.js. Keeps main.js light.

main.js:
init: function()
{
	this.cursor = this.spawnEntity(Cursor, 0, 0);
}

update: function()
{
	this.parent(;)
}

cursor.js:
animSheet: new ig.AnimationSheet(...)
visible: false,

init: function(x, y, settings)
{
	this.parent(x, y, settings);
	this.addAnim ('idle', 1, [0]);
	ig.input.bind(ig.KEY.MOUSE1, 'mouse1');
},

update: function()
{
	// handle mouse
	if (ig.input.pressed('mouse1'))
		this.visible = true;
	else if (ig.input.released('mouse1'))
		this.visible = false;

	if (this.visible)
	{
		// mouse was pressed and not yet released,
		// may or may not be on the move
		// i.e., we are visible. Time to do things...
		
		// First, follow the mouse
		this.pos.x = ig.input.mouse.x - this.size.x/2;
		this.pos.y = ig.input.mouse.y - this.size.y/2;
		
		// Now do what needs doing, or ask
		// delegate to do it for us
	}
	
	this.parent();
},

draw: function()
{
	if (this.visible)
		this.parent();
}

1 decade ago by paulh

Lol, good point.


Its a nice solution, and would work (and i greatly appreciate your time).. but i dont understand why i cant seem to access the update function in a spawned entity and why i cant seem to kill an entity after getting it's type .. its like the absolute basics :-(

I understand your solution is better though, but still if i cant use the update function of an entity, then i wont be able to do the other features or kill other things that will be spawned ..

Im more about the knoweldge is what i guess im saying, why cant i sem to kill a spawned entity!!! and why cant i even get a console.log to display in an entities update??? isnt the update of a spawned entity called every frame, and even if it isnt why doesn't it recognize a global bool or an input state change??

so annoyed with myself!

1 decade ago by paulh

and then i look at dominics player entity in jumpnrun and he


if( ig.input.state('left') ) {


does it all the time.

frustrating day .. 6 months after starting to learn code im still stumped at basics :-(


and docuemntation


 update: function() {
        // This method is called for every frame on each entity.
        // React to input, or compute the entity's AI here.
        
        if( ig.input.pressed('jump') ) {
            this.vel.y = -100;
            this.currentAnim = this.anims.jump.rewind();
        }
        
        // Call the parent update() method to move the entity
        // according to its physics
        this.parent(); 
    }


suggests that update like this:



	update: function() {
		
		console.log("frame");
		
	
       this.parent();
		
	}


should print Frame every frame?

1 decade ago by alexandre

EDIT: I had forgotten to call this.parent() from inside main.update. Now added. Should work okay.

Well first off, this (from your initial post) should work--provided you setup input binding first with ig.input.bind(ig.KEY.MOUSE1, 'click').

update: function()
{
	if (ig.input.pressed('click'))
	{
		console.log("spawn");
		ig.game.spawnEntity('EntityInput', ig.input.mouse.x,ig.input.mouse.y);
	}
	else if (ig.input.released('click'))
	{
		console.log("release");
		var cursor = this.getEntitiesByType( 'EntityInput' )[0];
		cursor.kill();
	}

	this.parent();
},

1 decade ago by paulh

Thats what i thought , not going mad :-)



ig.module( 
	'game.main' 
)
.requires(
	'impact.game',
	//'game.entities.ball',
	'game.plugins.local-gravity',
	'game.entities.player',
	'game.plugins.transparent-image',
	'game.levels.one',
	'game.entities.input',
	
	
	'impact.debug.debug', // <- Add this
	'game.plugins.debugdisplay'
)
.defines(function(){


MyGame = ig.Game.extend({

	clearColor: 'rgba(255,255,255,1)',
	gravity: 0,
	font: new ig.Font( 'media/04b03.font.png' ),

	

	init: function()
	{
		ig.game.state = true;
	
		
		ig.input.bind( ig.KEY.MOUSE1, 'click');
		this.loadLevel( LevelOne );
		
	},

	update: function()
{
    if (ig.input.pressed('click'))
    {
        console.log("spawn");
        ig.game.spawnEntity('EntityInput', ig.input.mouse.x,ig.input.mouse.y);
    }
    else if (ig.input.released('click'))
    {
        console.log("release");
        var cursor = this.getEntitiesByType( 'EntityInput' )[0];
        cursor.kill();
    }
},
		
	draw: function()
	{
		this.parent();
	}
});

ig.main('#canvas', MyGame, 30, 1024, 768, 1);

});


still does not kill spawned enttiy ...

1 decade ago by paulh


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

EntityInput = ig.Entity.extend({

	size: {x: 32, y:32},

	animSheet: new ig.AnimationSheet('media/touch.png#alpha:ff00ff',32,32),
	

    
	init: function( x, y, settings ) {
		this.parent( x, y, settings );
		console.log("spawned");
		
		this.addAnim( 'idle', 1, [0]  );
			console.log (ig.game.state);
	
	},



	update: function() {
		
		console.log("frame");
		
	
       this.parent();
		
	}
	
});
});


does not print Frame every frame!

1 decade ago by alexandre

EDIT: I had forgotten to call this.parent() from inside main.update and cursor.update. Now added. Should work okay.

You can also do this:

main.js:
update: function()
{
	if (ig.input.pressed('click'))
	{
		console.log("spawn");
		this.cursor = ig.game.spawnEntity('EntityInput', ig.input.mouse.x,ig.input.mouse.y);
	}
	else if (ig.input.released('click'))
	{
		console.log("release");
		this.cursor.kill();
	}

	this.parent();
},

cursor.js:
update: function()
{
	console.log('cursor alive, and possibly moving');
	this.parent();
},

Can you confirm that console shows:
spawn
cursor alive, and possibly moving...
release

?

1 decade ago by paulh

No .. i dont get that output .... something weird going on?

this output on two mouse clicks

spawn main.js:42
spawned input.js:19
true input.js:22
release main.js:47
spawn main.js:42
spawned input.js:19
true input.js:22
release 

1 decade ago by paulh

nothing in the level either really:

posting that data was a bad idea :-D

1 decade ago by alexandre

Hmm. I can take a quick look at it if you want. Send me your project via private channels (see contact info) or link thereto.

Now off to work I go. Hi-ho...

1 decade ago by paulh

Thanks, sent in email .. dont let me stop you working though!

I split all my work into unit tests now, so its a very simple project . not much more than what is above .. i found using small isolated bits of code for each feature/function much cleaner and easier for me in the long run.

1 decade ago by alexandre

Done. I had forgotten to call this.parent() from both classes' update methods; you used my code sample and of course, it was broken. Now should work. Will send you updated project by email in minutes. Cheers.
Page 1 of 1
« first « previous next › last »