Trouble adding Towers to my Tower Defense Game in AS3

Before I explain the details of what I have, I am just going to let you know I am a very inexperienced programmer, and I appreciate anybody's help.

What I have done so far is I have made an array of cells that takes up the whole board, except where the path is. All of the cells are being added to the Simulator class. My cell class just contains a few different event listeners to change the color of the cell when I scroll over them. I have been trying to add a addTurret function in my cell class that looks like this:

private function addTurret(e: MouseEvent): void
{
  var turret: Turret = new Turret();
  addChild(turret);
  turret.x = this.x;
  turret.y = this.y;
}

This is in the constructor of the cell class:

this.addEventListener(MouseEvent.CLICK, addTurret);

The Turret class just makes a simple red circle for now. When I am clicking the cells, the turrets are just showing up in random places (I don't know why), and I want them to appear right on top of the cell I click.

Thank you to anyone that has a solution.

Answers


You are adding the turret to the cell, which I am assuming is a DisplayObject that is added to the board. When you add turret as a child of the cell, it's x,y location is now relative to the cell's x,y location.

So setting the x,y position of the turret to the x,y position of the cell is likely the problem. There is no need as your turret should be at the 0,0 location of your cell.

So just modify your addTurret function as so :

private function addTurret(e: MouseEvent): void
{
  var turret: Turret = new Turret();
  addChild(turret);
}

Need Your Help

Azure storage account backup (tables and blobs)

azure backup windows-azure-storage

I need to periodically backup all blobs and tables in an Azure storage account so that we can restore all that data at a later time if we for any reason corrupt our data.

Mail didn't send when wifi is off and error is not shown

ios wifi mfmailcomposeviewcontroll

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.