RTS Movement Tutorial by Nivram

Beginner-Should have a basic understanding of MMF.

You will learn the following:
Make a Selection Box using a Active System Box (ASB)
Select a unit using the ASB.
Moving a unit after selecting it.

We will be doing an RTS Movement Tutorial. There will be no extensions required, but you should download the .mfa file so you can follow along, or compare my .mfa file to the one you will be building.

rtsmove.mfa (35kb)

This will be Point and Click type RTS example using an active character with "Static Movement". Static means we will not be assigning any kind of movement, and this is the default movement for MMF when you first create an active object.

Open up MMF and click New under File. Rename your Application to RTS Move 2. Save as RTSMove2. I would like to comment that you should save any project you are building often. Set your application window to 800X600 and your Frame to 2000X2000. The reason we are setting the Frame so large is that this will be a scrolling application.

Create an Active Object 32X32 and place the Hot Spot to the center. Rename it Unit 1. Place Unit 1 in the Top Left of the Frame. The color you make it does not matter and do not assign any movement. Create another Active Object 5X5 with the Hot Spot in the center and again the color doesn't mater. Do not assign any movement. Rename this active object to Destination Square. Place the Destination Square out of the Frame. Save your Application.

In the Events Editor and for your first code make a Comment:
RTS Movement Example by "your name".

Since this will be an Application that scrolls, go to the Events Editor and make the event:

Always
(under Storyboard controls, Scrollings, Center Window Position in Frame, Relative to Unit 1, 0,0)
Center display at (0,0) from Unit 1

Save your Application

What RTS would be complete without a Selection Box for selecting units. We will be using an Active System Box (ASB) to accomplish this. This will most likely be the most challenging coding for this tutorial.

Go to your Frame and add a Active System Box and set it outside the Frame. We are going to change some of the default settings for the ASB, so right click it and then Edit.

Change the Fill Color to None.
Change the Border Color 1, 2 to dark grey.
Uncheck Replace end by "..." if too long.
Change Alignment to Top and Left.

Ok, the ASB set up for our purposes.

In the Event Editor under the Always Event, let’s add a Group Events:
ASB Selection
Leave the Active when frames start Checked.

Now add a second Group Event and name it:
Selection
Uncheck Active when frames start.

Make a Comment:

We use an Active System Box for the Selector. The alterable value "Selection" to 0 for off or 1 for on. It is set to 1 by default.

3 Alterable Values are used and renamed:
A= XMouse
B= YMouse
C= Selection

Ok, let's rename our Alterable Values we will be using. Single click your ASB and in the Properties, single click the AZ Icon. Click New, then double click Alterable Value A. Up pops a Rename Box. Type in:

StartX
Click on OK.
Back in the Properties click New and double click Alterable Value B. Name it:
Start Y
Click Ok
Once again in the Properties click New and for Alterable Value C rename it:
Selection
Click OK.
For the Alterable Value Selection, change the number 0 to a
1.
We are done with the changes.

ASB Selector Group Events

In the Events Editor in your ASB Selector Group Events, add this Event: *Note: Compare to one of the Alterable Variables

Selection=0
+ Only one action when event loops
Deactivate Group Selection

Add this event:
*Note: Compare to one of the Alterable Varables

Selection=1
+ Only one action when event loops
Activate Group Selection


Make a comment:

Unit Selection

Add this event:

X Repeat while mouse key is pressed *Note: X donates negate
+ Selection Box is overlapping Unit 1
+ Only one action when event loops
Unit 1:
Flash for 00"-20

These are all the events in the ASB Selector Group Events.

Save your application.

Selector Group Events

We will now be adding events to the Selector Group Events. Your first event will be:

User Clicks with Left Button
Active System Box:
Set Xposition to XMouse
Set Yposition to YMouse
Set StartX to XMouse
Set StartY to YMouse

Your next event will be:

Repeat while left mouse key is pressed
Active System Box: Max(Abs(XMouse-StartX( "Active System Box" )), 1)
*Note: Set dimension Width and Height
Max(Abs(YMouse-StartY( "Active System Box" )), 1)
Min(XMouse, StartX( "Active System Box" ))
*Note: Set X,Y position
Min(YMouse, StartY( "Active System Box" ))

Your next event:

X Repeat while left mouse key is pressed
Active System Box: Set position to -89, 206

This is all the events for right now.

Save your application.

Test your application. If you have followed the tutorial closely and coded everything correctly, and you select Unit 1, it should start to flash.

Ok, now that we have the selector Box working, let's make Unit 1 move after it starts flashing. Also when the unit starts to move we will make it stop flashing so we don't get a headache.

In the ABS Selection Group event where we have the event to start Unit 1 flashing lets add another action:

Unit 1: Set internal Flag 1 on

Your event and actions should look like this:
X Repeat while mouse key is pressed *Note: X donates negate
+ Selection Box is overlapping Unit 1
+ Only one action when event loops
Unit 1:
Flash for .20
Set internal Flag 1 on

Make another Group Event and name it:

Move Unit 1

In the ABS Selection Group add the last event:

Unit 1 Internal Flag 1 is on
Activate Group Move Unit 1

Move Unit 1 Group Events

Now the first event in the Move Unit 1 Group Events will be an “out” for the user if they change their mind about moving Unit 1:

User clicks with right button on Unit 1
Set Global Value A to 0
*Note: Global Value A is used to deactivate Move Unit 1 Group.
Unit 1:
Set internal flag 1 to off

The next event will allow us to place the Destination Square for Unit 1:

User clicks with left button
+ Unit 1 internal flag 1 is on
Set Global Value A to 1
Unit 1:
Set Unit 1 internal flag 1 to off
Destination Square:
Set X position to XMouse
Set Y position to YMouse

The next event will make Unit 1 stop flashing:

Global Value A=1
+ Only one action when event loop
Unit 1:
Reappear

We will add the events to actually move Unit 1 to the Destination Square. Basically what we will be telling Unit 1 to do, if the Desination Square is to the Right of me increase my X position. To the left, decrease my X position. Below, increase my Y position. Up from me, decrease my Y position:

Global Value A=1
+X position of Unit 1 < than X position Destination Square
+ Every 00”-01
Unit 1:
Set X (“Unit 1”) to X (“Unit 1”)+1

Global Value A=1
+X position of Unit 1 > than X position Destination Square
+ Every 00”-01
Unit 1:
Set X (“Unit 1”) to X (“Unit 1”)-1

Global Value A=1
+Y position of Unit 1 < than Y position Destination Square
+ Every 00”-01
Unit 1:
Set Y (“Unit 1”) to Y (“Unit 1”)+1

Global Value A=1
+Y position of Unit 1 > than Y position Destination Square
+ Every 00”-01
Unit 1:
Set Y (“Unit 1”) to Y (“Unit 1”)-1

In the next event, if Unit 1 overlaps the Destination Square we are going to stop Unit 1, set the Destination Square outside the frame, and set Global Value to 0:

Unit 1 is overlapping Destination Square
+ Only one action when event loops
Set Global Value A to 0
Destination Square:
Set position to (-40, 110)

Our very last event will be deactivating the Move Unit 1 Group Events:

Global Value A=0
Deactivate Group “Move Unit 1

Save your Application and then test it.

A Final Note
You can find grass tiles in the library. You can actually make your Selection Box border any color you desire. That way it shows better with the grass quick back drop.

© 2008-2012 Marvin Hull