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 Aitor

I have injected to the Entity class so that It calls a method each frame passing itself as a parameter. This is de code (It's not the real code, but it is similar):
ig.Entity.inject({
	    funcToCall:undefined,

            update:function()
	    {
		if (funcToCall)
		     funcToCall(this);

		this.parent();
	    }
}

And It works fine if I only acces properties like pos or vel of the entity from the function that is called. But some entities have a flying property, and I want to know if that property is true. So i do this in the called function (It's in ig.game):
	testFunc:function(entity)
	{
			if (entity.flying)
			{
				console.log("flying");
			}
			
	}

But "flying" is never printed, even If the flying property is true. Does this happen because I pass this as a parameter in an injected method of Entity, and I can&039;t acces the subclass's properties? But if I do #entity instanceof EntitySubclass (replacing EntitySubclass with the kind of the entity that has the flying property) it returns true. So why I can't acces the flying property
P.D: I've tried calling a method of the subclass instead of accesing a property, and it works. For example, I can do this:
	testFunc:function(entity)
	{
			if (entity.aMethodOfTheSubclass)
			{
				aMethodOfTheSubclass();
			}
			
	}

and aMethodOfTheSubclass is called

1 decade ago by Aitor

I have solved It making getters and setters for every property that I want to acces from the function that is called in the Entity's update. It works, but I still would like to know why I can't acces properties directly. Why when I pass this as a parameter to a function on the Entity class's update method the function can only acces Entity's properties and methods and the subclass's methods, but not the subclass's properties?

1 decade ago by Aitor

After testing a bit more I've noticed that i actually can get properties, what I can't do Is modify values, as if the parameter pasn't to funcToCall was a copy of this, and not a pointer to this (well, I know there aren't pointers in javascript, but I mean an object that acts like a pointer)

1 decade ago by Aitor

Solved. The problem wasn't that I couldn't modify values, was that I was trying to assign a method of the Entity to a variable, and then call that func, like this:
var func=entity.aFunction;
if (func)
      func();

and in that function I did this.flying=true. But func&039;s #this wasn't the entity where the function came from, because the func was a separate variable, and not part of the entity object. I solved it refactoring the code to this:
if (entity.aFunction)
      entity.aFunction();

I still have a bit of trouble understanding well how javascript and the prototyping work. Does anyone know a good tutorial that explains the oop in javascript well?

1 decade ago by Joncom

Thanks for including the solution... You never if someone might run into a similar problem later on.

1 decade ago by Bushstar

Impact's class object is based on John Resig's inheritance code. You can read about it here.

http://ejohn.org/blog/simple-javascript-inheritance/

There's also a book on Impact that can be read on the O'Reilly online bookshelf.

http://my.safaribooksonline.com/book/-/9781449331207
Page 1 of 1
« first « previous next › last »