TOUCH_TAP not firing sometimes on iPhone
Hi,
I find that when I have sprites on the screen, the TOUCH_TAP event sometimes does not fire.
In the following program I generate random squares. On each tap event, I place a circle on where the tap occured. When there are little or no squares, the tap event is always triggered and the circle is placed below the tap. However, as I increase the number of squares, the probability of the tap event not firing increases. Here I only have 5 squares, and the tap event is not fired 1 out of 50 taps. As I increase it to 100, the probablity increaes to 1 out of 10.
package
{
import flash.desktop.NativeApplication;
import flash.events.Event;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.display.StageOrientation;
/**
* ...
* @7111211
*/
public class Main extends Sprite
{
public var m_clickPos:Sprite; // sprite that tells where you clicked
public var m_squares:Sprite; // parent sprite that contains random squares
public const k_numSquares:int = 5;
public function Main():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(Event.DEACTIVATE, deactivate);
// touch or gesture?
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
stage.setOrientation(StageOrientation.ROTATED_LEFT);
stage.addEventListener(TouchEvent.TOUCH_TAP, onTap);
m_squares = new Sprite();
genSquares();
addChild(m_squares);
m_clickPos = new Sprite();
m_clickPos.graphics.beginFill(0xFFFFFF);
m_clickPos.graphics.drawCircle(0, 0, 5);
m_clickPos.graphics.endFill();
m_clickPos.graphics.beginFill(0);
m_clickPos.graphics.drawCircle(0, 0, 1);
m_clickPos.graphics.endFill();
addChild(m_clickPos);
}
private function deactivate(e:Event):void
{
// auto-close
NativeApplication.nativeApplication.exit();
}
public function onTap(event:TouchEvent):void
{
m_clickPos.x = event.stageX;
m_clickPos.y = event.stageY;
}
public function randomInt(low:uint = 0, high:uint = 0):int
{
return Math.floor(Math.random() * (1 + high - low)) + low;
}
public function genSquares():void
{
var sprite:Sprite;
var squareWidth:int = 30;
var colors:Vector.<uint> = new Vector.<uint>;
colors.push(0xFF0000);
colors.push(0xFFFF00);
colors.push(0x00FF00);
colors.push(0x0000FF);
for (var square:int = 0; square < k_numSquares; square++)
{
sprite = new Sprite();
sprite.x = randomInt(0, 960 - squareWidth);
sprite.y = randomInt(0, 640 - squareWidth);
sprite.graphics.beginFill(colors[randomInt(0, colors.length - 1)]);
sprite.graphics.drawRect(0, 0, squareWidth, squareWidth);
sprite.graphics.endFill();
m_squares.addChild(sprite);
}
}
}
}
I am using iPod Touch. Any ideas why this is happening and how to work around this?
Thanks
