Deluxe Edition aircraft vs series (4) and deluxe edition aircraft vs Series

Source: Internet
Author: User

Deluxe Edition aircraft vs series (4) and deluxe edition aircraft vs Series

The previous article introduced the implementation of the enemy class EnemySprite. This article introduces the implementation of the main class. In the game, we can see that there are pig flying around the main character class, always beside the main character plane. Let's first introduce the implementation of PigSprite, this is because the leading aircraft class will be used later.

See the implementation of PigSprite. h:

# Include "cocos2d. h" USING_NS_CC; class PigSprite: public cocos2d: Sprite {public: PigSprite ();~ PigSprite (); virtual bool init (); // sprite initialize CREATE_FUNC (PigSprite); // create FUNCTION macro void f_createAnimate (int count, int fps ); // create the animation void f_followPlane (float dt); // follow the plane Sprite * spritepig ;};
Next, let's look at the implementation in PigSprite. cpp:
# Include "PigSprite. h" # include "PlaneLayer. h" PigSprite: PigSprite () {} PigSprite ::~ PigSprite () {} bool PigSprite: init () {if (! Sprite: init () {return false;} Size winSize = ctor :: getInstance ()-> getWinSize (); spritepig = Sprite: createWithSpriteFrame (SpriteFrameCache: getInstance () -> getSpriteFrameByName ("hero_01.png"); this-> setPosition (Point (winSize. width/2, winSize. height/2); this-> addChild (spritepig); // set the zoom factor to 0.6, that is, to 0.6this-> setScale (0.6 ); // pass-through pig flying animation f_createAnimate (3, 8); // time scheduling function, make every frame call the f_followPlane function to keep the piglet around the plane this-> schedule (schedule_selector (PigSprite: f_followPlane); return true ;} /*** obtain the location information of the plane so that the position of the piglet is always around the plane. The function determines whether the Piglet has reached the boundary, update pig * to the left or right of the plane */void PigSprite: f_followPlane (float dt) {Size winSize = Director: getInstance ()-> getWinSize (); auto PlanePos = PlaneLayer: sharedPlane-> getChildByTag (AIRPLANE)-> getPosition (); if (PlanePos. x + 60 + spritepig-> getContentSize (). width <= winSize. width) {this-> setPosition (Point (PlanePos. x + 60 + spritepig-> getContentSize (). width/2, PlanePos. y);} else {this-> setPosition (Point (PlanePos. x-60-spritepig-> getContentSize (). width/2, PlanePos. y);}/*** create a pig flying animation. count indicates the number of frames, fps indicates the interval of each frame, and * RepeatForever creates an infinite repeating animation, let spritepig execute this Animation */void PigSprite: f_createAnimate (int count, int fps) {char buff [16]; Animation * panimation = Animation: create (); panimation-> setDelayPerUnit (1.0/fps); for (int id = 1; id <= count; id ++) {sprintf (buff, "hero_01_d.png", id ); panimation-> addSpriteFrame (SpriteFrameCache: getInstance ()-> getSpriteFrameByName (buff);} spritepig-> runAction (RepeatForever: create (Animate :: create (panimation )));}
I will not explain it too much here. The Code has detailed comments, and there is no particular difficulty. In the function of pig following the airplane, the aircraft Singleton class is used to obtain the location information of the aircraft. The aircraft Singleton class is implemented below.

The Singleton plane class inherits the Layer. In this Layer, the PigSprite that follows will be added. Let's look at the implementation of PlaneLayer. h:

# Include "cocos2d. h "# include" PigSprite. h "USING_NS_CC; enum Enum_Plane {AIRPLANE = 1,}; class PlaneLayer: public cocos2d: Layer {public: static cocos2d: Scene * createScene (); // creation scenario, return the created virtual bool init (); // init initialization function void menuCloseCallback (cocos2d: Ref * pSender); // The menu callback function static PlaneLayer * create (); // create a static function, in which the Singleton is set and the init method public: PigSprite * mp_pig is called; // The following Piglet genie static PlaneLayer * sharedPlane; // global Singleton flag, return the void f_createSprite () in the preceding example. // create the airplane genie function public: bool onTouchBegan (Touch * touch, Event * pEvent ); // click the start Event Response Function void onTouchMoved (Touch * touch, Event * pEvent); // click the mobile Event Response Function };
The functions of each function are described in detail. Next, let's take a look at the implementation of PlaneLayer. cpp:

