PDA

View Full Version : Help on flash game


MikeRoch
03-12-2008, 02:27 PM
Right now im working on a flash maze tower defense game, and right now i have a working turret, enemies and bullets. This is the code so far


base_range = 300;
can_be_placed = false;
placed = false;
firing = false;
attachMovie("path", "path", _root.getNextHighestDepth());
attachMovie("cant_build", "cant_build", _root.getNextHighestDepth());
attachMovie("range", "range", _root.getNextHighestDepth(), {_width:base_range, _height:base_range});
attachMovie("base", "base", _root.getNextHighestDepth());
waypoint_x = new Array(40, 140, 140, 290, 290, 45, 45, 400, 400);
waypoint_y = new Array(220, 220, 120, 120, 320, 320, 430, 430, -30);
delay = 25;
new_monster = 0;
monsters_placed = 0;
onEnterFrame = function () {
if (monsters_placed<25) {
new_monster++;
}
if (new_monster == delay) {
monsters_placed++;
new_monster = 0;
min = attachMovie("minion", "minion"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:40, _y:-20});
min.point_to_reach = 0;
min.speed = 1;
min.onEnterFrame = function() {
dist_x = waypoint_x[this.point_to_reach]-this._x;
dist_y = waypoint_y[this.point_to_reach]-this._y;
if ((Math.abs(dist_x)+Math.abs(dist_y))<1) {
this.point_to_reach++;
}
angle = Math.atan2(dist_y, dist_x);
this._x = this._x+this.speed*Math.cos(angle);
this._y = this._y+this.speed*Math.sin(angle);
this._rotation = angle/Math.PI*180-90;
if (bullet.hitTest(this._x, this._y, true)) {
firing = false;
bullet.removeMovieClip();
this.removeMovieClip();
}
if (placed) {
distance_from_turret_x = base._x-this._x;
distance_from_turret_y = base._y-this._y;
if ((Math.sqrt(distance_from_turret_x*distance_from_t urret_x+distance_from_turret_y*distance_from_turre t_y)<base_range/2) and (!firing)) {
firing = true;
bullet = attachMovie("bullet", "bullet"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:base._x, _y:base._y});
bullet.dir = Math.atan2(distance_from_turret_y, distance_from_turret_x);
bullet.onEnterFrame = function() {
this._x -= 3*Math.cos(bullet.dir);
this._y -= 3*Math.sin(bullet.dir);
if ((this._x<0) or (this._y<0) or (this._y>400) or (this._x>500)) {
firing = false;
this.removeMovieClip();
}
};
}
}
};
}
};
base.onEnterFrame = function() {
if (!placed) {
this._x = _root._xmouse;
this._y = _root._ymouse;
_root.range._alpha = 100;
can_be_placed = true;
if (_root.cant_build.hitTest(this._x-this._width/2, this._y-this._height/2, true) or _root.cant_build.hitTest(this._x+this._width/2, this._y+this._height/2, true) or _root.cant_build.hitTest(this._x+this._width/2, this._y-this._height/2, true) or _root.cant_build.hitTest(this._x-this._width/2, this._y+this._height/2, true)) {
_root.range._alpha = 0;
can_be_placed = false;
}
}
};
range.onEnterFrame = function() {
if (!placed) {
this._x = _root._xmouse;
this._y = _root._ymouse;
}
};
onMouseDown = function () {
if (can_be_placed) {
placed = true;
}
};

My question is if anyone has any ideas on how to help make this do rounds with accumulating money per kill, along with buildable turrets. Also any tut's for this type of thing would help.

Continuity-B
03-12-2008, 02:36 PM
I have no idea but it's best to put your AS in AS tags like this (using the big red F button when posting).


base_range = 300;
can_be_placed = false;
placed = false;
firing = false;
attachMovie("path", "path", _root.getNextHighestDepth());
attachMovie("cant_build", "cant_build", _root.getNextHighestDepth());
attachMovie("range", "range", _root.getNextHighestDepth(), {_width:base_range, _height:base_range});
attachMovie("base", "base", _root.getNextHighestDepth());
waypoint_x = new Array(40, 140, 140, 290, 290, 45, 45, 400, 400);
waypoint_y = new Array(220, 220, 120, 120, 320, 320, 430, 430, -30);
delay = 25;
new_monster = 0;
monsters_placed = 0;
onEnterFrame = function () {
if (monsters_placed<25) {
new_monster++;
}
if (new_monster == delay) {
monsters_placed++;
new_monster = 0;
min = attachMovie("minion", "minion"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:40, _y:-20});
min.point_to_reach = 0;
min.speed = 1;
min.onEnterFrame = function() {
dist_x = waypoint_x[this.point_to_reach]-this._x;
dist_y = waypoint_y[this.point_to_reach]-this._y;
if ((Math.abs(dist_x)+Math.abs(dist_y))<1) {
this.point_to_reach++;
}
angle = Math.atan2(dist_y, dist_x);
this._x = this._x+this.speed*Math.cos(angle);
this._y = this._y+this.speed*Math.sin(angle);
this._rotation = angle/Math.PI*180-90;
if (bullet.hitTest(this._x, this._y, true)) {
firing = false;
bullet.removeMovieClip();
this.removeMovieClip();
}
if (placed) {
distance_from_turret_x = base._x-this._x;
distance_from_turret_y = base._y-this._y;
if ((Math.sqrt(distance_from_turret_x*distance_from_t urret_x+distance_from_turret_y*distance_from_turre t_y)<base_range/2) and (!firing)) {
firing = true;
bullet = attachMovie("bullet", "bullet"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:base._x, _y:base._y});
bullet.dir = Math.atan2(distance_from_turret_y, distance_from_turret_x);
bullet.onEnterFrame = function() {
this._x -= 3*Math.cos(bullet.dir);
this._y -= 3*Math.sin(bullet.dir);
if ((this._x<0) or (this._y<0) or (this._y>400) or (this._x>500)) {
firing = false;
this.removeMovieClip();
}
};
}
}
};
}
};
base.onEnterFrame = function() {
if (!placed) {
this._x = _root._xmouse;
this._y = _root._ymouse;
_root.range._alpha = 100;
can_be_placed = true;
if (_root.cant_build.hitTest(this._x-this._width/2, this._y-this._height/2, true) or _root.cant_build.hitTest(this._x+this._width/2, this._y+this._height/2, true) or _root.cant_build.hitTest(this._x+this._width/2, this._y-this._height/2, true) or _root.cant_build.hitTest(this._x-this._width/2, this._y+this._height/2, true)) {
_root.range._alpha = 0;
can_be_placed = false;
}
}
};
range.onEnterFrame = function() {
if (!placed) {
this._x = _root._xmouse;
this._y = _root._ymouse;
}
};
onMouseDown = function () {
if (can_be_placed) {
placed = true;
}
};

