Skip to content

aemoncannon/as3-observer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

An AS3 library inspired by the C# Rx framework. Just a proof of concept so far.





Here's how you might synthesize a mouseDrag event:


var circle:Sprite = new Sprite();

var mouseDown:IObservable = Obs.es(circle, "onMouseDown");
var mouseMove:IObservable = Obs.es(circle, "onMouseMove");
var mouseUp:IObservable = Obs.es(circle, "onMouseUp");

var mouseDrag:IObservable = mouseDown.selectMany(function(ea:Event):IObservable{
		return mouseMove.until(mouseUp).select(function(ea:Event):Event{ return new Event("onMouseDrag"); });
	});

mouseDrag.attach(Obs.f(function(ea:String):void{
			trace("Just got a drag event!");
		}));





Here's a 3 key sequence, automatically cancelled if the wrong key is pressed.


// Helper for making an IObservable of key events of a specific type
function keyStream(key:int):IObservable{
	 return Obs.es(stage, "onKeyDown").select(function(e:KeyboardEvent):Boolean{ return e.keyCode == key; })
}

// Helper for making an IObservable of key events that are NOT a specific type
function notKeyStream(key:int):IObservable{
	 return Obs.es(stage, "onKeyDown").select(function(e:KeyboardEvent):Boolean{ return e.keyCode != key; })
}

// Wait for an 'a'
var abcPressed:IObservable = keyStream(Keyboard.A).selectMany(function(ea:Event):IObservable{
                // then wait for a 'b', but cancel if something else is pressed 
		return keyStream(Keyboard.B).until(notKeyStream(Keyboard.B)).selectMany(function(ea:Event):IObservable{
                  // then wait for a 'c', but cancel if something else is pressed 
		  return keyStream(Keyboard.C).until(notKeyStream(Keyboard.C)).select(function(ea:Event):Event{ 
		    // dispatch the our new event
		    return new Event("onABC"); 
		  });
	       });
	});

abcPressed.attach(Obs.f(function(ea:Event):void{
			trace("a,b,c has been pressed!");
		}));

About

A library inspired by the C# Rx framework.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published