Forums → Programming Forum → Help for an important question
4 | 2647 |
Hello all of AG. I'm trying to make a Tower Defense game, and what tower defense game is complete if you can't click on the towers?
So there is my problem. I'm using AS2, here is the code for my tower:
menu.tower1.onPress = function() {
_root.towerNum = 0;
if (!_root.placeTower && gold >= towerStats[towerNum][4]) {
_root.placeTower = true;
_root.selectedTower = "arrowTower";
_root.towerNum = 0;
_root.attachMovie("rangeCircle","rangeCircle",_root.getNextHighestDepth());
_root.rangeCircle._xscale = _root.rangeCircle._yscale=_root.towerStats[_root.towerNum][0]*2;
_root.attachMovie(selectedTower,"towerHold",_root.getNextHighestDepth());
towerHold._x = (Math.floor(_xmouse/32)*32)+16;
towerHold._y = (Math.floor(_ymouse/32)*32)+16;
rangeCircle._x = towerHold._x;
rangeCircle._y = towerHold._y;
}
}
That is for the tower icon in the menu which is already there.
Then i have this:
onMouseDown = function () {
if (placeTower && _ymouse<416 && !towerColl.hitTest(new Point(towerHold._x, towerHold._y), 255, path, new Point(0, 0), 255)) {
numTowers++;
name = selectedTower+numTowers;
this.attachMovie(selectedTower,name,this.getNextHighestDepth());
this[name]._x = towerHold._x;
this[name]._y = towerHold._y;
this[name].num = numTowers;
this[name].range = towerStats[towerNum][0];
this[name].reload = towerStats[towerNum][1];
this[name].damage = towerStats[towerNum][2];
this[name].speed = towerStats[towerNum][3];
this[name].cost = towerStats[towerNum][4];
this[name].fires = towerStats[towerNum][5];
this[name].upgrade = towerStats[towerNum][6];
this[name].reloadHold = this[name].reload;
this[name].canFire = false;
gold -= this[name].cost;
goldBox.gold.text = gold;
towers.push(this[name]);
towerIDs.push(numTowers);
placeTower = false;
towerHold.removeMovieClip();
rangeCircle.removeMovieClip();
var mat:Matrix = new Matrix();
mat.tx = this[name]._x;
mat.ty = this[name]._y;
path.draw(this[name],mat);
towerID = this[name];
}
}
It works exept i would like to click on a tower, and be able to upgrade separate towers. If i trace towerID outside of this function, it traces whichever tower was last.
The code for that is here:
this[name].onPress = function() {
trace(towerID);
}
}
This should make it so when i click on any tower, it traces the towerID. The problem is when i click on a tower, it traces whatever the last towers name was, because the towerID variable is newly asigned every time you place a new tower.
That didn't work, so i added this:
this.onEnterFrame = function() {
for (i=0; i<towers.length; i++) {
tower = towers[i];
tower.onPress = function() {
trace("hi" + tower);
}
}
};
What i thought this would do is:
When any tower is pressed, it traces that towers name.
What it actually does is:
When any tower is pressed, it traces the last tower placed name.
So my question for you all is: How can i make it so each time i click on a tower, it traces its own name as oppose to the last tower placed name.
I hope this makes sense, thanks in advance,
~Kcaz
- 4 Replies
trace(_name)? That doesn't do anything because the name variable is in the onMouseDown function. I tried it and the output is just blank.
_name is a property put on every MovieClip that is instantiated. Instead of creating a new variable, just use that one.
Why not programming inside the tower Movie Clip?
When I needed a bullet to take the direction I was aiming and tried this in the main time line, let's say I shoot at upper left and then at upper right, the upper left one would change it's direction when the other was shot
So I programmed inside the bullet and each one takes it's own direction
You must be logged in to post a reply!