As:Main
This is a RTS movement style, and an example of the final product is here
*Note, the rotation code and code for making the box was done in other threads, but I think I explained them well enough, and I liked the way they worked. Thanks to Denvish and notliam for makin the codes. Related links and the bottem of the page.
RTS (real time strategy) Movement
First, you need to add these actions into the frame
onLoad = function () {
// only done once
var1 = 0;
// makes the variable set to o
_root.createEmptyMovieClip("box", 1);
// creates and empty moveclip called box with a depth of 1
box.lineStyle(2, 0x0099FF, 100);
// makes box's properties (line syle, color, alpha)
};
// closes load
onMouseDown = function () {
// when the mouse is down
box.moveTo(_xmouse, _ymouse);
// box goes to the mouse
var1 = 1;
// makes the varialbe equal to 1
curX = _xmouse;
// makes varialbe at mouse
curY = _ymouse;
// same but with y
};
// closes function
onMouseUp = function () {
// if the mouse is not down
var1 = 0;
// makes the varialbe equal to 0
};
// closes function
onEnterFrame = function () {
// checks every frame
if (Key.isDown(65)) {
// if A is pressed
_root.mse._x = _xmouse;
// makes the MC mse goto the x of the mouse
_root.mse._y = _ymouse;
// same but wtih y
}
duplicateMovieClip("box", "box2", 2);
// makes a movelcip called box2, with a depth of 2
box2.lineStyle(2, 0x000000, 100);
// boxes properties (see above)
if (var1 == 1) {
// if the variable var1 is equal to 1
box2.beginFill(0x000000, 25);
// makes and sets fill properties (color, alpha)
box2.lineTo(_xmouse, curY);
// makes a line to the x mouse and the varialbe set when the mouse was down
box2.lineTo(_xmouse, _ymouse);
// same but with x mouse and y mouse
box2.lineTo(curX, _ymouse);
// same with the other variable made
box2.lineTo(curX, curY);
// same with both of the varialbes made
}
// closes if statement
};
// closes enter frame
Now make a character, and make it into a movieclip, Make 2 frames inside of it. On the first frame make the character unselected, and on the second make the character selected. Then add these actions into it.
onClipEvent (load) {
//when the movie loads
gotoAndStop(1);
//stops the MC on frame 1
spd = 4;
//sets a variable called speed to 4
follow = false;
//sets a variable called follow to false
}
onClipEvent (enterFrame) {
//checks every frame
if (Key.isDown(1) && !this.hitTest(_root.box)) {
// if the mouse is down and it is not touching a MC called box
select = false;
//makes the variable called select false
gotoAndStop(1);
// makes the MC stop on frame 1
}
//closes the if statement
if (_root.box2.hitTest(this)) {
//if box2 touches this
select = true;
//makes select true
gotoAndStop(2);
// makes the MC stop on frame 2
}
// closes the if statement
if (select == false) {
// if select is false
follow = false;
//follow is false
}
//closes the if statmemt
if (select == true && Key.isDown(83)) {
// if select is true and S is pressed
follow = true;
// makes follow true
}
if (follow == true) {
// if the variable follow is true
Xdiff = _parent.mse._x-_x;
// makes the variable Xdiff the x distance between the MC mse
Ydiff = _parent.mse._y-_y;
// same but with y
radAngle = Math.atan2(Ydiff, Xdiff);
// makes a varialbe called radAngle, and uses the variables created
_rotation = int((radAngle*360/(2*Math.PI))+90);
// rotates towards the character
updateAfterEvent();
// updates after doing so
if (this.hitTest(_parent.mse)) {
// hitTest actions here
} else {
// otherwise
if (_rotation>180) {
// if the MC's rotation is greater than 180
_y += (spd*Math.cos(Math.PI/180*_rotation));
// moves by Y to the character
_x -= (spd*Math.sin(Math.PI/180*_rotation));
// same but with x
} else {
// otherwise
_y -= (spd*Math.cos(Math.PI/180*_rotation));
//see above
_x += (spd*Math.sin(Math.PI/180*_rotation));
//see above
}
//closes else
}
// closes if
}
// closes if
}
// closes enterFrame
Now make a cursor, and give it the instance name of mse. Give it these actions
onClipEvent (enterFrame) {
//checks every frame
_rotation += 10;
// makes it rotate clockwise by 10
}
// ends enterFrame
Thats all there is to it. The players are copy and pastable, so make as many as you want. They require no instance name.
Related Links
API- notlaim
Basic AI-- Dancing Thunder (Code posted by Denvish right afterwards)
This is my second As: Main topic, and I hoped I helped.