HTML and CSS Reference
In-Depth Information
Creating Enemies and Actions
It is time to set up the enemies and make them move in the direction defined in the tile map tlWayPoint layer. There
are three types of monsters, and all of them can display themselves and update the hit points. (Refer to Monster.js for
the implementation.)
Monsters can be created both automatically and manually. The ToolBar allows you to create monsters
manually, and you will use a schedule to add monsters to the game automatically. Listing 24-16 provides the code to
accomplish this.
Listing 24-16. Adding the First Monster, and Starting a Schedule for Automatically Adding Monsters in GameLayer.js
var GameLayer = cc. Layer.extend ( {
init : function ( ) {
this.addMonster ( Monster.createOrange ( ) ) ;
this.schedule ( this.autoAddMonster , 2 ) ; // Add a monster every 2 seconds.
} ,
} ) ;
The code will add a monster randomly every two seconds. Monsters will move on the path defined with
cc.CardinalSplineTo , as shown in Listing 24-17.
Listing 24-17. The addMonster Function in GameLayer.js
addMonster : function ( monster ) {
if ( ! this._monsterLayer ) {
this._monsterLayer = cc.Layer.create ( ) ;
this.addChild ( this ._monsterLayer ) ;
}
this ._monsterLayer.addChild ( monster , 1 ) ;
var array = this._maps.getWayPositions ( ) ; // Get the way point frome tlWayPoint layer.
var action1 = cc.CardinalSplineTo.create ( 20 , array , 0 ) ; // create the action.
var remove = cc.CallFunc.create ( function ( ) {
// You will lose when monster passed the gate. We will create the GameOverScene later.
cc.Director.getInstance().replaceScene(new GameOverScene(false));
this.removeFromParent ( ) ;
// cc.log("remove monster");
} , monster ) ;
monster.showRange ( HD .SHOW_RANGE ) ;
monster.getSprite ( ).runAction ( cc.Sequence.create ( action1 , remove ) ) ;
It will create a monster layer and then add the monsters to it. After adding monster layer to the game layer, your
screen should look like the one shown in Figure 24-18 .
 
Search WWH ::




Custom Search