Be a Supporter!

As: Rts Movement

  • 3,420 Views
  • 21 Replies
New Topic Respond to this Topic
Pyromaniac
Pyromaniac
  • Member since: Jan. 14, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
As: Rts Movement Apr. 6th, 2006 @ 07:18 PM Reply

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.

Blaze
Blaze
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to As: Rts Movement Apr. 6th, 2006 @ 07:22 PM Reply

quite nice, actually.

I like the example, but im not in the mood for ASing right now, so, can you tell me, what are the movement keys? :)

Tantric
Tantric
  • Member since: Oct. 29, 2005
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to As: Rts Movement Apr. 6th, 2006 @ 07:30 PM Reply

Your example doesn't work. -_-

Pyromaniac
Pyromaniac
  • Member since: Jan. 14, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to As: Rts Movement Apr. 6th, 2006 @ 07:32 PM Reply

Sorry, I forgot the controls.

Controls
A---Moves cursor to loaction of mouse when pressed
S--Makes the character(s) selected move to the cursor
Click+Drag--Select characters
Click (on character)--- Select only that character

sinz3ro
sinz3ro
  • Member since: Aug. 2, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to As: Rts Movement Apr. 8th, 2006 @ 02:36 AM Reply

wow that is cool as!


// Harvest++ coming soon, more modes, more stuff, customizable character, achievements and more!

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Rts Movement Apr. 8th, 2006 @ 02:36 AM Reply

At 4/6/06 07:32 PM, pyro111 wrote: Controls
S--Makes the character(s) selected move to the cursor

This doesn't work for me in the sample you posted.
However, the code does work, here is a working example


- - Flash - Music - Images - -

BBS Signature
SpeckOfSand
SpeckOfSand
  • Member since: Sep. 10, 2003
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to As: Rts Movement Apr. 8th, 2006 @ 02:37 AM Reply

pyrod do you have msn????????

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Rts Movement Apr. 8th, 2006 @ 02:39 AM Reply

At 4/8/06 02:36 AM, Denvish wrote:
At 4/6/06 07:32 PM, pyro111 wrote: Controls
S--Makes the character(s) selected move to the cursor
This doesn't work for me in the sample you posted.
However, the code does work, here is a working example

Blah. There's a problem with selecting units when the swf is in a browser. It doesn't work uploaded to either imageshack or my site. I'm trying to work out where the problem is


- - Flash - Music - Images - -

BBS Signature
SpeckOfSand
SpeckOfSand
  • Member since: Sep. 10, 2003
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to As: Rts Movement Apr. 8th, 2006 @ 02:41 AM Reply

At 4/8/06 02:39 AM, Denvish wrote:
At 4/8/06 02:36 AM, Denvish wrote:
At 4/6/06 07:32 PM, pyro111 wrote: Controls
S--Makes the character(s) selected move to the cursor
This doesn't work for me in the sample you posted.
However, the code does work, here is a working example
Blah. There's a problem with selecting units when the swf is in a browser. It doesn't work uploaded to either imageshack or my site. I'm trying to work out where the problem is

it worked for me ok i had no problem i selected unit and moved them
im hoping that he can now show me how to make a character walk to the spot the mouse is clicked

BlackmarketKraig
BlackmarketKraig
  • Member since: Dec. 8, 2004
  • Offline.
Forum Stats
Member
Level 32
Artist
Response to As: Rts Movement Apr. 8th, 2006 @ 02:43 AM Reply

At 4/8/06 02:39 AM, Denvish wrote:
Blah. There's a problem with selecting units when the swf is in a browser. It doesn't work uploaded to either imageshack or my site. I'm trying to work out where the problem is

What's the problem? It seems to work for me...


This sig is 100% effective protection from all hexes, curses, evil spirits and bad karma. Guaranteed.

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Rts Movement Apr. 8th, 2006 @ 02:44 AM Reply

At 4/8/06 02:39 AM, Denvish wrote: Blah. There's a problem with selecting units when the swf is in a browser.

More specifically, in Firefox. It works fine in IE.


- - Flash - Music - Images - -

BBS Signature
BlackmarketKraig
BlackmarketKraig
  • Member since: Dec. 8, 2004
  • Offline.
