-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5d3469
commit e9b29a8
Showing
3 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
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
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
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); | ||
} | ||
} | ||
} |
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
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; | ||
} | ||
} | ||
} |