Skip to content

Commit

Permalink
add demo scene for custom hit test
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimaryFeather committed Jul 26, 2011
1 parent f5d3469 commit e9b29a8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
8 changes: 5 additions & 3 deletions samples/demo/src/Game.as
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package

import scenes.AnimationScene;
import scenes.BenchmarkScene;
import scenes.CustomHitTestScene;
import scenes.Scene;
import scenes.TextureScene;
import scenes.TouchScene;
Expand Down Expand Up @@ -32,10 +33,11 @@ package
mMainMenu.addChild(logo);

var scenesToCreate:Array = [
["Textures", TextureScene],
["Textures", TextureScene],
["Multitouch", TouchScene],
["Animation", AnimationScene],
["Benchmark", BenchmarkScene]
["Animations", AnimationScene],
["Custom hit-test", CustomHitTestScene],
["Benchmark", BenchmarkScene]
];

var buttonTexture:Texture = Assets.getTexture("ButtonBig");
Expand Down
32 changes: 32 additions & 0 deletions samples/demo/src/scenes/CustomHitTestScene.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package scenes
{
import starling.text.TextField;
import starling.utils.HAlign;
import starling.utils.VAlign;

import utils.RoundButton;

public class CustomHitTestScene extends Scene
{
public function CustomHitTestScene()
{
var description:String =
"Pushing the egg only works when the touch occurs within a circle." +
"This can be accomplished by overriding the method 'hitTestPoint:'.";

var infoText:TextField = new TextField(300, 100, description, "Arial", 13);
infoText.x = infoText.y = 10;
infoText.vAlign = VAlign.TOP;
infoText.hAlign = HAlign.CENTER;
addChild(infoText);

// 'RoundButton' is a helper class of the Demo, not a part of Starling!
// Have a look at its code to understand this sample.

var button:RoundButton = new RoundButton(Assets.getTexture("EggClosed"));
button.x = Constants.CenterX - int(button.width / 2);
button.y = 150;
addChild(button);
}
}
}
45 changes: 45 additions & 0 deletions samples/demo/src/utils/RoundButton.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package utils
{
import flash.geom.Point;
import flash.geom.Rectangle;

import starling.display.Button;
import starling.display.DisplayObject;
import starling.textures.Texture;

public class RoundButton extends Button
{
public function RoundButton(upState:Texture, text:String="", downState:Texture=null)
{
super(upState, text, downState);
}

public override function hitTestPoint(localPoint:Point, forTouch:Boolean=false):DisplayObject
{
// When the user touches the screen, this method is used to find out if an object was
// hit. By default, this method uses the bounding box, but by overriding it,
// we can change the box (rectangle) to a circle (or whatever necessary).

// when the hit test is done to check if a touch is hitting the object, invisible or
// untouchable objects must cause the hit test to fail.
if (forTouch && (!visible || !touchable))
return null;

// get center of button
var bounds:Rectangle = this.bounds;
var centerX:Number = bounds.width / 2;
var centerY:Number = bounds.height / 2;

// calculate distance of localPoint to center.
// we keep it squared, since we want to avoid the 'sqrt()'-call.
var sqDist:Number = Math.pow(localPoint.x - centerX, 2) +
Math.pow(localPoint.y - centerY, 2);

// when the squared distance is smaller than the squared radius,
// the point is inside the circle
var radius:Number = bounds.width / 2;
if (sqDist < Math.pow(radius, 2)) return this;
else return null;
}
}
}

0 comments on commit e9b29a8

Please sign in to comment.