クリック可能なオブジェクトがあるポイント&クリックゲームを作成しています。プレイヤーがオブジェクトの上にマウスを移動すると、カーソルの横にツールチップが表示されます。以下のコードでほぼ意図したとおりに動作します。
private function added():void
{
removeEventListener(Event.ADDED, added);
this.addEventListener(TouchEvent.TOUCH, onTouch);
}
protected function onTouch(e:TouchEvent):void
{
var touchHover:Touch = e.getTouch(this, TouchPhase.HOVER);
if (touchHover)
{
trace("show");
//mouse is hovered over this object. Therefore call Hovertext:
if (Game.hoverText.message != name_)
Game.hoverText.message = name_
}
else
{
//mouse leaves the object
trace("hide");
Game.hoverText.hideMessage(name_);
}
}
しかし、私には理解できない奇妙な問題があります。マウスをオブジェクトの上に移動してから、オブジェクトの上にとどまったまま下に移動すると、2 フレームごとに非表示機能がトリガーされます。カーソルを右に移動すると同じことが起こりますが、上または左に移動すると起こりません。
私の質問は、私のコードの何が問題なのですか? これは、マウスがオブジェクトの上をいつ移動し、いつ離れたのかを検出するための最良の方法ですか?
編集:私は、それぞれ同じ問題で次の反復を行ってきました:
var touch:Touch = event.getTouch(this);
if (touch == null) {
// Set Object alpha to 0;
//trace("pois");
Game.hoverText.hideMessage(name_);
}
else if (touch.phase == TouchPhase.HOVER) {
// Set Object alpha to 1;
//trace("paalla");
if (Game.hoverText.message != name_)
Game.hoverText.message = name_;
}
else {
// for a phase BEGIN/MOVE/STATIONARY case
// see if the touch is over the bounds of the tile (assuming 'this' is the tile)
HELPER_POINT.x = touch.globalX;
HELPER_POINT.y = touch.globalY;
this.globalToLocal(HELPER_POINT, HELPER_POINT);
if(this.hitTest(HELPER_POINT, true) != null)
{
// Set Object alpha to 1; over tile
trace("paalla");
}
else
{
// Set Object alpha to 0; not over tile
trace("pois");
}
}
var touchHover:Touch = e.getTouch(this);
if (touchHover && touchHover.phase == TouchPhase.HOVER)
{
trace("show");
//mouse is hovered over this object. Therefore call Hovertext:
if (Game.hoverText.message != name_)
Game.hoverText.message = name_
}
if (touchHover == null)
{
//mouse leaves the object
trace("hide");
Game.hoverText.hideMessage(name_);
}
また、問題を示すための swf もここにあります: http://www.students.tut.fi/~salmi26/ScorpionBox.html