2048 Little Games Little notes

Source: Internet
Author: User
Tags gety

1. Game Interface Layout

by Textview1 and Textview2 to display the score, 2048 game main interface consists of GridLayout, the bottom of which is to add a button to restart the game.

2. Create a game main class Gameview

Add the initialization Method Initgameview () in the build function;

Setontouchlistener () in the initialization Method Initgameview () to listen for touch events and add methods for the upper and lower left and right touch events swipeup (), Swipedown (), Swipeleft (), swiperight ();

To determine the touch event code:

 Public BooleanOnTouch (View V, motionevent event) {Switch(Event.getaction ()) { CaseMotionEvent.ACTION_DOWN:startX=Event.getx (); Starty=event.gety ();  Break;  CaseMotionEvent.ACTION_UP:offsetX= Event.getx ()-StartX; OffsetY= Event.gety ()-Starty; if(Math.Abs (OffsetX) >Math.Abs (OffsetY)) {                            if(offsetx<-5) {swipeleft (); }Else if(offsetx>5) {swiperight (); }                        }Else{                            if(offsety<-5) {swipeup (); }Else if(offsety>5) {Swipedown (); }                        }                         Break; }

Use a custom class on an Android XML layout file

  

In a custom class, you must include a constructor, XXX (Context context,attributeset attr) {super (context,attr);}

Use Package.class in XML to invoke, such as registering for Com.test class as HelloWorld in XML <com.test.HelloWorld/>

The Layout_main.xml.

<com.mark.game.my2048.GameView        android:id= "@+id/gameview"        android:layout_width= " Fill_parent "        android:layout_height=" 0DP "        android:layout_weight=" 1 "        >    < /com.mark.game.my2048.gameview>

where android:layout_height= "0DP" android:layout_weight= "1" is used to indicate the amount of space to allocate. 1 means weight

  

    

2048 Little Games Little notes

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.