I want to write a blog, but I find that I haven't done anything recently. I'm just busy with my work.
So I used the post I posted here to fill it out.
I had a lot of ideas about playing against Cao Zhuan, or the network of playing games, a long time ago, but I didn't have the energy and ability to turn it into reality.
Let me talk about my ideas about the network of war games,
1. Like other online games, each player can choose the character he or she prefers to play the game. These characters have different attributes, such as the cavalry, infantry, bows, and military divisions in Cao's biography, sports car, etc.
2. the player's initial attribute varies depending on the type of personnel selected by the player, but the data is always equal.
3. Players increase their levels based on tasks or upgrades, and then obtain the corresponding attribute points and assign them to the corresponding attributes for corresponding capacity growth, however, when the number of attribute points is allocated, the growth of each attribute varies depending on different types of troops. For example, cavalry attacks are allocated a little bit, growth + 4, infantry attacks are allocated a little, growth + 2, and so on, is to cancel the growth of intelligence based on force.
4. Like other online games, there are no special ideas about equipment and medicines.
5. About combat
1) task battle: it is a battle between humans and machines. Multiple players can join the battle and compete with the computer to complete some difficult tasks.
2) player vs.: it is a PK.
6. On the battlefield, because each player has only one role, each person can only control one character and add a time limit. Each player has a control time of about 10 seconds, if this time is exceeded, no action will be taken or automatically controlled by the computer.
7. In order to prevent a player from being attacked by a group P after entering the opponent's position, each person is added to the speed attribute. The battlefield determines the speed based on the action sequence of each character, in this way, even if one person is beaten, other people can continue to seek blood.
8. To prevent the fight, the war games are different from other games. No one wants to go into the attack scope of the other party first. The solution I want to solve is as follows:
1) players are allocated to the battlefield immediately, so that the players are under attack or under attack at the beginning. However, this method may be too unreasonable for those with weak defenses.
2) When the battle starts, add the attacker and the defender. When the attacker does not perform any attack within the specified round, the attacker will be punished accordingly, it can be random, or it can be determined before a player starts a war.
In general, let's talk about this many ideas first.
I am a very fond user, and I will try to do it if I have an idea, but I am also a relatively lazy person who really want to complete such a game, i'm afraid I don't have the time and energy.
So here, I will simply implement some of the functions on the battlefield and, by the way, practice the preliminary production of online games.
I hope I have the perseverance to complete it as much as possible ......
The client implementation method, I still choose flash, because Flash can be used without downloading,
The implementation of the server can use C, C ++ or Java. Here, I select Java
For flash and Java, socket
Now, let's get started.
To be lazy, I made it directly on the basis of my previous games. For details, see my other posts on SLG games.
The Code mainly starts to look at the source code. This time I will only make a brief description
First, you need to connect to and log on to the server.
So, let's take a look at the connection between flash and the Java Server.
If you are interested in studying with me, you need Flash CS3 or later and eclipse
Client:
// First, establish the connection IP address and port. Here, the server uses local temporarily <br/> private var hostip: String = '2017. 0.0.1 '; <br/> private var port: Int = 8210; <br/> // connect to the server <br/> Public Function start (): void {<br/> // connect to socket <br/> socket = new socket (hostip, Port ); <br/> // register the listener function for connection completion <br/> socket. addeventlistener (event. connect, onconnect, false, 0, true); <br/> // listener Connection closed <br/> socket. addeventlistener (event. close, funclose); <br/> // register the data communication function <br/> socket. addeventlistener (progressevent. socket_data, onsocketdata); <br/> // register the corresponding error function <br/> socket. addeventlistener (ioerrorevent. io_error, failconnect, false, 0, true); <br/> socket. addeventlistener (securityerrorevent. security_error, onsecurityerror, false, 0, true); <br/>}< br/> // send data to the server <br/> Public Function writemsg (MSG: string ): void {<br/> socket. writeutfbytes (MSG + "/N"); <br/> socket. flush (); <br/>}
This is the client's simple code for connecting to the server.
Check the server:
To put it simply, establish a socket and connect to the socket
Private serversocket server;
Server = new serversockets (8210 );
However, when using these simple code for connection, you will encounter a security problem, that is, the Flash Security Sandbox problem,
The solution is that when Flash is accessed, returns a <cross-domain-Policy> <allow-access-from domain =/"*/" to-ports =/"8210/"/> </Cross-Domain-Policy >/0 string, to allow access
Of course, the real implementation requires a lot of code. At last, I will put the code up.
Create a login interface on the client to connect to and log on to the server
Effect,
When the server is not started or fails to connect to the server, the corresponding error message is displayed.
For ease of use, I set the password for logon authentication information to 123, and the user name is random.
Enter the login information and send it to the server for verification.
Sav. Socket. writemsg ("type = login & name =" + txtname. Text + "& pass =" + txtpass. Text );
Here, the type indicates the type of authentication information, which indicates login authentication, user name, and password.
After successful login, the server returns
Bman. sendtosocket (this. socket, "type = in ");
Indicates Successful Logon, user name, and output information.
Of course, these rules are self-defined, because in the Process of server intercommunication information, for convenience, I assume that all information is legal and does not need to be processed, because when we are actually playing online games, all the information must be verified to prevent unauthorized users. This time, we will omit these items.
After the client receives the information successfully verified by the server, the page jumps
If (resulthash. getvalue ("type") = "in "){
Sav. Game. loginsuc ();
Return;
}
After successful logon,
Add a simple chat function
The client sends the input information to the server.
Sav. Socket. writemsg ("type = talk & MSG =" + _ txtinput. Text );
The server sends this information to all users.
If ("talk". Equals (result. Get ("type "))){
Bman. sendtoall ("type = talk & name =" + User. getname () + "& MSG =" + result. Get ("MSG "));
}
After receiving the information, the client displays it.
If (resulthash. getvalue ("type") = "talk "){
Addtext (resulthash. getvalue ("name") + ":" + resulthash. getvalue ("MSG "));
Return;
}
Effect,
Now everyone knows the charm of flash. It's so easy to make a chat tool.
Before entering the battle, players must have their own roles. This should be set at the beginning of the game,
Because I only studied the battlefield this time, I chose a character before the battle.
Time is limited. I only describe how I work. I only write the code to the key point. The complete code will be provided for download when the game is about to take shape.
After successful logon, create an interface,
Used for pre-war preparation,
Create four optional characters first
Private VaR _ arr_select: array = [New charactermc (1), new charactermc (98), new charactermc (100), new charactermc (103)];
Then they are placed on the page in sequence, and click events are added. When a user clicks a character, the user selects the character to play the game.
(_ Arr_select [0] As sprite). addeventlistener (mouseevent. mouse_down, onselect01 );
(_ Arr_select [1] As sprite). addeventlistener (mouseevent. mouse_down, onselect02 );
(_ Arr_select [2] As sprite). addeventlistener (mouseevent. mouse_down, onselect03 );
(_ Arr_select [3] As sprite). addeventlistener (mouseevent. mouse_down, onselect04 );
The following is a click event. When a user clicks this character, the selected role is displayed in the upper-left corner.
Private function onselect01 (E: mouseevent): void {
If (Sav. index> = 0 ){
Return;
}
_ Arr_select [0]. Alpha = 1;
_ Arr_select [1]. Alpha = 1;
_ Arr_select [2]. Alpha = 1;
_ Arr_select [3]. Alpha = 1;
Sav. ID = 1;
_ Arr_select [0]. Alpha = 0.5;
// Sav. show_user is a sprite that has been created to display the selected role.
Sav. show_user.removechildat (1 );
VaR showchara: charactermc = new charactermc (Sav. ID );
Showchara. Y = 25;
Sav. show_user.addchild (showchara );
}
Then, draw four optional boxes: Two Party A and two Party B.
For example
When you click the corresponding position, you should add the role selected by yourself to this position
However, it is not acceptable to add a game interface on your own. It should be visible to everyone. Therefore, when selecting a location, you should first notify the server
Sav. Socket. writemsg ("type = startin & Index = 0 & id =" + SAV. ID );
This statement indicates that the server selects the Sav. ID role and places it in the 0 position.
Then the server notifies everyone else,
Bman. sendtoall ("type = select & name =" + User. getname () + "& Index =" + User. getroomindex () + "& id =" + User. GETID ());
This statement indicates that the server notifies all users, the user. getname () user, selects the user. GETID () role, and selects the user. getroomindex () location.
Then, after receiving the notification from the server, all users are displayed on the page.
As you can see, I used four users to log on. After they chose the role and location of the person, they can all see it.
The next study will bring these guys into the battlefield at the same time.
After all four players have selected their characters and positions, the server notifies each player to enter the battlefield,
Tell players which battlefield they enter and where they appear
As for map data, it can be directly loaded locally. I can save some trouble and send it to the players directly from the server.
String startxml = "<DATA>" +
"<Map> 1 </map>" +
"<Datamap>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"<List>, </List>" +
"</Datamap>" +
"<Our>" +
"<List x @/" 9/"Y @/" 12/"speed @/" "+ ourlist. get (0 ). getspeed () + "/" username @/"" + ourlist. get (0 ). getname () + "/"> "+ ourlist. get (0 ). GETID () + "</List>" +
"<List x @/" 10/"Y @/" 13/"speed @/" "+ ourlist. get (1 ). getspeed () + "/" username @/"" + ourlist. get (1 ). getname () + "/"> "+ ourlist. get (1 ). GETID () + "</List>" +
"</Our>" +
"<Enemy>" +
"<List x @/" 8/"Y @/" 13/"speed @/" "+ enemylist. get (0 ). getspeed () + "/" username @/"" + enemylist. get (0 ). getname () + "/"> "+ enemylist. get (0 ). GETID () + "</List>" +
"<List x @/" 11/"Y @/" 13/"speed @/" "+ enemylist. get (1 ). getspeed () + "/" username @/"" + enemylist. get (1 ). getname () + "/"> "+ enemylist. get (1 ). GETID () + "</List>" +
"</Enemy>" +
"</Data> ";
Bman. sendtoall ("type = Start & xml =" + startxml );
Because there is an equal sign in the string when communicating with the server, the equal sign in the XML data is changed to @ first, and then to the client.
Each user enters the battlefield after receiving the start command
In the previous game, we started to enter the battlefield. This time, we only added some things before entering the battlefield.
The screen is as follows. The user name is displayed on each character on the battlefield, and your information is displayed on the top left of the screen.
This simple version has been completed,
However, I just want to learn how to create an online game and simply implement the following functions. I also changed the original code. It can be said that the code is messy,
If you want to see how the AS and Java communicate, you can refer to it. If you want to learn, don't read the code.
For test, open the Directory
SLG/as3.0/bin
Worker Process
There are two methods to close: one is to close directly in the task manager, and the other is to send the Stop command in the game dialog box.
After the server starts, start the game caocao.exe file,
I originally wanted to make it a web version, but because of the falsh embedded in the web page after flash10.1, when the page is not the current page, the automatic dynamic frame rate of flash will be automatically reduced to 2 frames per second, the timer will only be triggered twice per second. The game is basically in the stopped state, so there is no way to publish it as an EXE file.
Enter the user name and password after entering the game. The user name is arbitrary and the password is 123
Then, select one of the following four roles, and click the corresponding location of Party A or Party B.
Click the caocao.exe file and enter another user name.
In this way, a total of four game files are opened, which is equivalent to four playing games.
After selecting the corresponding roles and positions, the four members will enter the battlefield interface of the game and look at the characters in the upper left corner. Black and white indicate that the current operation is unavailable, and the color indicates that the current operation can be performed, there are text prompts in the lower right corner of the game.
Conclusion: as3.0 is easy to play online games.
Download source code:
Http://download.csdn.net/source/2892360