Pixel Perfect Touch

author:
Łukasz 'Cywil' Cywiński
description:
Pixel perfect touch Image
lastupdate:
2012-05-07
compatible:
v1.1
tag:
touch, pixel, perfect pixel, perfect pixel touch, touch on alpha
download:
https://github.com/krechagames/starling-extensions

Overview

This extension class allow you to create an Image display object that is touchable only at the points where alpha is 0 (or higher – by using the threshold parameter).

Usage

//First we must create hitArea for a bitmapaData used in Image.
var myBitmap:Bitmap = new MyBitmap ();
var hit_1:PixelHitArea = new PixelHitArea ( myBitmap, .2); // second arg is sampling for bitmapData
//or
var hit_2:PixelHitArea = new PixelHitArea ( myBitmap, .5, 'area_name');
//then
trace ( PixelHitArea.getHitArea ('area_name') == hit_2 ) // true
 
//Now we can create a PixelImageTouch.
var img_1:PixelImageTouch = new PixelImageTouch ( Texture.fromBitmap (myBitmap), hit, 150 ); //last arg is a treshold for alpha channel;
var img_2:PixelImageTouch = new PixelImageTouch ( Texture.fromBitmap (myBitmap), PixelHitArea.getHitArea('area_name'));
 
//Additional usage.
img_1.hitArea = otherHitArea; //or null, then PixelImageTouch works just like Image, still is touchable but without alpha test
 
//dispose, after that PixelImageTouch works just like an Image
hit_1.dispose ();
PixelHitArea.disposeHitArea ( PixelHitArea.getHitArea('my_name') );
PixelHitArea.dispose (); //dispose all ares
 
trace (PixelHitArea.getDebugInfo ()) //get size and time needed to create for all hitAreas, size is valid only in debug mode, in normal it equals 0;

Good practices

  1. dispose bitmaps if you don't need them, PixelTouchArea has its own data.
  2. dispose unnecessary hitAreas.
  3. use getDebugInfo() for more info.
  4. for smallest memory usage:
  • set lower bitmapSampling ( 1 is perfect, default is .5 - enough in mostly projects )
  • put all touchable objects in one atlas

Changelog

  • 2012/11/21 10:00: PixelHitArea was move to src.starling.extension.krecha package
  • 2012/05/12 23:00: Fix PixelHitArea dispose issue (not a critic)
  • 2012/05/08 16:00: Fix PixelImageTouch dispose issue
  • 2012/05/07 03:04: First public version

Source Code

User Comments

Sorry! I don't now which Bitmap class did you import…… So you have to write down which Bitmap class did you import.

- it is the flash.display.Bitmap; (works great for me, thx!)

dear, what is MyBitmap? can you give example thank you very much!!1

Yes, what is “MyBitmap” please give an example

Hi how to use this with your asset manager ? in order to cache only needed images from one particular Atlas ?

  extensions/pixel_perfect_touch.txt · Last modified: 2015/03/11 14:06 (external edit)
 
Powered by DokuWiki