I am using Titanium Appcelerator.
I have an array of buttons that I've created for a game. There is one array of buttons per player. I also have an array of player objects, and each one has a "score" property.
When a player presses one of the buttons, which all have an assigned value, the player to whom this button belongs is to have his score updated.
Here is how I am creating a set of buttons for each player:
for(var i=0;i<7;i++){
playerButtons[i] = Titanium.UI.createButton({
myPlayer: thePlayer,
index: i,
value: 50,
touchEnabled: true,
});
}
I am setting a property of "myPlayer" to the player that the button belongs to. This is so that I can associate it to the player when pressing it, and so that I know which score to update. This for loop is in a function that gets called when the game begins, and is passed the "thePlayer" variable which holds the player object to which it belongs to. However, there seems to be a disconnect here. Example: If player 1's score is, let's say, 0, and a button is pressed that's worth 50, I do either of these things, which have no effect on the object property for the player's score:
("this" is the button pressed)
var totalScore = 50;
this.myPlayer.score = totalScore;
player[1].score = totalScore;
Ti.API.debug(player[1].score) //this prints out 0, not 50
No idea why I can't change a player's score. I hope this is clear enough.