# Include "PlaneLayer. h "# include" BulletSprite. h "USING_NS_CC;/*** static Singleton variable initialization */PlaneLayer * PlaneLayer: sharedPlane = NULL;/*** creation scenario, and add the PlaneLayer layer */Scene * PlaneLayer: createScene () {auto scene = Scene: create (); auto layer = PlaneLayer: create (); scene-> addChild (layer); return scene;}/*** create a layer and assign the created layer to the singleton variable */PlaneLayer * PlaneLayer: create () {PlaneLayer * pRet = new PlaneLayer (); if (pRet & pRet-> init () {PRet-> autorelease (); sharedPlane = pRet; // assign a value to the singleton variable return pRet;} else {CC_SAFE_DELETE (pRet); return NULL ;}} bool PlaneLayer: init () {if (! Layer: init () {return false;}/*** load the cache image. Use TexturePacker to package the image and load the generated plist file, that is, the xml file * SpriteFrameCache cache class loads and caches the Image Based on the image corresponding to the key value in plist to the system * facilitates obtaining the corresponding image from the cache */SpriteFrameCache: getInstance () -> addSpriteFramesWithFile ("pig. plist "); SpriteFrameCache: getInstance ()-> addSpriteFramesWithFile (" bullet. plist "); SpriteFrameCache: getInstance ()-> addSpriteFramesWithFile (" cached "); SpriteFrameCache: getInstance ()-> addSpriteFramesWithFile (" nplane. plist "); Size visibleSize = Director: getInstance ()-> getVisibleSize (); Point origin = Director: getInstance ()-> getVisibleOrigin (); // create closemenuauto closeItem = MenuItemImage: create ("Hangzhou", "CloseSelected.png", CC_CALLBACK_1 (PlaneLayer: menuCloseCallback, this); closeItem-> setPosition (Point (origin. x + visibleSize. width-closeItem-> getContentSize (). width/2, origin. y + closeItem-> getContentSize (). height/2); auto menu = Menu: create (closeItem, NULL); menu-> setPosition (Point: ZERO); this-> addChild (menu, 1 ); // call the createsprite function f_createSprite (); return true;}/*** to create an airplane genie, add the pig genie, and register the Click Event */void PlaneLayer: f_createSprite () {Size winSize = Director: getInstance ()-> getWinSize (); // create an airplane genie auto plane = Sprite: create ("mplane.png "); plane-> setTag (AIRPLANE); plane-> setScale (0.7f); plane-> setPosition (Point (winSize. width/2-plane-> getContentSize (). width/2, winSize. height/7); this-> addChild (plane); // use the previously implemented pig class to create pig and add it to the layer mp_pig = PigSprite :: create (); this-> addChild (mp_pig);/*** register the click RESPONSE event */auto touchListener = EventListenerTouchOneByOne: create (); touchListener-> setSwallowTouches (true ); // do not pass down the touch event touchListener-> onTouchBegan = CC_CALLBACK_2 (PlaneLayer: onTouchBegan, this); // touch start touchListener-> onTouchMoved = CC_CALLBACK_2 (PlaneLayer: Unknown, this); // touch Mobile _ eventDispatcher-> addEventListenerWithSceneGraphPriority (touchListener, plane); // plane genie register touch event}/*** touch start event, * mainly set the position of the plane to move to the Touch */void PlaneLayer: onTouchMoved (touch * Touch, Event * pEvent) {auto target = static_cast <Sprite *> (pEvent-> getCurrentTarget (); target-> setPosition (touch-> getLocation ();}/*** touch mobile event, the plane moves along the finger to the corresponding position */bool PlaneLayer: onTouchBegan (Touch * touch, Event * pEvent) {auto target = static_cast <Sprite *> (pEvent-> getCurrentTarget (); Point locationInNode = target-> convertToNodeSpace (touch-> getLocation ()); target-> setPosition (touch-> getLocation (); return true;}/*** closemenu callback function to end the game */void PlaneLayer: menuCloseCallback (Ref * pSender) {# if (CC_TARGET_PLATFORM = CC_PLATFORM_WP8) | (CC_TARGET_PLATFORM = CC_PLATFORM_WINRT) MessageBox ("You pressed the close button. windows Store Apps do not implement a close button. "," Alert "); return; # endifDirector: getInstance ()-> end (); # if (CC_TARGET_PLATFORM = CC_PLATFORM_IOS) exit (0); # endif}

In the above introduction, the airplane genie created and followed by the pig genie were added, and the corresponding response callback function was added for the touch screen, which basically had no difficulty, after completing the above and previous work, we should introduce the most important main game scenarios and the logic in the main layer.

The next article introduces the main game scenarios and adds the main logic in the game to implement the main functions of the game.



Aircraft vs. luxury edition

You can use titanium backup to back up and unmount it. You can recover it the next time you install it. In theory, any software supports titanium backup. Trial is recommended. Not just games!

What about the aircraft vs. luxury edition?

A good game. I just played it several times. Easy to use. However, it is challenging to break the limits. Nice.


Related Article

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.