开发者

Weird problem with a custom bindable class and a vector

I'm having a very weird problem with a vector in my application.

Details...

I 开发者_开发问答have the following classes.

Person,Player,PlayerController.

Player extends Person. Person extends ObjectProxy in order to enable binding. So the Player class has the [Bindable] tag.

The PlayerController class contains a remote object calling a php method to receive a firstname and a lastname and when the CallResponder gets the result from the call,the result handler creates a Player instance. At that moment I am trying to push the player object into a Vector..

The problem is the following.

Every time the push method is called, the vector is being populated with the last player that was created but not just in the end of the vector. It replaces the other instances as well! So the vector always contains the most recent player instance but in every position of it. :S

I have also tried doing it with an Array and the results are the same.

Any thoughts on what I'm doing wrong? It's driving me crazy. :S


My guess is that you are pushing the same object reference into your vector after setting that reference to a new instance of Player, meaning that all of the items in your vector refer to the same object, which is always the newest object. I say "guess" because I haven't seen your code. What are you pushing into your vector, a local variable? A member variable?

Edit: Based on your comment below, try adding your new Player object to your vector using a local variable rather than from your member variable (player_):

var newPlayer:Player = new Player();
newPlayer.firstName = results[firstName];
newPlayer.lastName = results[lastName];
players_.push(newPlayer);
player_ = newPlayer;

You are doing what I suspected, which is adding multiple references to the same object to your vector. Since all of the references in your object refer to the same object, changing the one object changes ALL of the entries in your vector. Doing the above will create a brand new (and unique) Player object each time you add to your vector.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