多点触控as3问题

时间:2013-07-30 08:49:42

标签: actionscript-3 multi-touch

我在as3中遇到多​​点触控问题。 最近开始了一个与我的学生练习有关的项目。 这对于Android来说是个小游戏。 用于控制两个虚拟操纵杆,这就是它们的问题。 只要我使用其中一个或另一个,所有工作都很好。但是,当我尝试同时使用两个时,其中一个被阻挡,移动的物体开始随机移动,我无法控制它。

这是我的代码:

joystick.as:

package com.controls {

import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.display.Sprite;
import flash.events.Event;
import flash.display.MovieClip;
import flash.geom.Rectangle;

import com.controls.JoystickKnob;
import com.Hero;
import com.Fire;

import com.greensock.*;
import com.greensock.easing.*;

public class Joystick extends MovieClip {

    Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; 

    private var my_x:Number;
    private var my_y:Number;

    private var knob:JoystickKnob;
    private var hero:Hero;
    private var fire:Fire;

    private var knob_tween:TweenLite;

    public function Joystick(margin_left, margin_bottom, hero_mc) {

        var circle:Sprite = new Sprite();
        circle.graphics.beginFill(0x696969);
        circle.graphics.drawCircle(50, 50, 60);
        circle.graphics.endFill();
        addChild(circle);

        my_x = margin_left;
        my_y = margin_bottom;
        hero = hero_mc;

        if (stage) {
            init();
        } else {
            addEventListener(Event.ADDED_TO_STAGE,init);
        }       
    }

    private function init(e:Event = null):void {


        if (hasEventListener(Event.ADDED_TO_STAGE)) {
            removeEventListener(Event.ADDED_TO_STAGE,init);
        }

        this.x = my_x + this.width / 2;
        this.y = stage.stageHeight - my_y - this.height / 2;

        knob = new JoystickKnob();
        knob.x = 0;
        knob.y = 0;

        knob.origin_x = 0;
        knob.origin_y = 0;

        addChild(knob);

        this.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin, true);
        knob.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove, true);
        stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd, true);

        knob.buttonMode = true;
    }

    private function onTouchBegin(event:TouchEvent):void {

        knob.x = this.mouseX;
        knob.y = this.mouseY;

        onTouchMove(null);

    }

    private function onTouchMove(event:TouchEvent):void {

        if (knob_tween) {
            knob_tween.kill();
        }

        this.addEventListener(Event.ENTER_FRAME, knobMoved);
        knob.startDrag(false, new Rectangle( -  this.width /2, - this.height /2, this.width, this.height));

    }

    private function knobMoved(event:Event):void {

        // LEFT OR RIGHT
        if (knob.x > 15) {
            hero.move_right = true;
            hero.move_left = false;

        } else if (knob.x < -15) {
            hero.move_right = false;
            hero.move_left = true;

        } else {
            hero.move_right = false;
            hero.move_left = false;

        }

        // UP OR DOWN
        if (knob.y > 15) {
            hero.move_down = true;
            hero.move_up = false;

        } else if (knob.y < -15) {
            hero.move_down = false;
            hero.move_up = true;

        } else {
            hero.move_down = false;
            hero.move_up = false;
        }
    }

    private function onTouchEnd(event:TouchEvent):void {

        knob.stopDrag();

        hero.move_left = false;
        hero.move_up = false;
        hero.move_right = false;
        hero.move_down = false;


        if (this.hasEventListener(Event.ENTER_FRAME)) {
            this.removeEventListener(Event.ENTER_FRAME, knobMoved);
        }


        mover();
    }

    private function mover():void {

        knob_tween = new TweenLite(knob, 0.5, {x: knob.origin_x, y:knob.origin_y, ease:Bounce.easeOut});
    }
}   

}

joystickKnob.as:

package com.controls {

import flash.display.Sprite;
import flash.display.MovieClip;

public class JoystickKnob extends MovieClip {       

    private var _origin_x:Number;
    private var _origin_y:Number;       

    private var knob:Class;

    public function JoystickKnob() {

        var circle:Sprite = new Sprite();
         circle.graphics.beginFill(0x32CD32);
         circle.graphics.drawCircle(50, 50, 35);
         circle.graphics.endFill();
         addChild(circle);
    }

    public function get origin_x():Number {

        return _origin_x;
    }

    public function set origin_x(o_x:Number):void {

        _origin_x = o_x;
    }

    public function get origin_y():Number {

        return _origin_x;
    }

    public function set origin_y(o_y:Number):void {

        _origin_y = o_y;
    }
}

}

第二个操纵杆代码看起来相同,只是它存储在文件joystick2.as,joystickKnob2.as中。

这是我的课程的主要课程:

package com {

import flash.events.TouchEvent;
import flash.display.MovieClip;
import flash.events.MouseEvent;

import com.controls.Joystick;
import com.controls.Joystick2;

import com.Hero;
import com.Fire;

public class MyApp extends MovieClip {

    private var joystick:Joystick;
    private var hero:Hero;
    private var joystick2:Joystick2;
    private var fire:Fire;

    public function MyApp() {

        hero = new Hero();
        hero.x = stage.stageWidth/1.7;
        hero.y = stage.stageHeight/1.7;
        addChild(hero);

        fire = new Fire();
        fire.x = stage.stageWidth/1.7;
        fire.y = stage.stageHeight/1.7;
        addChild(fire);

        joystick = new Joystick(-350, 100, hero);
        addChild(joystick);

        joystick2 = new Joystick2(600, 100, fire);
        addChild(joystick2);

    }
}

}

当同时使用两个操纵杆时,旋钮图形也会出现问题 - 而是在特定区域移动,其中一个几乎总是移动到屏幕的另一端,靠近第二个操纵杆的区域

你们中是否有人遇到过这样的问题并且知道如何解决这个问题?

致以最诚挚的问候,并提前感谢您的帮助

2 个答案:

答案 0 :(得分:2)

Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;不支持多个手指,如文档中所述:

For user interaction with multiple points of contact (such as several fingers moving across a touch screen at the same time) use the related GestureEvent, PressAndTapGestureEvent, and TransformGestureEvent classes. And, use the properties and methods of these classes to construct event handlers that respond to the user touching the device.

所以你想要Multitouch.inputMode = MultitouchInputMode.GESTURE;

答案 1 :(得分:0)

在touch_point上搜索信息时会显示此帖子,所以我想确保它正确无误。

TOUCH_POINT支持多个触摸点。每次触摸都是独一无二的,并赋予独特的身份你可以有100个同时接触。

如果您想编写自己的触摸处理程序,可以使用TOUCH_POINT。 GESTURE是为您处理的TOUCH_POINT。 GESTURE决定接触点是否正在滑动,点击等.TOUCH_POINT就像一个鼠标事件 - 原始事件。