Instance analysis jetboy

Source: Internet
Author: User
Implementation principle:

Start a separate game thread to display different view and button states based on different thread states in the onclik event in the main view.

Private jetboythread mjetboythread; // game thread

Private jetboyview mjetboyview; // view of game running

Private button mbutton; // play button

Private button mbuttonretry; // Retry button

Private textview mtextview; // help text displayed in the game

Private textview mtimerview; // Game clock

Program entry point:

<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"

Package = "com. example. Android. jetboy" Android: versioncode = "1"
Android: versionname = "1.0.0">
<Application Android: icon = "@ drawable/icon"
Android: Label = "@ string/app_name"
Android: theme = "@ Android: style/theme. notitlebar">
<Activity Android: Name = ". jetboy"

Android: Label = "@ string/app_name"
Android: screenorientation = "Landscape">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main
"/>
<Category
Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
</Application>
<Uses-SDK Android: minsdkversion = "3"> </uses-SDK>
</Manifest>

It is obviously jetboy. java.

In Main. XML, framelayout is used, where the view and button of appeal are deployed.

The code in oncreate is very simple, mainly to get the view and button, and then initialize mjetboythread and mjetboyview;

Public class jetboy extends activity implements view. onclicklistener

Note that the purpose of this implementation is to process all button events in an onclick. The main logic is implemented in onclick (view V. For details, see:

Public void onclick (view v ){

If (mjetboythread. getgamestate () = jetboythread. state_start ){
Mbutton. settext ("play! ");
Mtextview. setvisibility (view. Visible );

Mtextview. settext (R. String. helptext );
Mjetboythread. setgamestate (jetboythread. state_play );

}
// We have entered game play, now we about to start running
Else if (mjetboythread. getgamestate () = jetboythread. state_play ){
Mbutton. setvisibility (view. Invisible );
Mtextview. setvisibility (view. Invisible );
Mtimerview. setvisibility (view. Visible );
Mjetboythread. setgamestate (jetboythread. state_running );

}
// This Is A Retry button
Else if (mbuttonretry. Equals (V )){

Mtextview. settext (R. String. helptext );

Mbutton. settext ("play! ");
Mbuttonretry. setvisibility (view. Invisible );
// Mbuttonrestart. setvisibility (view. Invisible );

Mtextview. setvisibility (view. Visible );
Mbutton. settext ("play! ");
Mbutton. setvisibility (view. Visible );

Mjetboythread. setgamestate (jetboythread. state_play );

} Else {
Log. D ("JB View", "unknown click" + v. GETID ());

Log. D ("JB View", "State is" + mjetboythread. mstate );

}
}

It is easy to understand.

In addition, two onkeydown and onkeyup events are implemented to handle keys in the game.

 

Core Implementation: jetboyview

1 surfaceview Analysis

Public class jetboyview extends surfaceview implements surfaceholder. Callback {}

Surfaceview is often used in complex game development. It provides a graphic interface that can be directly controlled. It can draw various backgrounds, characters, and scenes on the canvas.

Use getholder () to return surfaceholde to control surfaceview;

Surfaceholder holder = getholder ();
Holder. addcallback (this); // Add a callback interface for this holder.

The above is the code in the program;

 

Surfaceholder. Callback can be used to obtain the surface change information;

Public void surfacechanged (surfaceholder holder, int format, int width, int height) {}// triggered when the surface size changes

Public void surfacecreated (surfaceholder arg0 ){}

// Triggered during creation. Generally, the drawing thread is called here.

Public void surfacedestroyed (surfaceholder arg0 ){}

// Triggered when the image is destroyed. Generally, the drawing thread is stopped and released here.

The above are the implemented interface methods;

 

2 gameevent

The program defines two gameevent subclasses: keygameevent and jetgameevent.

Keygameevent from keyboard input

Jetgameevent is from jetplayer.

What is jetplayer involved here? I should be familiar with games I have developed. I checked the help to find out what I did. A series of jet concepts will be involved here and we will learn from relevant materials:

Jet is a music player on embedded devices, including embedded devices that run small robot platforms. It allows applications to include interactive music audio tracks in the MIDI format in response to real-time game events and user interaction.

Jet Engine is an engine that controls the sound effects of a game. It uses MIDI format and can control the time and progress of the game (a precise clock is essential for a game)

Jetplayer provides access to jet content

Jetcreator is a box of tools for creating jet content;

 

Detailed description will be provided later;

 

3. jetboythread

The main thread of the entire game, first look at the definition

Class jetboythread extends thread implements onjeteventlistener

It implements the onjeteventlistener interface to obtain jetplayer events;

First, let's take a look at the main functions of this thread:

  • Handle jetplayer and key events;
  • 2. Draw the display in surfaceview of various animations;
  • 3. Game status settings;
  • 4. Storage of game data for users;

Program Logic

Before carefully analyzing the code, it is necessary to see how the logic of the program is implemented? How does a thread start and run?

 

First, jetboy: oncreate

Jetboyview: jetboyview (context, attributeset attrs {

Thread = new jetboythread (holder, context, new handler (); // create a thread and initialize the game status in the thread constructor; mstate = state_start; setrunning (true );

}

Start thread. Start ();

Start to execute jetboythread: Run () // enter the game status set. This is the subject of execution. operations are performed based on different game statuses. Until setrunning (false );

Wait for user operation at this time;

Jetboy onkeydown and onkeyup will trigger the dokeydown and dokeyup of jetboythread, put the event in the queue, and then check meventqueue in updategamestate for event processing:

@ Override
Public Boolean onkeydown (INT keycode, keyevent MSG ){

If (keycode = 4)
Super. onkeydown (keycode, MSG );

Return mjetboythread. dokeydown (keycode, MSG );
}

 

Public Boolean dokeydown (INT keycode, keyevent MSG ){
Meventqueue. Add (New keygameevent (keycode, false, MSG ));

Return true;
}

 

Protected void updategamestate (){
// Process any game events and apply them
While (true ){
Gameevent event = meventqueue. Poll ();
If (event = NULL)
Break;

// Log. D (TAG, "*** event =" + Event );

// Process keys tracking the input context to pass in to later
// Cballs
If (event instanceof keygameevent ){
// Process the key for affects other then asteroid hits
Mkeycontext = processkeyevent (keygameevent) event, mkeycontext );

// Update laser state. Having this here allows the laser
// Be triggered right when the key is
// Pressed. If we comment this out the laser will only be
// Turned on when updatelaser is called
// When processing a timer event below.
Updatelaser (mkeycontext );

}
// Jet events trigger a State update
Else if (event instanceof jetgameevent ){
Jetgameevent jetevent = (jetgameevent) event;

// Only update state on a timer event
If (jetevent. value = timer_event ){
// Note the time of the last beat
Mlastbeattime = system. currenttimemillis ();

// Update laser state, turning it on if a key has been
// Pressed or off if it has been
// On for too long.
Updatelaser (mkeycontext );

// Update explosions before we update asteroids because
// Updateasteroids may add
// New explosions that we do not want updated until next
// Frame
Updateexplosions (mkeycontext );

// Update asteroid positions, hit status and animations
Updateasteroids (mkeycontext );
}


Processjetevent (jetevent. Player, jetevent. segment, jetevent. Track,
Jetevent. Channel, jetevent. Controller, jetevent. value );
}
}
}

The logic should be clear.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.