Cocos2d_android Developing Simple Games

Source: Internet
Author: User

1) game layer design:

public class Wellcomelayer extends Cclayer {public wellcomelayer () {this.setistouchenabled (true); Ccsprite background1 = Ccsprite.sprite ("background.jpg"); Ccsprite Background2 = Ccsprite.sprite ("background.jpg"); Background1.setanchorpoint (0, 0); Background1.setposition ( 0, 0); This.addchild (BACKGROUND1); background2.setanchorpoint (0, 0); Background2.setposition (0, Background1.getcontentsize (). height); this.addchild (BACKGROUND2); Ccsprite logo = Ccsprite.sprite ("logo.png"); Logo.setanchorpoint (0, 0); Logo.setposition ((Ccdirector.shareddirector ( ). Winsize (). Width-logo.getcontentsize (). width)/2, Ccdirector.shareddirector (). Winsize (). HEIGHT/2); this.addchild (logo);} Game Interface layer public class Gameoverlayer extends Cclayer {public gameoverlayer (int score) {... }}

2) scrolling background design

At the same time, put three pictures in the layer, vertical arrangement, and 2 pixels per frame to move downward, when the second picture to the origin, repeated loop


Background1 = Ccsprite.sprite ("background.jpg"); Background2 = Ccsprite.sprite ("background.jpg"); Background3 =  Ccsprite.sprite ("background.jpg"); Background3.setanchorpoint (0, 0); Background2.setanchorpoint (0, 0); Background1.setanchorpoint (0, 0); This.addChild (  BACKGROUND1); This.addchild (Background2); This.addchild (BACKGROUND3);    Initialize three background Graph//Use cocos2d timing scheduling This.schedule ("Backgroundmove", 0.01f); /** * Background Move * @param dalat */public void Backgroundmove (float Dalat) {background1.setposition (0, BACKGROUND1.GETP  Osition (). y-2);  Background2.setposition (0, Background1.getposition (). Y +   background1.getcontentsize (). height);  Background3.setposition (0, Background2.getposition (). Y + background2.getcontentsize (). height);  if (Background2.getposition (). y = = 0) {background1.setposition (0, 0); }  }

2) Background music:

context = Ccdirector.shareddirector (). getactivity ();  Soundengine.sharedengine (). Preloadeffect (context, r.raw.game_music); Soundengine.sharedengine (). PlaySound (Context, r.raw.game_music, true);

4) Wizard design and Generation:

/** * Fired bullets * @param dalat */public void shoot (float dalat) {Ccsprite bullet = Ccsprite.sprite ("Bullet1.png");  Bullet.setposition (Player.getposition (). x, Player.getposition (). Y + player.getcontentsize (). height);  AddChild (bullet);  Ccmoveby move = ccmoveby.action (5, CGPOINT.CCP (0, Winsize.getheight () + 10));  Bullet.runaction (move);  This.bulletList.add (bullet);   /** * Randomly generated enemy aircraft @param Dalat */public void Addenemy (float Dalat) {ccsprite enemy = Ccsprite.sprite ("Enemy1.png");  Enemy.setposition (Random.nextint ((int) winsize.getwidth ()), Winsize.getheight ());  Cgpoint p = CGPOINT.CCP (0, 0-winsize.getheight ()-20);  AddChild (enemy);  Ccmoveby move = Ccmoveby.action (p);  Enemy.runaction (move);  This.enemyList.add (enemy); }

5) Collision Detection

/** * Infer If the host is colliding with enemy aircraft * @param Dalat * @return * */public void Boom (float Dalat) {for (int i = 0; i < enemylist . Size ();  i++) {Ccsprite enemy = Enemylist.get (i);  if (Cgrect.intersects (Player.getboundingbox (), Enemy.getboundingbox ())) {  Ccblink blink = ccblink.action (1, 3);  Cchide hide = Cchide.action ();  This.enemyList.remove (i);  Ccsequence seq = ccsequence.actions (blink, hide);  Enemy.runaction (ENEMYDOWNSEQ);  Player.runaction (seq);  New Thread () {public void run () {try {thread.sleep (1000);  } catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();  } exitgame ();  }}.start ();  } if (Enemy.getposition (). Y < 0) {this.enemyList.remove (i);  This.removechild (enemy, true); }  }  }

Interface:





Source code: Https://github.com/wujiagan/plane-war

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.