38th Day a fun-android game jerk Off (ii) mobile aircraft

Source: Internet
Author: User

August 6, Yin. " Sang-no-leaf soil smoke, xiao Tube Ying Dragon Water Temple before." Shumon a few places to see the song and dance, the fear of Spring Yin Swallow orchestral.

The last article has put the plane on the screen, how to let the plane with the finger free movement, is the problem solved.

Some of the functions to be used in this article are explained:

One, the cocos2d touch event is as follows:

1, cctouchesmoved: Accept the finger on the screen slide/stay information, that is 当用户手指在屏幕移动时,执行该方法 .

2, Cctouchesbegan: Accept the finger Click on the screen of the information, that is 当用户开始触摸屏幕时,执行该方法 .

3, cctouchesended: Accept the finger to leave the screen of that The information, that is 当用户手指离开屏幕时,执行该方法 .

Second, coordinate system

1, OpenGL coordinate system: The coordinate system origin in the lower left corner of the screen, the x-axis to the right, the y-axis upward. This is the coordinate system used in COCOS2DX.
2, screen coordinate system: the origin of the coordinate system in the upper left corner of the screen, the x-axis to the right, the y-axis downward, in fact, and the OpenGL coordinate system is the y-axis direction. Suppose that the resolution of the game scene is (500,500), where the coordinates of one point are (200,200), then it is (200,200) in the OpenGL coordinate system, or (200,500-200) in the screen coordinate system.
3, World coordinate system: Also known as absolute coordinate system, the world coordinate system and OpenGL coordinate system are in the same direction, the origin point is in the lower left corner of the screen, X axis to the right, Y axis upward.
4, node coordinate system: Also known as the relative coordinate system, and the OpenGL coordinate system direction, the difference is the origin of the parent node in the lower left corner.

Converttogl (CGPOINT.CCP (Event.getx (), Event.gety ())): Get screen coordinates first, and then go to OpenGL coordinate system in call CONVERTTOGL .

Third,Mainactivity.java

Package Edu.eurasia.cocos2d_game02;import Org.cocos2d.layers.ccscene;import Org.cocos2d.nodes.ccdirector;import Org.cocos2d.opengl.ccglsurfaceview;import Android.os.bundle;import Android.view.window;import Android.view.windowmanager;import Android.app.activity;public class Mainactivity extends Activity {//Create a View object, The COCOS2D engine will draw the graph over the View object private Ccglsurfaceview view = null, @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate);//Do not display the title bar requestwindowfeature (Window.feature_no_title); /Set Current program full-screen display GetWindow (). SetFlags (Windowmanager.layoutparams.flag_fullscreen,windowmanager.layoutparams.flag_ fullscreen);//settings do not allow the screen to automatically hibernate GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); view = new Ccglsurfaceview (this); Setcontentview (view);// Get Director Object Ccdirector Director = Ccdirector.shareddirector ();//Set the output target Viewdirector.attachinview (view) of the game engine screen;// Sets whether the game shows FPS value Director.setdisplayfps (TRUE);//Set the game's refresh rate FPS = Frame per seconddirector.setanimationinterval (1/30.0f);//Generate Scene object Ccscene scene = Ccscene.node ();//Generate Layer Object Planelayer Layer = new Planelayer (this);//Add layers to the scene scene.addchild (layer);//notify Director, run scene director.runwithscene (scene);}}

This paragraph adds a feature that does not display the title bar, full screen, and does not allow automatic hibernation.

Four, Planelayer.java

Package Edu.eurasia.cocos2d_game02;import Org.cocos2d.layers.cclayer;import Org.cocos2d.nodes.ccdirector;import Org.cocos2d.nodes.ccsprite;import Org.cocos2d.types.cgpoint;import Org.cocos2d.types.cgrect;import Org.cocos2d.types.cgsize;import Android.content.context;import Android.view.motionevent;public class PlaneLayer Extends cclayer{//declares an Elf object private ccsprite plane;private ccdirector director;private cgsize winsize;private CGPoint    Offset;private Boolean flag = false;    Public Planelayer (Context context) {//sets whether the current layer accepts touch event this.setistouchenabled (true);    Director = Ccdirector.shareddirector (); winsize = Director.winsize ();    Initializes the Sprite object plane = Ccsprite.sprite ("P.png");    Sets the position of the Sprite object Plane.setposition (CGPOINT.CCP (WINSIZE.WIDTH/2, 200));    This.addchild (plane); }//When the user starts to touch the screen, execute the method @Overridepublic Boolean Cctouchesbegan (Motionevent event) {Cgpoint point = DIRECTOR.CONVERTTOGL ( CGPOINT.CCP (Event.getx (), Event.gety ())); CGRect rect = Plane.getboundingbox (); flag= Cgrect.containspoint (rect, point), if (flag) {offset = Cgpoint.ccpsub (Plane.getposition (), point);}    Return Super.cctouchesbegan (event);} When the user finger leaves the screen, the method @overridepublic Boolean cctouchesended (Motionevent event) {flag = False;return super.cctouchesended (event);} When the user finger moves on the screen, the method is executed @overridepublic boolean cctouchesmoved (Motionevent event) {if (flag) {Cgpoint point = DIRECTOR.CONVERTTOGL (CGPOINT.CCP (Event.getx (), Event.gety ()));p oint = Cgpoint.ccpadd (point, offset); Plane.setposition (point);}       Return super.cctouchesmoved (event);} }
v. Results of Operation


Vi. Source Code

http://download.csdn.net/detail/zwszws/7722519


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.