Andengine----customizing Buttonsprite with text

Source: Internet
Author: User

On an article, learned the use of andengine in the Buttonsprite, said to achieve the buttonsprite with text, directly paste code:

public class Textbuttonsprite extends Rectangle {private final static String TAG = "Textbuttonsprite";

	Display text private final text mText;

	Buttonsprite Mbuttonsprite; Public Textbuttonsprite (float PX, float PY, tiledtextureregion ptiledtextureregion, Font Pfont, String ptext, Vertex
				Bufferobjectmanager Pvertexbufferobjectmanager) {super (PX, PY, Ptiledtextureregion.getwidth (), ptiledtextureregion
		. GetHeight (), Pvertexbufferobjectmanager);
		Mbuttonsprite = new Buttonsprite (0, 0, ptiledtextureregion, Pvertexbufferobjectmanager);
		MText = new Text (0, 0, Pfont, Ptext, Pvertexbufferobjectmanager);
		float buttontextureregionwidth = Ptiledtextureregion.getwidth ();
		float buttontextureregionheight = Ptiledtextureregion.getheight ();
		float textWidth = Mtext.getwidth ();
		float textHeight = Mtext.getheight (); When the length of the text is greater than the length of the button, the button is scaled to fit the text if (TextWidth > Buttontextureregionwidth | | textHeight > BUTTONTEXTUREREGIONH Eight) {Float Widthscale = TextwidTh/buttontextureregionwidth;
			float Heightscale = textheight/buttontextureregionheight;
				if (Widthscale > Heightscale) {setwidth (buttontextureregionwidth * widthscale);
				SetHeight (Buttontextureregionheight * widthscale);
			Add an offset, of course, you can not add Mbuttonsprite.setscale ((Widthscale + 0.1f));
				} else {setwidth (buttontextureregionwidth * heightscale);
				SetHeight (Buttontextureregionheight * heightscale);
			Add an offset, of course, you can not add Mbuttonsprite.setscale ((Heightscale + 0.1f));
		}} if (Mchildrenignoreupdate) {}//Calculate center position Float TEXTX = (getwidth ()-mtext.getwidth ())/2;
		float texty = (getheight ()-mtext.getheight ())/2;
		Mbuttonsprite.setposition ((GetWidth ()-buttontextureregionwidth)/2, (GetHeight ()-buttontextureregionwidth)/2);
		Mtext.setposition (TEXTX, texty);
		Attachchild (Mbuttonsprite);
	Attachchild (MText); }//===========================================================//Constants//===========================================================//===========================================================//Fields//====== =====================================================

	// ======================================================== = = =//constructors//===========================================================//===============================  ============================//Getter & Setter//===========================================================// ===========================================================//Methods for/from superclass/interfaces//=========== ================================================ @Override Public boolean onareatouched (TouchEvent pscenetouchevent
		, float ptoucharealocalx, float ptoucharealocaly) {log.e (TAG, "[onareatouched]");
	Return mbuttonsprite.onareatouched (Pscenetouchevent, Ptoucharealocalx, ptoucharealocaly); }//===========================================================//Methods//===========================================================//===========================================================//Inner
 and Anonymous Classes//===========================================================}


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.