HTML and CSS Reference
In-Depth Information
onTouchMoved : function ( touch , event ) {
var touchPoint = touch. getLocation ( ) ;
var moveTouch = cc.pSub ( touchPoint , this ._beganTouch ) ;
this.setPosition ( cc.pAdd ( this ._curPosition , moveTouch ) ) ;
} ,
onTouchEnded : function ( touch , event ) {
// check position
var location = this ._gameLayer. checkTowerLocation ( this.getPosition ( ) ) ;
if ( ! location ) {
// If it is a invalid location, set to previous position.
this.setPosition ( this ._curPosition ) ;
} else {
// Set the tower to new position.
this.setPosition ( location ) ;
}
this ._curPosition.x = 0;
this ._curPosition.y = 0;
}
} ) ;
After registration of touch, you will have an input event in OnTouchBegan , OnTouchMoved , and OnTouchEnded .
The code shows that the tower will move to the new position when the position is in a valid location. Please refer to
Tower.js for implementation.
There are two types of bullets. The difference between them is that the low-level bullet will disappear after it hits
a monster, and it can only hurt one monster at a time. Conversely, high-level bullets can go through and hurt all of
the monsters.
High-level bullets extend from cc.Node , coming from a high-level tower and are removed when a lifetime is over.
It has lifetime property and attack function. (Refer to Tower.js for the implementation.) The high-level bullets will be
added to the bullet layer.
Low-level bullets are created when monsters come within the attack range of low-level towers. Low-level bullets
are simply a sprite with a MoveTo action, and every bullet will hit one monster and then disappear. Thus you don't
need to check the collision of low-level bullets, you just add them to game layer instead of the bullet layer (refer to the
Tower.js file). The code of low-level bullets is shown in Listing 24-15.
Listing 24-15. Low-level Bullets in Tower.js
var bullet = cc.Sprite.create ( s_Bullet ) ;
bullet. setPosition (
cc. pAdd ( this.getPosition ( ), this ._sBall. getPosition ( ) ) ) ;
this ._gameLayer. addChild ( bullet ) ;
var move = cc. MoveTo.create ( 0.1 , monster. getSprite ( ).getPosition ( ) ) ;
bullet. runAction ( cc. Sequence.create (
move ,
cc. CallFunc.create ( function ( ) {
bullet. removeFromParent ( ) ; // auto removing call back.
} , bullet )
) ) ;
monster. lostBlood ( 20 ) ; // it will shoot at monsters always.
The low-level bullets use cc.Sequence to create a series of actions, which will be executed in order. The
cc.CallFunc function enables bullets to remove themselves when the action sequence is completed.
Search WWH ::




Custom Search