2

I am trying to convert the Luxurious animals tutorial on combining createjs and box 2d, to Typescript. So far the mess I'm making seems to be working. But now I'm getting a Uncaught TypeError: Cannot call method 'push' of undefined on this line:

this.actors.push(this);

But actors is defined on the .ts as private actors = [];. I've tried may ways but none work. Anyone knows whats going on?

app.ts => http://paste.laravel.com/8HM default.html => http://paste.laravel.com/8HN

Using VS 2012.

2 Answers 2

1

Try private actors:TYPE[] = []; where TYPE is the type of your array, e.g. for an array of strings private actors:string[] = [];

This seems to be working for me.

If not, then maybe post some more code - I can't get your .ts files to come up in source view from the links you've posted, and I can't tell which of the (several) scripts in the page your issue is with.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, pasted the same link twice. Fixed.
I can't see immediately what the issue is. Perhaps try tracing 'this' to make certain it is actually the Box2d, as if not the actors var will be out of scope.
Ahh... I think I've got it. this traces as Box2d.actorObject, meaning that this.actors would be Box2d.actorObject.actors, wich is undefined? Maybe a TS bug? BEcause from what I remember on the intro video, Anders said that TS would automatically add that = this, or something of the sort.
I only just had time to glance at the code, but it looked like the correct closure was Box2d.actorObject, which is why I suggested tracing this. I don't think it's a bug - and it should be easy enough to fix, either by passing the Box2d value in or creating a static singleton you can reference from anywhere (I've been doing this with my root controller in TS to make accessing it easier, but obviously it's only appropriate in some circumstances. Anyway, hope you get it fixed.
1

Ok, so the problem was that it wasn't calling actorObject as a method, but creating it as an object: new Box2d.actorObject.

I just made is as a class of it's own and it's all pretty now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.