Be a Supporter!
Fredofrex
Fredofrex
  • Member since: Jun. 19, 2015
  • Offline.
Forum Stats
Member
Level 01
Blank Slate

Hi, I'm developing a Tower Defense game and I'm close to finish it. I put a lot of effort on it but I'm not even a programmer haha. The problem is I need to add one last system to the game and Is the capability to sell the Towers I place on the game.

When you place a tower on the game, the Tower are stored in an Array called "towers" and the Tile where you place it in another array called "nonPlacableTiles" (because you can't put another tower on it). And there are different towers in the game and all of the towers are stored in the same array.

I make a Sell Button who uses the same logic of a Tower, when you click it you can place it in the correct place, the Tower slot (It's a tile with a circle to put the Tower on). And I manage to remove the Tile from the array "nonPlacableTiles" using this.

nonPlacableTiles.splice(nonPlacableTiles.indexOf(currTile), 1);

And now I can put towers above it because it "clears" the Tile when I place the "Sell Tower" above one Tower.

But the question is: ¿How can I remove the tower?. And how can the code do to identify the tower when I place the "Sell Tower" in the Tile. I have the variable "currTower" who stores the last tower you place in the game. But If I delete it like I say it only remove the LAST tower placed. And I have "towers" the Array to store the towers.

Sorry if I could not explain it well enough but I'm from Argentina and I'm doing my best. If you can help me I'll be very greatful.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Help With Arrays? 2017-04-22 00:23:58 Reply

Based on the code you had in your post, this is certainly JavaScript or ActionScript. I am going to assume it's ActionScript, and version 3 of ActionScript (i.e. AS3, not AS2).

Doing it like that: you will need to store the index position of the tower inside of the towers array in your nonPlacableTiles array. So, you could add on to the towers array first and get the new index after adding to it, and then store that alongside the tile information which is added to nonPlacableTiles. Then, since you know the index of the tile being sold, you can look it up and remove the tower first and then remove the tile:

var tileIndex:int = nonPlacableTiles.indexOf(currTile);

towers.splice(nonPlacableTiles[tileIndex].towerIndex, 1);
nonPlacableTiles.splice(tileIndex, 1);

Or you could just store the tower itself and use the indexOf() function:

var tileIndex:int = nonPlacableTiles.indexOf(currTile);
var towerIndex:int = towers.indexOf(nonPlacableTiles[tileIndex].tower);

towers.splice(towerIndex, 1);
nonPlacableTiles.splice(tileIndex, 1);

Without knowing what the rest of your code looks like I can't suggest how you would store the tower information with the tile.

Fredofrex
Fredofrex
  • Member since: Jun. 19, 2015
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Help With Arrays? 2017-04-22 17:43:10 Reply

I'm having trouble doing this. Can I send you the code?

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Help With Arrays? 2017-04-22 19:36:53 Reply

At 4/22/17 05:43 PM, Fredofrex wrote: I'm having trouble doing this. Can I send you the code?

If you're making this game with Adobe Flash or Adobe Animate I won't be able to look at the code because I don't have those installed on any of my computers. If you can post just the code in this thread I can help fix the problems you're having.