-
-
Save amosl/f783372dd92b205585e6 to your computer and use it in GitHub Desktop.
Spot Light
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ui | |
{ | |
import flash.display.Bitmap; | |
import starling.display.BlendMode; | |
import starling.display.Image; | |
import starling.display.Quad; | |
import starling.textures.RenderTexture; | |
import starling.textures.Texture; | |
public class Spotlight extends Image | |
{ | |
private var scrWidth:Number; | |
private var scrHeight:Number; | |
private var mAlpha:Number; | |
private var mBuffer:RenderTexture; | |
private var mOverlay:Quad; | |
private var mSpot:Image; | |
[Embed(source="oval.png")] | |
private static var OVAL_IMG_CLS:Class; | |
public function Spotlight(w:Number, h:Number) | |
{ | |
scrWidth= w; | |
scrHeight=h; | |
mBuffer= new RenderTexture(scrWidth/2, scrHeight/2, false); | |
super(mBuffer); | |
} | |
public function create(alphaVal:Number=0.7, color:uint= 0x333333):void | |
{ | |
mAlpha= alphaVal; | |
mOverlay= new Quad(scrWidth/2, scrHeight/2, color); // size is 1/4 of the screen | |
var ovalImg:Bitmap = new OVAL_IMG_CLS(); | |
mSpot= new Image(Texture.fromBitmap(ovalImg, false)); | |
mSpot.pivotX= mSpot.width/2; | |
mSpot.pivotY= mSpot.height/2; | |
// Place spot and set blendMode to erase | |
mSpot.x = mOverlay.width/2; | |
mSpot.y = mOverlay.height/2; | |
mSpot.blendMode= BlendMode.ERASE; | |
// Draw the texture | |
mBuffer.drawBundled(drawElements); | |
this.touchable = false; | |
this.scaleX = this.scaleY = 2; // Scale by 2 | |
} | |
override public function dispose():void | |
{ | |
mBuffer.dispose(); | |
mBuffer= null; | |
mSpot.dispose(); | |
mSpot= null; | |
super.dispose(); | |
} | |
public function updateSpot(xp:Number, yp:Number, szX:Number=0, szY:Number=0):void | |
{ | |
mSpot.x = xp/2; | |
mSpot.y = yp/2; | |
if (szX > 0) mSpot.width = szX/2; | |
if (szY > 0) mSpot.height = szY/2; | |
// Redraw | |
mBuffer.clear(); | |
mBuffer.drawBundled(drawElements); | |
} | |
private function drawElements():void | |
{ | |
mBuffer.draw(mOverlay, null, mAlpha); | |
mBuffer.draw(mSpot); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Isn't it the same?
http://wiki.starling-framework.org/extensions/pixelmask