You can build a MacaoNet to have paths for your objects to walk on. A MacaoNet is identified by its name (= net type). See getNet(). A MacaoNet is build of MacaoNode objects, which are connected by MacaoConnection objects.
You create a net just by creating a node for this net (See MacaoNode()). When creating a node you have to provide the net type, which is the name of the net. The new node will register itself at the net. If there is no net of this type, the net will be created. You connect two nodes by calling its method connectTo(). A connection is always a one-way connection. So if you connect node A to B, the objects, that are using the net, can only walk from A to B but not from B to A. To build both connections, from A to B and from B to A, you have to set the parameter alsoBack to true. This will cause the method to create two connections.
When you have built your net by nodes and connections, you have to bind your objects to the net, so they can walk along the net. You call the method bindToNet() to bind an object to a net or remove the binding. An object can only be bind to one net at a time. Last you use the method setAppoachClick() to activate the ability of the object to walk to the position, where the user clicks. When the object is bound to a net, it will use the net and will walk to that node of the net, which is nearest to the click position.
The following example builds a net named "Road" which is compound of the four nodes A, B, C and D. A builds the hub node, to that the others are connected as spokes. The car is placed on the position of the hub. The images are added to the car as a bunch for standing, because there are no extra images for walking. The car is bound to the net and it is activated to walk to the users mouse click.
<html> <head> <script> var basePath = "../../" </script> <link rel=stylesheet type="text/css" href="../core/macao.css"> </head> <body> <script language="JavaScript" src="../../core/storageManager.js" type="text/javascript"></script> <script language="JavaScript" src="../../core/kernel.js" type="text/javascript"></script> <script language="JavaScript" src="../../core/dynamic.js" type="text/javascript"></script> <script language="JavaScript"> // create the nodes var nodeA = new MacaoNode("Road", 400, 250) var nodeB = new MacaoNode("Road", 200, 450) var nodeC = new MacaoNode("Road", 600, 450) var nodeD = new MacaoNode("Road", 400, 50) // link the nodes nodeA.connectTo(nodeB, true) nodeA.connectTo(nodeC, true) nodeA.connectTo(nodeD, true) // build the car var car = new MacaoObject("Car", "Car", 400, 250) car.addLookBunch( car.BUNCH_TYPE_STAND, "optional/cars/red/carRed", 3, ".gif", 20, 20 ) car.bindToNet("Road") car.setApproachClick(true) </script> </body> </html>
For nets that are representing a room for example, you will build the net "manual" like showed above. For road maps, there is the package core/road.js. You can use this package to build large road maps. There you can use the Road Editor to build a large net, just by using the mouse. This functionality is using MacaoNet, MacaoNode and MacaoConnection to build a net with the name (= net type) "Road". You can use this net like a net you build manually.
Method Summary | |
---|---|
void | addNode(MacaoNode node)
Call this method to add a node to the net. |
void | clearAllNodes()
Call this method to remove all nodes from the net. |
MacaoNode | getNearestNode(integer xPos, integer yPos)
Call this method to get the node of the net, which is next to the specified position. |
String | getNetType()
Call this method to get the name of the net. |
MacaoNode | getNodeByIndex(integer index)
Call this method to get a node of the net by its index. |
Array | getNodes()
Call this method to get an array containing all the nodes of the net. |
MacaoNode | getRandomNode()
Call this method to get a random node of the net. |
Method Details |
---|
Call this method to add a node to the net.
See also MacaoNet.
Call this method to remove all nodes from the net.
Call this method to get the node of the net, which is next to the specified position.
Call this method to get the name of the net.
Call this method to get a node of the net by its index.
See also MacaoNode.getIndex().
Call this method to get an array containing all the nodes of the net.
Call this method to get a random node of the net.
This method is used internally to calculate targets for the method MacaoObject.wanderAround().