Forum Topic: As: Rts Movement

(2,370 views • 21 replies)

This topic is 1 page long.

<< < > >>
None

Pyromaniac

Reply To Post Reply & Quote

Posted at: 4/6/06 07:18 PM

Pyromaniac EVIL LEVEL 18

Sign-Up: 01/14/05

Posts: 2,968

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.


None

Blaze

Reply To Post Reply & Quote

Posted at: 4/6/06 07:22 PM

Blaze LIGHT LEVEL 22

Sign-Up: 08/04/05

Posts: 6,957

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? :)


None

Tantric

Reply To Post Reply & Quote

Posted at: 4/6/06 07:30 PM

Tantric LIGHT LEVEL 10

Sign-Up: 10/29/05

Posts: 473

Your example doesn't work. -_-


None

Pyromaniac

Reply To Post Reply & Quote

Posted at: 4/6/06 07:32 PM

Pyromaniac EVIL LEVEL 18

Sign-Up: 01/14/05

Posts: 2,968

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


None

sinz3ro

Reply To Post Reply & Quote

Posted at: 4/8/06 02:36 AM

sinz3ro NEUTRAL LEVEL 07

Sign-Up: 08/02/05

Posts: 598

wow that is cool as!

// Harvest++ coming sometime later this year, with better graphics, more modes, more pointlessness!

BBS Signature

None

Denvish

Reply To Post Reply & Quote

Posted at: 4/8/06 02:36 AM

Denvish DARK LEVEL 42

Sign-Up: 04/25/03

Posts: 15,933

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 - Report Abuse - -
Not around any more, see last news post.

BBS Signature

None

SpeckOfSand

Reply To Post Reply & Quote

Posted at: 4/8/06 02:37 AM

SpeckOfSand NEUTRAL LEVEL 02

Sign-Up: 09/10/03

Posts: 117

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


None

Denvish

Reply To Post Reply & Quote

Posted at: 4/8/06 02:39 AM

Denvish DARK LEVEL 42

Sign-Up: 04/25/03

Posts: 15,933

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 - Report Abuse - -
Not around any more, see last news post.

BBS Signature

None

SpeckOfSand

Reply To Post Reply & Quote

Posted at: 4/8/06 02:41 AM

SpeckOfSand NEUTRAL LEVEL 02

Sign-Up: 09/10/03

Posts: 117

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


None

BlackmarketKraig

Reply To Post Reply & Quote

Posted at: 4/8/06 02:43 AM

BlackmarketKraig NEUTRAL LEVEL 29

Sign-Up: 12/08/04

Posts: 6,393

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...

: [ fl ] : + : [ art ] : + : [ dA ] : + : [ ms ] : + : [ <3 ] :
. l . o . v . e .

BBS Signature

None

Denvish

Reply To Post Reply & Quote

Posted at: 4/8/06 02:44 AM

Denvish DARK LEVEL 42

Sign-Up: 04/25/03

Posts: 15,933

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 - Report Abuse - -
Not around any more, see last news post.

BBS Signature

None

BlackmarketKraig

Reply To Post Reply & Quote

Posted at: 4/8/06 02:44 AM

BlackmarketKraig NEUTRAL LEVEL 29

Sign-Up: 12/08/04

Posts: 6,393

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. =)

: [ fl ] : + : [ art ] : + : [ dA ] : + : [ ms ] : + : [ <3 ] :
. l . o . v . e .

BBS Signature

None

Denvish

Reply To Post Reply & Quote

Posted at: 4/8/06 02:45 AM

Denvish DARK LEVEL 42

Sign-Up: 04/25/03

Posts: 15,933

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 - Report Abuse - -
Not around any more, see last news post.

BBS Signature

None

Denvish

Reply To Post Reply & Quote

Posted at: 4/8/06 03:22 AM

Denvish DARK LEVEL 42

Sign-Up: 04/25/03

Posts: 15,933

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 - Report Abuse - -
Not around any more, see last news post.

BBS Signature

None

Pyromaniac

Reply To Post Reply & Quote

Posted at: 4/8/06 09:15 AM

Pyromaniac EVIL LEVEL 18

Sign-Up: 01/14/05

Posts: 2,968

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


None

Chaz

Reply To Post Reply & Quote

Posted at: 4/8/06 09:59 AM

Chaz LIGHT LEVEL 21

Sign-Up: 09/27/05

Posts: 4,103

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

Click sig for "The Cell 2" Production Blog.

BBS Signature

None

Lopas1

Reply To Post Reply & Quote

Posted at: 4/9/06 05:04 AM

Lopas1 LIGHT LEVEL 22

Sign-Up: 06/06/05

Posts: 2,824

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

Add my Xbox Live Gamertag: Macintawtsh
Mah new ride! :D

BBS Signature

None

liaaaam

Reply To Post Reply & Quote

Posted at: 4/9/06 05:36 AM

liaaaam NEUTRAL LEVEL 21

Sign-Up: 12/11/04

Posts: 12,766

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


None

iv00w

Reply To Post Reply & Quote

Posted at: 5/4/07 03:32 PM

iv00w EVIL LEVEL 08

Sign-Up: 04/24/05

Posts: 375

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.


None

iv00w

Reply To Post Reply & Quote

Posted at: 5/5/07 06:26 AM

iv00w EVIL LEVEL 08

Sign-Up: 04/24/05

Posts: 375

nvm my last post :P


None

Theomylad

Reply To Post Reply & Quote

Posted at: 9/5/08 09:32 PM

Theomylad LIGHT LEVEL 11

Sign-Up: 07/14/07

Posts: 92

*Drools*

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


None

DashDingo

Reply To Post Reply & Quote

Posted at: 9/5/08 10:15 PM

DashDingo EVIL LEVEL 14

Sign-Up: 06/25/04

Posts: 5,100

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

All times are Eastern Standard Time (GMT -5) | Current Time: 01:18 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!