...

/

Implementing Shooting Through Operators and Observables

Implementing Shooting Through Operators and Observables

Learn to implement shoot functionality using the merge Observable and controlling it through the timestamp operator.

We'll cover the following...

It’s a bit scary seeing the hordes of enemies coming at us, but all we can do is move out of the way and hope they don’t see us.

How about we give our hero the ability to shoot at the evil alien spaceships?

We want our spaceship to shoot whenever we click the mouse or press the spacebar, so we’ll create an Observable for each event and merge them into a single Observable called playerShots. Notice that we filter the keydown Observable by the key code of the spacebar, 32:

Press + to interact
var playerFiring = Rx.Observable
.merge(Rx.Observable.fromEvent(canvas, 'click'),
Rx.Observable.fromEvent(document, 'keydown')
.filter(function(evt)
{
return evt.keycode === 32;
}) )

Now that we know about the sample method, we can use it to spice up the game and limit the shooting frequency of our spaceship. Otherwise, the player ...

Access this course and 1400+ top-rated courses and projects.