MikeRoch
03-19-2008, 01:29 PM
Im trying to make a running total based on how many enemies i kill, and also to be able to create more turrets and place them on the screen at a certain price
i have this in my frame stop();

base_range = 300;
can_be_placed = false;
placed = false;
firing = false;
attachMovie("path", "path", _root.getNextHighestDepth());
attachMovie("cant_build", "cant_build", _root.getNextHighestDepth());
attachMovie("range", "range", _root.getNextHighestDepth(), {_width:base_range, _height:base_range});
attachMovie("base", "base", _root.getNextHighestDepth());
waypoint_x = new Array(40, 140, 140, 290, 290, 45, 45, 400, 400);
waypoint_y = new Array(220, 220, 120, 120, 320, 320, 430, 430, -30);
delay = 25;
new_monster = 0;
monsters_placed = 0;
onEnterFrame = function () {
if (monsters_placed<25) {
new_monster++;
}
if (new_monster == delay) {
monsters_placed++;
new_monster = 0;
min = attachMovie("minion", "minion"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:40, _y:-20});
min.point_to_reach = 0;
min.speed = 2.5;
min.onEnterFrame = function() {
dist_x = waypoint_x[this.point_to_reach]-this._x;
dist_y = waypoint_y[this.point_to_reach]-this._y;
if ((Math.abs(dist_x)+Math.abs(dist_y))<1) {
this.point_to_reach++;
}


angle = Math.atan2(dist_y, dist_x);
this._x = this._x+this.speed*Math.cos(angle);
this._y = this._y+this.speed*Math.sin(angle);
this._rotation = angle/Math.PI*180-90;
if (bullet.hitTest(this._x, this._y, true)) {
firing = false;
bullet.removeMovieClip();
this.removeMovieClip();
}
if (placed) {
distance_from_turret_x = base._x-this._x;
distance_from_turret_y = base._y-this._y;
if ((Math.sqrt(distance_from_turret_x*distance_from_t urret_x+distance_from_turret_y*distance_from_turre t_y)<base_range/2) and (!firing)) {
firing = true;
bullet = attachMovie("bullet", "bullet"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:base._x, _y:base._y});
bullet.dir = Math.atan2(distance_from_turret_y, distance_from_turret_x);
bullet.onEnterFrame = function() {
this._x -= 10*Math.cos(bullet.dir);
this._y -= 10*Math.sin(bullet.dir);
if ((this._x<0) or (this._y<0) or (this._y>400) or (this._x>500)) {
firing = false;
this.removeMovieClip();
}
};
}
}
}
}
}


base.onEnterFrame = function() {
if (!placed) {
this._x = _root._xmouse;
this._y = _root._ymouse;
_root.range._alpha = 100;
can_be_placed = true;
if (_root.cant_build.hitTest(this._x-this._width/2, this._y-this._height/2, true) or _root.cant_build.hitTest(this._x+this._width/2, this._y+this._height/2, true) or _root.cant_build.hitTest(this._x+this._width/2, this._y-this._height/2, true) or _root.cant_build.hitTest(this._x-this._width/2, this._y+this._height/2, true)) {
_root.range._alpha = 0;
can_be_placed = false;
}
}
};
range.onEnterFrame = function() {
if (!placed) {
this._x = _root._xmouse;
this._y = _root._ymouse;
}
};
onMouseDown = function () {
if (can_be_placed) {
placed = true;
}
};

Any help would be great, if you have any suggestions on how to make it into levels that would help

rrh
03-19-2008, 04:20 PM
Well, you're using arrays and adding bullets and monsters, so you've got the necessary principles.
To add a new base, do it like you do with a bullet or monster, and then push it onto an array of bases.

Levels in TD games are really waves. More monsters come, only tougher. Right now you're just removing the minion when it gets shot. You'll need to give it hit points, which are reduced by the strength of the bullet, and then remove it if that reaches zero.

Also, I see you're only keeping track of one bullet right now. You'll want to keep track of at least one bullet per turret. That's where the arrays come in. Once you have an array of bases, you can go:
for (var count:Number=0;count<bases.length;count++)
if (bases[count].placed) {
distance_from_turret_x = bases[count]._x-this._x;
distance_from_turret_y = bases[count]._y-this._y;
See how I made "placed" relative to that base, instead of global? You'll need to do the same with a bunch of relevant variables.

MikeRoch
03-31-2008, 03:10 PM
:mad::rolleyes::confused::)
ty