AS3/AIR Mobile Click and/or touchevents on moving objects

孤街醉人 提交于 2019-12-12 18:06:58

问题


few weeks ago i asked a question about a similair issue, but within the Starling Framework (Starling TouchEvent on Sprite)

Now, i am working on another mobile app/game with Away3d. I seem to have the same problem touching/tapping a moving sphere or box. When the sphere is not moving, i can just tap it exactly at its position, but when it rotates, the click-event doesnt fire very accurate. this only happens on Mobile (tested on Android 4.2.2: Nexus7/Galaxy S2 and iOS: iPad2/iPad Retina)

package {
import away3d.containers.ObjectContainer3D;
import away3d.containers.View3D;
import away3d.core.pick.PickingColliderType;
import away3d.core.pick.PickingType;
import away3d.debug.AwayStats;
import away3d.entities.Mesh;
import away3d.events.MouseEvent3D;
import away3d.primitives.SphereGeometry;

import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Vector3D;

[SWF(frameRate="60")]
public class Main extends Sprite {
    private var _container:ObjectContainer3D;
    private var _view:View3D;
    private var _sphere:Mesh;
    public function Main()
    {
        super();

        addEventListener(Event.ADDED_TO_STAGE, onAdded_toStage);
    }

    private function onAdded_toStage(e:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE, onAdded_toStage);

        stage.align         = StageAlign.TOP_LEFT;
        stage.scaleMode     = StageScaleMode.NO_SCALE;

        stage.addEventListener(Event.RESIZE, onResize_stage);

        init3d();
    }

    private function onResize_stage(e:Event):void {
        _view.width     = stage.stageWidth;
        _view.height    = stage.stageHeight;
    }

    private function onEnter_frame(e:Event):void {
        _container.rotationY += 1;
        _view.render();
    }

    private function onClick_sphere(e:MouseEvent3D):void {
        //reposition the sphere
        var deg:Number          = Math.random() * 360;
        _sphere.x               = 250 * Math.cos(deg);
        _sphere.z               = 250 * Math.sin(deg);
        _sphere.y               = 80 + (Math.random() * 40);
    }

    private function init3d():void {
        //create the 3d-view
        _view               = new View3D();
        _view.width         = stage.stageWidth;
        _view.height        = stage.stageWidth;
        addChild(_view);

        //create a cub
        _view.camera.z = -400;
        _view.camera.y = 75;
        _view.camera.lookAt(new Vector3D(0, 125, 0));

        //create container
        _container = new ObjectContainer3D();
        _view.scene.addChild(_container);

        //create sphere to tap
        _sphere                 = new Mesh(new SphereGeometry(10));
        _sphere.pickingCollider = PickingColliderType.AS3_FIRST_ENCOUNTERED;
        _sphere.y               = 100;
        _sphere.z               = -250;
        _sphere.mouseEnabled = true;
        _sphere.addEventListener(MouseEvent3D.CLICK     , onClick_sphere);
        _container.addChild(_sphere);

        var stats:AwayStats = new AwayStats(_view);
        addChild(stats);

        addEventListener(Event.ENTER_FRAME, onEnter_frame);
    }
}

NOTE: i also tried different framerates, more events(Mouse_up, down etc), different pickers.

As this problem also happens with 2d moving sprites (in starling) my guess is that is might be Stage3D or AIR related, instead of starling/away3d.

Anyone has some suggestions? Maybe i'm forgetting to set something in the app.xml?


回答1:


Both framework have to deal with an event less system (Stage3D). Stage3D doesn't catch or receive any events and certianly not touch or mouse event. So both frameworks simulate those events with an internal system that catches events on the stage and try to reproduce an accurate estimation. I personally find those systems too CPU expensive and not accurate enough so my advice is to do like me, create your own system. Simply catch event on the stage and decide which of displayed objects (on Stage3D using their position) has been touched. I personally get a performance boost out of it (especially with starling) and much more accuracy.



来源:https://stackoverflow.com/questions/17103740/as3-air-mobile-click-and-or-touchevents-on-moving-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!