Use of controllers in andengine -- digitalonscreencontrol & analogonscreencontrol

Source: Internet
Author: User
 

Package org. andengine. examples;

Import org. andengine. Engine. Camera. camera;
Import org. andengine. Engine. Camera. Hud. Controls. analogonscreencontrol;
Import org. andengine. Engine. Camera. Hud. Controls. analogonscreencontrol. ianalogonscreencontrollistener;
Import org. andengine. Engine. Camera. Hud. Controls. baseonscreencontrol;
Import org. andengine. Engine. handler. Physics. physicshandler;
Import org. andengine. Engine. Options. engineoptions;
Import org. andengine. Engine. Options. screenorientation;
Import org. andengine. Engine. Options. resolutionpolicy. ratioresolutionpolicy;
Import org. andengine. entity. Scene. scene;
Import org. andengine. entity. Scene. Background. background;
Import org. andengine. entity. Sprite. Sprite;
Import org. andengine. entity. util. fpslogger;
Import org. andengine. Input. Touch. Controller. multitouch;
Import org. andengine. OpenGL. texture. textureoptions;
Import org. andengine. OpenGL. texture. Atlas. bitmap. bitmaptextureatlas;
Import org. andengine. OpenGL. texture. Atlas. bitmap. bitmaptextureatlastextureregionfactory;
Import org. andengine. OpenGL. texture. region. itextureregion;
Import org. andengine. UI. activity. simplebasegameactivity;
Import org. andengine. util. Math. mathutils;

Import Android. OpenGL. gles20;
Import Android. widget. Toast;

