40th days happy here-Android games hitting planes (3) adding enemy planes
August 9. "The city is picturesque, and Shan Xiaowang is clear. The rain is transparent, and the bridges fall into the rainbow. ."
The previous article has loaded bullets, sound effects, and background music for airplanes. This article mainly introduces enemy planes.
Several functions used in this article are described as follows:
1. voidSetTag(Int nTag) sets the action tag.
2. The CCRANDOM_0_1 () function generates a random number between [0, 1]. The number between [0-100] is to be generated; generate the float number between [1, 5], that is, CCRANDOM_0_1 * 4 + 1.
3. sprite. getContentSize to get the width and height of the sprite rectangle. Obtain the original node size, which is a logical size, not a pixel.
Float initX = (winSize. width-sprite. getContentSize (). width) * ccMacros. CCRANDOM_0_1 () + sprite. getContentSize (). width/2;
Sprite. getContentSize (). width/2 indicates any position of the enemy on the X axis to prevent the occurrence of half of the enemy.
I,
MainActivity. java
Package edu. eurasia. cocos2d_game04; import org. cocos2d. layers. CCScene; import org. cocos2d. nodes. CCDirector; import org. cocos2d. opengl. CCGLSurfaceView; import org. cocos2d. sound. soundEngine; import android. OS. bundle; import android. app. activity; import android. view. window; import android. view. windowManager; public class MainActivity extends Activity {// create a view object. The cocos2d engine will draw the image on this 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); // sets getWindow () for full screen display of the current program (). setFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, WindowManager. layoutParams. FLAG_FULLSCREEN); // The getWindow () cannot be automatically sleep on the screen (). setFlags (WindowManager. layoutParams. FLAG_KEEP_SCREEN_ON, WindowManager. layoutParams. FLAG_KEEP_SCREEN_ON); view = new CCGLSurfaceView (this); setContentView (view); // gets the director object CCDirector ctor = CCDirector. sharedDirector (); // sets the output target of the Game Engine screen Viewdirector. attachInView (view); // sets whether the game displays the FPS value. // director. setDisplayFPS (true); // set the game's update rate FPS = frame per seconddirector. setAnimationInterval (1/60366f); // generate the scenario object CCScene scene = CCScene. node (); // generate the layer object PlaneLayer layer = new PlaneLayer (this); // Add the layer to scene in the scenario. addChild (layer, 1); // notify the director of the running scenario director. runWithScene (scene) ;}@ Overrideprotected void onDestroy () {super. onDestroy (); // clear all sounds SoundEngine. sharedEngine (). realesAllSounds (); SoundEngine. sharedEngine (). realesAllEffects (); // completely disable the audio system SoundEngine. purgeSharedEngine ();}}
Ii. PlaneLayer. java
Package edu. eurasia. cocos2d_game04; import java. util. arrayList; import java. util. list; import org. cocos2d. actions. instant. CCCallFuncN; import org. cocos2d. actions. interval. CCMoveTo; import org. cocos2d. actions. interval. CCSequence; import org. cocos2d. config. ccMacros; import org. cocos2d. layers. CCLayer; import org. cocos2d. nodes. CCDirector; import org. cocos2d. nodes. CCSprite; import org. cocos2d. sound. soundEngine; 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 {// declare a private CCSprite plane; private CCDirector ctor; private CGSize winSize; private CGPoint offset; private boolean flag = false; // define the speed of the bullet to 500 pixels per second private float bulletSpeed = 500; private Context context; private List
Bullets = new ArrayList
(); Private List
Enemies = new ArrayList
(); Private static final int egion tag = 1; private static final int E2_TAG = 2; public PlaneLayer (Context context) {super (); this. context = context; // set whether the current layer accepts the touch event this. setIsTouchEnabled (true); director = CCDirector. sharedDirector (); winSize = director. winSize (); // initialize the sprite object plane = CCSprite. sprite ("p.png"); // set the location of the sprite object plane. setPosition (CGPoint. ccp (winSize. width/2,200); this. addChild (plane); // timer schedule, Add a bullet, call schedule ("addBullet", 0.5f) every 0.5 seconds; // Add an enemy, call schedule ("addEnemy", 1f) every second ); // background music SoundEngine. sharedEngine (). playSound (context, R. raw. game_music, true);} // when the user starts to touch the screen, execute this 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 (fla G) {offset = CGPoint. ccpSub (plane. getPosition (), point);} return super. ccTouchesBegan (event);} // when the user's finger leaves the screen, execute this method @ Overridepublic boolean ccTouchesEnded (MotionEvent event) {flag = false; return super. ccTouchesEnded (event);} // when the user's finger moves on the screen, execute this method @ Overridepublic boolean ccTouchesMoved (MotionEvent event) {if (flag) {CGPoint = director. convertToGL (CGPoint. ccp (event. getX (), event. getY (); point = CGPoi Nt. ccpAdd (point, offset); plane. setPosition (point);} return super. ccTouchesMoved (event);} public void addBullet (float delta) {// generate a bullet sprite object CCSprite bullet = CCSprite. sprite ("bullet.png"); // Add the bullet object to the layer. this. addChild (bullet); // place the newly added bullet object in bullets set bullets. add (bullet); // obtain the sprite size. The getContentSize function obtains the original node size CGSize planeSize = plane. getContentSize (); CGSize bulletSize = bullet. getContentSize (); CGPoint in ItPos = plane. getPosition (); // the initial position of the bullet's Y axis, initPos. y = initPos. y + planeSize. height/2 + bulletSize. height/2; bullet. setPosition (initPos); // create an object CGPoint targetPos = CGPoint that represents coordinates. ccp (initPos. x, winSize. height); // calculate the distance between two coordinate points, and calculate the float distance = CGPoint. ccpDistance (initPos, targetPos); // calculate the bullet running time float t = distance/bulletSpeed; // generate an animation object and move the bullet to the top of the screen. CCMoveTo moveTo = CCMoveTo. action (t, ta RgetPos); // generates an action object. When this action is executed, the onBulletMoveToFinished method of the current object is called. // CCCallFuncN: // It allows you to specify a callback function for an object that executes this action. The callback function we specified is onBulletMoveToFinishedCCCallFuncN func = CCCallFuncN. action (this, "onBulletMoveToFinished"); // CCSequence: // it allows us to combine a series of actions into an action sequence, and these actions can be executed in order. All actions are executed at a time. CCSequence seq = CCSequence. actions (moveTo, func); // notifies the genie to execute the bullet action. runAction (seq); // The Bullet effect SoundEngine. sharedEngine (). playEffect (context, R. raw. bullet);} public void onBulletMoveToFinished (Object sender) {if (sender instanceof CCSprite) {CCSprite sprite = (CCSprite) sender; // remove the bullets from the set. remove (sprite); // remove the bullet object from the screen this. removeChild (sprite, true) ;}} public void addEnemy (float delta) {String imgName = "e1a0.png"; int tag = E1_TAG; // The generated value is [0, 1] Random Number float ran = ccMacros. CCRANDOM_0_1 (); if (ran> 0.8) {imgName = "e2a0.png"; tag = E2_TAG;} // generate the genie object CCSprite sprite = CCSprite representing the enemy. sprite (imgName); // sets the action flag sprite. setTag; // Add the attacker to the current layer. this. addChild (sprite); // place the newly added enemy Host object in the benemies set enemies. add (sprite); // calculates the initialization position float initY = winSize of the enemy machine. height + sprite. getContentSize (). height/2; float initX = (winSize. width-sprite. getContentSize (). width) * ccMacros. CCRANDOM_0_1 () + sprite. getContentSize (). width/2; // create an object that represents coordinates CGPoint initPos = CGPoint. ccp (initX, initY); // sets the sprite location of the enemy. setPosition (initPos); // calculate the end point of an enemy's machine running. float targetX = initX; float targetY =-sprite. getContentSize (). height/2; // create an object CGPoint targetPos = CGPoint that represents coordinates. ccp (targetX, targetY); // calculate the running time of the enemy machine float t = 3 * ccMacros. CCRANDOM_0_1 () + 2; // generates the MoveTo object CCMoveTo moveTo = CCMoveTo. action (t, targetPos); CCCallFuncN func = CCCallFuncN. action (this, "onEnemyMoveToFinished"); CCSequence seq = CCSequence. actions (moveTo, func); // notifies the sprite to execute the action. runAction (seq);} public void onEnemyMoveToFinished (Object sender) {if (sender instanceof CCSprite) {CCSprite sprite = (CCSprite) sender; // remove the enemy planes that fly out of the screen from the set. remove (sprite); // remove this from the screen. removeChild (sprite, true );}}}
III,
Running result
: Http://download.csdn.net/detail/zwszws/7734537