Walking by mouse click

We modify the World class to listen on mouse click event. When the mouse clicks the world, we get the isometric tile position from the mouse screen coordinate. The map is the reference to the IsometricMapData class which contains the A* pathfinding class. We set up the starting and ending points for the pathfinding class to find a path solution. Then we pass the path to the WalkingAvatar class to follow. At last we need to update the position in the room variables.

private function onMapClicked(e:MouseEvent):void {
var newIsoPosition:Point = map.s2i(stage.mouseX,stage.mouseY);
if (!map.isWalkable(newIsoPosition.x,newIsoPosition.y)){
trace("can't walk to there");
return;
}
/* find the path by A* pathfinding class */
map.astarMap.setEndPoints(myAvatar.isoPosition.x,_myAvatar. isoPosition.y,newIsoPosition.x,newIsoPosition.y);
var path:Array = map.astarMap.solve();
myAvatar.walkPath = path;
myAvatar.startWalkByPath();
/* update the userVariables*/
var params:Object = new Object();
params.isoPosX = newIsoPosition.x;
params.isoPosY = newIsoPosition.y;
params.firstTime = false;
_sfs.setUserVariables(params);
}

The updated position in room variables is useful for newly joined players. Existing players in the same room do not need it because they will get the updated position from broadcasted message.

The position set to the final destination in user variables is updated before start to move the avatar. If someone joins the room when you are walking, the new players will see you standing at the destination position instead of walking towards there. It causes a small issue of being asynchronous but it is fine as long as the positioning is not critical.

We can instead do some tricks to make newly joined players see the walking process. We need to put the destination point and the current position on user variables. The user variables need updates every time the walking avatar walks into new position. In this case, the newly joined room players can play the avatar walking by the current position and destination from user variables.

I used the former movement implementation in practical production and it works very well. It is because most of the time we do not need very precise position animation unless the game play requires it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.30.118