/**
* (C) 2010 Nicolas gramlich
* (C) 2011 Zynga
*
* @ Author Nicolas gramlich
* @ Since 00:06:23-11.07.2010
*/
Public class analogonscreencontrolsexample extends simplebasegameactivity {
// ================================================ ==================================
// Constants
// ================================================ ==================================

Private Static final int camera_width = 480;
Private Static final int camera_height = 320;

// ================================================ ==================================
// Fields
// ================================================ ==================================

Private camera mcamera;

Private bitmaptextureatlas mbitmaptextureatlas;
Private itextureregion mfacetextureregion;

Private bitmaptextureatlas monscreencontroltexture;
Private itextureregion monscreencontrolbasetextureregion;
Private itextureregion monscreencontrolknobtextureregion;

Private Boolean mplaceonscreencontrolsatdifferentverticallocations = false;

// ================================================ ==================================
// Constructors
// ================================================ ==================================

// ================================================ ==================================
// Getter & setter
// ================================================ ==================================

// ================================================ ==================================
// Methods for/from superclass/interfaces
// ================================================ ==================================

@ Override
Public engineoptions oncreateengineoptions (){
This. mcamera = new camera (0, 0, camera_width, camera_height );

Final engineoptions = new engineoptions (true, screenorientation. landscape_fixed, new ratioresolutionpolicy (camera_width, camera_height), this. mcamera );
Engineoptions. gettouchoptions (). setneedsmultitouch (true );
// Set multi-touch because there are two handles
If (multitouch. issupported (this )){
If (multitouch. issupporteddistinct (this )){
Toast. maketext (this, "multitouch detected --> both controls will work properly! ", Toast. length_short). Show ();
} Else {
This. mplaceonscreencontrolsatdifferentverticallocations = true;
Toast. maketext (this, "multitouch detected, but your device has problems distinguishing between fingers. \ n \ ncontrols are placed at different vertical locations. ", toast. length_long ). show ();
}
} Else {
Toast. maketext (this, "Sorry your device does not support multitouch! \ N (falling back to singletouch.) \ n \ ncontrols are placed at different vertical locations. ", Toast. length_long). Show ();
}

Return engineoptions;
}

@ Override
Public void oncreateresources (){
Bitmaptextureatlastextureregionfactory. setassetbasepath ("GFX /");

This. mbitmaptextureatlas = new bitmaptextureatlas (this. gettexturemanager (), 32, 32, textureoptions. bilinear );
This. mfacetextureregion = bitmaptextureatlastextureregionfactory. createfromasset (this. mbitmaptextureatlas, this, "face_box.png", 0, 0 );
This. mbitmaptextureatlas. Load ();

This. monscreencontroltexture = new bitmaptextureatlas (this. gettexturemanager (), 256,128, textureoptions. bilinear );
This. monscreencontrolbasetextureregion = bitmaptextureatlastextureregionfactory. createfromasset (this. monscreencontroltexture, this, "onscreen_control_base.png", 0, 0 );
This. monscreencontrolknobtextureregion = bitmaptextureatlastextureregionfactory. createfromasset (this. monscreencontroltexture, this, "onscreen_control_knob.png", 128, 0 );
This. monscreencontroltexture. Load ();
}

@ Override
Public scene oncreatescene (){
This. mengine. registerupdatehandler (New fpslogger ());

Final scene = new scene ();
Scene. setbackground (new background (0.09804f, 0.6274f, 0.8784f ));

Final float centerx = (camera_width-This. mfacetextureregion. getwidth ()/2;
Final float centery = (camera_height-This. mfacetextureregion. getheight ()/2;
Final sprite face = new sprite (centerx, centery, this. mfacetextureregion, this. getvertexbufferobjectmanager ());
Final physicshandler = new physicshandler (FACE );
Face. registerupdatehandler (physicshandler );

Scene. attachchild (FACE );

// This is the first handle to control translation.

/* Velocity Control (left ).*/
Final float X1 = 0;
Final float Y1 = camera_height-This. monscreencontrolbasetextureregion. getheight ();
Final analogonscreencontrol velocityonscreencontrol = new analogonscreencontrol (x1, Y1, this. mcamera, this. example, this. example, 0.1f, this. getvertexbufferobjectmanager (), new ianalogonscreencontrollistener ()
{
@ Override
Public void oncontrolchange (final baseonscreencontrol pbaseonscreencontrol, final float pvaluex, final float pvaluey ){
Physicshandler. setvelocity (pvaluex * 100, pvaluey * 100 );
}

@ Override
Public void oncontrolclick (final analogonscreencontrol panalogonscreencontrol ){
/* Nothing .*/
}
});

Velocityonscreencontrol. getcontrolbase (). setblendfunction (gles1_gl _ src_alpha, gles1_gl _ one_minus_src_alpha );
Velocityonscreencontrol. getcontrolbase (). setalpha (0.5f );

Scene. setchildscene (velocityonscreencontrol );

// This is the second handle that controls Rotation
/* Rotation control (right ).*/
Final float y2 = (this. mplaceonscreencontrolsatdifferentverticallocations )? 0: Y1;
Final float X2 = camera_width-This. monscreencontrolbasetextureregion. getwidth ();
Final analogonscreencontrol rotationonscreencontrol = new analogonscreencontrol (X2, Y2, this. mcamera, this. example, this. example, 0.1f, this. getvertexbufferobjectmanager (), new ianalogonscreencontrollistener ()
{
@ Override
Public void oncontrolchange (final baseonscreencontrol pbaseonscreencontrol, final float pvaluex, final float pvaluey ){
If (pvaluex = x1 & pvaluey = x1 ){
Face. setrotation (X1 );
} Else {
Face. setrotation (mathutils. radtodeg (float) math. atan2 (pvaluex,-pvaluey )));
}
}

@ Override
Public void oncontrolclick (final analogonscreencontrol panalogonscreencontrol ){
/* Nothing .*/
}
});

// Set the mixed color code
Rotationonscreencontrol. getcontrolbase (). setblendfunction (gles1_gl _ src_alpha, gles1_gl _ one_minus_src_alpha );
Rotationonscreencontrol. getcontrolbase (). setalpha (0.5f );

Velocityonscreencontrol. setchildscene (rotationonscreencontrol );

Return scene;
}

// ================================================ ==================================
// Methods
// ================================================ ==================================

// ================================================ ==================================
// Inner and anonymous classes
// ================================================ ==================================
}

There are three objects in the field of view (scene): The controlled object (FACE), the left-side handle (velocityonscreencontrol), The right handle (rotationonscreencontrol ), add these three objects to the field of view and use the following core code: scene. attachchild (FACE); scene. setchildscene (velocityonscreencontrol );

Velocityonscreencontrol. setchildscene (rotationonscreencontrol); note that the third sentence uses velocityonscreencontrol instead of scene, because if scene is used, the previous handle is overwritten.

The above uses analogonscreencontrol (analog handle). Of course, we can also use another "Digital handle", that is, digitalonscreencontrol. Let's look at the Code directly below:

Package org. andengine. examples;

Import org. andengine. Engine. Camera. camera;
Import org. andengine. Engine. Camera. Hud. Controls. baseonscreencontrol;
Import org. andengine. Engine. Camera. Hud. Controls. baseonscreencontrol. ionscreencontrollistener;
Import org. andengine. Engine. Camera. Hud. Controls. digitalonscreencontrol;
Import org. andengine. Engine. handler. Physics. physicshandler;
Import org. andengine. Engine. Options. engineoptions;
Import org. andengine. Engine. Options. screenorientation;
Import org. andengine. Engine. Options. resolutionpolicy. ratioresolutionpolicy;
Import org. andengine. entity. Scene. scene;
Import org. andengine. entity. Scene. Background. background;
Import org. andengine. entity. Sprite. Sprite;
Import org. andengine. entity. util. fpslogger;
Import org. andengine. OpenGL. texture. textureoptions;
Import org. andengine. OpenGL. texture. Atlas. bitmap. bitmaptextureatlas;
Import org. andengine. OpenGL. texture. Atlas. bitmap. bitmaptextureatlastextureregionfactory;
Import org. andengine. OpenGL. texture. region. itextureregion;
Import org. andengine. UI. activity. simplebasegameactivity;

Import Android. App. alertdialog;
Import Android. App. Dialog;
Import Android. content. dialoginterface;
Import Android. OpenGL. gles20;

/**
* (C) 2010 Nicolas gramlich
* (C) 2011 Zynga
*
* @ Author Nicolas gramlich
* @ Since 00:06:23-11.07.2010
*/
Public class digitalonscreencontrolexample extends simplebasegameactivity {
// ================================================ ==================================
// Constants
// ================================================ ==================================

Private Static final int camera_width = 480;
Private Static final int camera_height = 320;
Private Static final int dialog_allowdiagonal_id = 0;

// ================================================ ==================================
// Fields
// ================================================ ==================================

Private camera mcamera;

Private bitmaptextureatlas mbitmaptextureatlas;
Private itextureregion mfacetextureregion;

Private bitmaptextureatlas monscreencontroltexture;
Private itextureregion monscreencontrolbasetextureregion;
Private itextureregion monscreencontrolknobtextureregion;

Private digitalonscreencontrol mdigitalonscreencontrol;

// ================================================ ==================================
// Constructors
// ================================================ ==================================

// ================================================ ==================================
// Getter & setter
// ================================================ ==================================

// ================================================ ==================================
// Methods for/from superclass/interfaces
// ================================================ ==================================

@ Override
Public engineoptions oncreateengineoptions (){
This. mcamera = new camera (0, 0, camera_width, camera_height );

Return new engineoptions (true, screenorientation. landscape_fixed, new ratioresolutionpolicy (camera_width, camera_height), this. mcamera );
}

@ Override
Public void oncreateresources (){
Bitmaptextureatlastextureregionfactory. setassetbasepath ("GFX /");

This. mbitmaptextureatlas = new bitmaptextureatlas (this. gettexturemanager (), 32, 32, textureoptions. bilinear );
This. mfacetextureregion = bitmaptextureatlastextureregionfactory. createfromasset (this. mbitmaptextureatlas, this, "face_box.png", 0, 0 );
This. mbitmaptextureatlas. Load ();

This. monscreencontroltexture = new bitmaptextureatlas (this. gettexturemanager (), 256,128, textureoptions. bilinear );
This. monscreencontrolbasetextureregion = bitmaptextureatlastextureregionfactory. createfromasset (this. monscreencontroltexture, this, "onscreen_control_base.png", 0, 0 );
This. monscreencontrolknobtextureregion = bitmaptextureatlastextureregionfactory. createfromasset (this. monscreencontroltexture, this, "onscreen_control_knob.png", 128, 0 );
This. monscreencontroltexture. Load ();
}

@ Override
Public scene oncreatescene (){
This. mengine. registerupdatehandler (New fpslogger ());

Final scene = new scene ();
Scene. setbackground (new background (0.09804f, 0.6274f, 0.8784f ));

Final float centerx = (camera_width-This. mfacetextureregion. getwidth ()/2;
Final float centery = (camera_height-This. mfacetextureregion. getheight ()/2;
Final sprite face = new sprite (centerx, centery, this. mfacetextureregion, this. getvertexbufferobjectmanager ());
Final physicshandler = new physicshandler (FACE );
Face. registerupdatehandler (physicshandler );

Scene. attachchild (FACE );

This. mdigitalonscreencontrol = new digitalonscreencontrol (0, camera_height-This. interval. getheight (), this. mcamera, this. interval, this. interval, 0.1f, this. getvertexbufferobjectmanager (),
New ionscreencontrollistener (){
@ Override
Public void oncontrolchange (final baseonscreencontrol pbaseonscreencontrol, final float pvaluex, final float pvaluey ){
Physicshandler. setvelocity (pvaluex * 100, pvaluey * 100 );
}
});
This. mdigitalonscreencontrol. getcontrolbase (). setblendfunction (gles1_gl _ src_alpha, gles1_gl _ one_minus_src_alpha );
This. mdigitalonscreencontrol. getcontrolbase (). setalpha (0.5f );
This. mdigitalonscreencontrol. getcontrolbase (). setscalecenter (0,128 );
This. mdigitalonscreencontrol. getcontrolbase (). setscale (1.25f );
This. mdigitalonscreencontrol. getcontrolknob (). setscale (1.25f );
This. mdigitalonscreencontrol. refreshcontrolknobposition ();

Scene. setchildscene (this. mdigitalonscreencontrol );

Return scene;
}

@ Override
Public void ongamecreated (){
This. showdialog (dialog_allowdiagonal_id );
}

@ Override
Protected dialog oncreatedialog (final int PID ){
Switch (PID ){
Case dialog_allowdiagonal_id:
Return new alertdialog. Builder (this)
. Settitle ("setup ...")
. Setmessage ("do you wish to allow diagonal directions on the onscreencontrol? ")
. Setpositivebutton ("yes", new dialoginterface. onclicklistener (){
@ Override
Public void onclick (final dialoginterface pdialog, final int pwhich ){
Digitalonscreencontrolexample. This. mdigitalonscreencontrol. setallowdiagonal (true );
}
})
. Setnegativebutton ("no", new dialoginterface. onclicklistener (){
@ Override
Public void onclick (final dialoginterface pdialog, final int pwhich ){
Digitalonscreencontrolexample. This. mdigitalonscreencontrol. setallowdiagonal (false );
}
})
. Create ();
}
Return super. oncreatedialog (PID );
}

// ================================================ ==================================
// Methods
// ================================================ ==================================

// ================================================ ==================================
// Inner and anonymous classes
// ================================================ ==================================
}

 

This function uses a handle to control the movement of objects. Digitalonscreencontrol and analogonscreencontrol have similar functions.

Digitalonscreencontroldigitalonscreencontroldigitalonscreencontroldigitalonscreencontroldigitalonscreenonscreencontroldigitalonscreenco ntrol

 

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.