Leapmotionas3交互手指作为鼠标

时间:2013-08-23 23:18:20

标签: actionscript-3 flash leap-motion

我正在从标识中测试leapmotionAS3库:

https://github.com/logotype/LeapMotionAS3

我可以让手势工作,我有2个应用程序使用这个手势,  但我不能让手指充当“鼠标”并与屏幕上的按钮互动。

有谁知道怎么做?任何建议,提示,教程?

要连接到我使用的设备:

package  {

import flash.display.Sprite;
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.events.LeapEvent;
import com.leapmotion.leap.Frame;
import com.leapmotion.leap.Hand;
import com.leapmotion.leap.Finger;
import com.leapmotion.leap.Vector3;
import com.leapmotion.leap.util.LeapUtil;
import com.leapmotion.leap.Gesture;
import com.leapmotion.leap.SwipeGesture;
import com.leapmotion.leap.ScreenTapGesture;
import com.leapmotion.leap.KeyTapGesture;
import com.leapmotion.leap.Screen;
import flash.events.Event;
import com.greensock.TweenLite;
import com.greensock.easing.Expo;
import com.greensock.plugins.*;
import com.greensock.easing.Back;
import flash.display.Shape;
import flash.text.TextField;
import flash.display.StageDisplayState;
import flash.events.TimerEvent;
import flash.utils.Timer;

TweenPlugin.activate([TintPlugin]);



public class Controlador extends Sprite {
    private var leap:Controller;
    private var cursor:Sprite;
    private var screenWidth:uint;
    private var screenHeight:uint;
    private var currentVector:Vector3;
    private var screen:Screen;
    private var screenList:Vector.<Screen>;
    private var texto:TextField;
    private var texto2:TextField;
    private var boton:Sprite;

    public function Controlador() {

        stage.displayState = StageDisplayState.FULL_SCREEN;

        cursor = new Sprite();
        cursor.graphics.beginFill( 0xff0000 );
        cursor.graphics.drawCircle( -5, -5, 20 );
        cursor.graphics.endFill();
        this.addChild( cursor );

        texto = new TextField();
        texto.text = "CODIGO";  
        texto.width = 250;  
        texto.x = 25;  
        texto.y = 25;  
        addChild(texto);


        texto2 = new TextField();
        texto2.text = "CODIGO";  
        texto2.width = 250;  
        texto2.x = 25;  
        texto2.y = 125;  
        addChild(texto2);

        boton = new Sprite();
        boton.graphics.beginFill(0xff6600);
        boton.graphics.drawCircle(150,150,50);
        boton.graphics.endFill();
        addChild(boton);
        setChildIndex(boton,0);

        // constructor code
        leap = new Controller();
        leap.addEventListener(LeapEvent.LEAPMOTION_CONNECTED, alConectar);
        leap.addEventListener(LeapEvent.LEAPMOTION_FRAME, enFrame);
        leap.addEventListener(LeapEvent.LEAPMOTION_INIT, initLeap);




    }
    private function alConectar(event:LeapEvent):void{
        trace("conectado");

        screenList = leap.locatedScreens();
        screen = screenList[ 0 ];
        screenWidth = screen.widthPixels();
        screenHeight = screen.heightPixels();


        leap.enableGesture(Gesture.TYPE_SCREEN_TAP);

    }
    private function initLeap(event:LeapEvent):void{
            trace("init");

    }
    private function enFrame(event:LeapEvent):void{

        texto2.text = "X Stage: " + String(mouseX)+ "Y Stage: " + String(mouseY);
        var frame:Frame = event.frame;
            if ( frame.hands.length > 0 )
        {
            var hand:Hand = frame.hands[ 0 ];

            var fingers:Vector.<Finger> = hand.fingers;
            if ( !fingers.length == 0 )
            {

            }
            var normal:Vector3 = hand.palmNormal;
            var direction:Vector3 = hand.direction;


        }
        var gestures:Vector.<Gesture> = frame.gestures();
        for ( var i:int = 0; i < gestures.length; i++ )
        {
            var gesture:Gesture = gestures[ i ];

            switch ( gesture.type )
            {


                case Gesture.TYPE_SCREEN_TAP:
                    var screentap:ScreenTapGesture = ScreenTapGesture ( gesture);
                        texto.text = "KeyTapGesture: x" + Math.round(screentap.position.x ) + ", y"+ Math.round( screentap.position.y);
                    break;

                default:

                    trace( "Unknown gesture type." );

                    break;
            }
        }
        if ( event.frame.pointables.length > 0)
        {

            currentVector = screen.intersectPointable( event.frame.pointables[ 0 ], true );
            cursor.x = screenWidth * currentVector.x - stage.nativeWindow.x;
            cursor.y = screenHeight * ( 1 - currentVector.y ) - stage.nativeWindow.y;

            if (cursor.hitTestObject(boton))
        {
            var timer:Timer = new Timer(200, 0);
                timer.addEventListener(TimerEvent.TIMER, timerHandler);
                timer.start();


            texto2.text = "Colision detectada";

        }
        else
        {
            var timer2:Timer = new Timer(500, 0);
                timer2.addEventListener(TimerEvent.TIMER, timerHandler);
                timer2.start();
        texto2.text = "Sin colision";
            TweenLite.to(boton, 3, {scaleX:1, scaleY:1, alpha:1, tint:0xff0000, ease:Back.easeInOut})
            }

        }

    }
    private function timerHandler(event:TimerEvent):void{
        //TweenLite.to(boton, 3, {scaleX:10, scaleY:10, alpha:1, tint:0x33ff00, ease:Back.easeInOut})
        trace("click");
    }
}   

}

光标跟随指针作为鼠标。我实现了hitTestObject,但问题是它被多次调用。有没有办法只做一次,比如鼠标点击?

0 个答案:

没有答案