Forum Stats
Member
Level 32
Artist
Response to As: Rts Movement Apr. 8th, 2006 @ 02:44 AM Reply

At 4/8/06 02:44 AM, Denvish wrote:
At 4/8/06 02:39 AM, Denvish wrote: Blah. There's a problem with selecting units when the swf is in a browser.
More specifically, in Firefox. It works fine in IE.

and Opera. =)


This sig is 100% effective protection from all hexes, curses, evil spirits and bad karma. Guaranteed.

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Rts Movement Apr. 8th, 2006 @ 02:45 AM Reply

At 4/8/06 02:41 AM, SpeckOfSand wrote: im hoping that he can now show me how to make a character walk to the spot the mouse is clicked

Check the links at the bottom of his post
http://www.newground../topic.php?id=296768


- - Flash - Music - Images - -

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Rts Movement Apr. 8th, 2006 @ 03:22 AM Reply

At 4/8/06 02:44 AM, Denvish wrote:
At 4/8/06 02:39 AM, Denvish wrote: Blah. There's a problem with selecting units when the swf is in a browser.
More specifically, in Firefox. It works fine in IE.

Hrm. Dunno why, but Firefox doesn't seem to like duplicating a non-closed API-drawn MC

Replace the first block of actions (the ones on the frame) with this:

onLoad = function () {
var1 = 0;
};
onMouseDown = function () {
var1 = 1;
curX = _root._xmouse;
curY = _root._ymouse;
};
onMouseUp = function () {
var1 = 0;
};
onEnterFrame = function () {
if (Key.isDown(65)) {
_root.mse._x = _xmouse;
_root.mse._y = _ymouse;
}
createEmptyMovieClip("box2", 2000);
box2.lineStyle(2, 0x000000, 100);
if (var1 == 1) {
box2.lineStyle(2, 0x0099FF, 100);
box2.beginFill(0x000000, 25);
box2.moveTo(curX, curY);
box2.lineTo(_xmouse, curY);
box2.lineTo(_xmouse, _ymouse);
box2.lineTo(curX, _ymouse);
box2.lineTo(curX, curY);
box2.endFill();
}
};

That appears to work


- - Flash - Music - Images - -

BBS Signature
Pyromaniac
Pyromaniac
  • Member since: Jan. 14, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to As: Rts Movement Apr. 8th, 2006 @ 09:15 AM Reply

Denvish, thanks for making it work in other browers. I use IE so I didnt have a problem.

Chaz
Chaz
  • Member since: Sep. 27, 2005
  • Offline.
Forum Stats
Member
Level 23
Game Developer
Response to As: Rts Movement Apr. 8th, 2006 @ 09:59 AM Reply

dude just what i needed, you popped up at the right time =D


BBS Signature
Lopas1
Lopas1
  • Member since: Jun. 6, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to As: Rts Movement Apr. 9th, 2006 @ 05:04 AM Reply

This is Awesome!!!! Yay More AS to learn!


BBS Signature
liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to As: Rts Movement Apr. 9th, 2006 @ 05:36 AM Reply

Denvish, the first worked for me in Firefox (or more specifically, "Bon Echo" [FF2 alpha]).


Sup, bitches :)

BBS Signature
iv00w
iv00w
  • Member since: Apr. 24, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As: Rts Movement May. 4th, 2007 @ 03:32 PM Reply

It works fine, but if you select multiple units, eventually they'll walk on top of eachother, and after that, they're stuck together as if they're one unit.

iv00w
iv00w
  • Member since: Apr. 24, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As: Rts Movement May. 5th, 2007 @ 06:26 AM Reply

nvm my last post :P

Theomylad
Theomylad
  • Member since: Jul. 14, 2007
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to As: Rts Movement Sep. 5th, 2008 @ 09:32 PM Reply

*Drools*


Zelda is like high-fructose-corn-syrup-based cookies with extra MSG preserverative, it never goes bad!

DashDingo
DashDingo
  • Member since: Jun. 25, 2004
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to As: Rts Movement Sep. 5th, 2008 @ 10:15 PM Reply

For the box being dragged and stuff, maybe if you thrown in a updateAfterEvent(); somewhere on the mouse commands, it'll draw more smoothly.

I'm just guessing, though.


BBS Signature