Step-by-step implementation of the aircraft war in cocos2d-Implementation of your own aircraft
In the previous section, we talked about the rolling of the background. Now we are setting up our own planes in the game. In order to keep the GameScene code too much, you can encapsulate your own planes and call them in GameScene. Create Plane:
Plane. h:
# Include "cocos2d. h"
USING_NS_CC;
Class plane: publicNode {
Public:
Int hp = 100;
Int px, py;
CREATE_FUNC (plane );
Bool init ();
Void moveTo (int x, int y );
};
Plane. cpp:
# Include "plane. h"
Boolplane: init (){
If (! Node: init ()){
Return false;
}
Auto sp = Sprite: create ("boss0.png ");
Sp-> setTag (333 );
This-> addChild (sp );
Return true;
}
Voidplane: moveTo (int x, int y ){
This-> getChildByTag (333)-> setPosition (x, y );
This-> px = x;
This-> py = y;
}
Call in GameScene
First, start referencing Plane. h.
# Include "Plane. h"
Instantiate in init:
BoolgameScene: init (){
If (! Layer: init ()){
Return false;
}
Plane * p = plane: create ();
This-> addChild (p );
P-> moveTo (Director: getInstance ()-> getWinSize (). width/2,200 );
P-> settags (444 );
Return true;
}
In this way, your plane will appear in the game scene.