0

嗨,我试图在我的 as3 上实现一个简单的按钮。但这不起作用。

我刚刚将 bouton1 添加到我的场景中,但是当我点击它时没有任何反应(跟踪功能不显示我的消息

这是我的代码

package {

import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.display.SimpleButton;






//import org.papervision3d.core.animation.clip.AnimationClip3D;
import org.papervision3d.objects.parsers.DAE;
//import org.papervision3d.objects.parsers.Max3DS;
//import org.papervision3d.objects.parsers.KMZ;
import org.papervision3d.view.BasicView;    
import org.papervision3d.events.FileLoadEvent;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.view.BasicView;

//added color to objects
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.utils.MaterialsList;

import org.papervision3d.core.math.Matrix3D;
import org.papervision3d.core.math.Number3D;
import org.papervision3d.lights.PointLight3D;

public class ExternalModelsExample extends BasicView

{
    private static const FORWARD:Number3D = new Number3D(0, 0, 1);
    private var model:DisplayObject3D;
    private var model2:DisplayObject3D;
    private var model3:DisplayObject3D;/*
    private var model4:DisplayObject3D;
    private var model5:DisplayObject3D;
    private var model6:DisplayObject3D;
    private var model7:DisplayObject3D;
    private var model8:DisplayObject3D;
    private var model9:DisplayObject3D;
    private var model10:DisplayObject3D;*/
    private var rotX:Number = 0.2;
    private var rotY:Number = 0.2;
    private var camPitch:Number = 90;
    private var camYaw:Number = 270;
    private var easeOut:Number = 0.1;


    // definition de la propriété couleur
    private var color:ColorMaterial;
    private var color2:ColorMaterial;
    private var color3:ColorMaterial;/*
    private var color4:ColorMaterial;
    private var color5:ColorMaterial;
    private var color6:ColorMaterial;
    private var color7:ColorMaterial;
    private var color8:ColorMaterial;
    private var color9:ColorMaterial;
    private var color10:ColorMaterial;*/

    private var previousMousePoint:Point = new Point();
    private var isMouseDown:Boolean = false;

    var bouton1:SimpleButton = new SimpleButton();




    public function ExternalModelsExample()
    {   
        stage.frameRate = 40;
        init();
        startRendering();
        addChild(bouton1);
        bouton1.addEventListener(MouseEvent.CLICK, test);
        stage.addEventListener(MouseEvent.MOUSE_DOWN, stage_mouseDownHandler);
        stage.addEventListener(MouseEvent.MOUSE_UP, stage_mouseUpHandler);




    }
    public function test(){
        trace("test");
    }


    private function init():void
    {




        //Declaration color material
        color = new ColorMaterial(0x65b5ff);
        color2 = new ColorMaterial(0xffffff);
        color3 = new ColorMaterial(0x757575);/*
        color4 = new ColorMaterial(0x65b5ff);
        color5 = new ColorMaterial(0xffffff);
        color6 = new ColorMaterial(0x874630);
        color7 = new ColorMaterial(0xbe0da7);
        color8 = new ColorMaterial(0x3107bb);
        color9 = new ColorMaterial(0xff4448);
        color10 = new ColorMaterial(0xc7c7c7);*/


        //COLLADA Utah Teapot
        //Add import on top: import org.papervision3d.objects.parsers.DAE;
        model = new DAE();
        model.addEventListener(FileLoadEvent.LOAD_COMPLETE,modelLoaded);
        DAE(model).load("assets/Z1.DAE", new MaterialsList ( { all:color } ));
        model2 = new DAE();
        model2.addEventListener(FileLoadEvent.LOAD_COMPLETE,modelLoaded);
        DAE(model2).load("assets/Z2.DAE", new MaterialsList ( { all:color2 } ));
        model3 = new DAE();
        model3.addEventListener(FileLoadEvent.LOAD_COMPLETE,modelLoaded);
        DAE(model3).load("assets/Z3.DAE", new MaterialsList ( { all:color3 } ));
        /*model = new DAE();
        model.addEventListener(FileLoadEvent.LOAD_COMPLETE,modelLoaded);
        DAE(model).load("assets/Z4.DAE", new MaterialsList ( { all:color } ));
        model2 = new DAE();
        model2.addEventListener(FileLoadEvent.LOAD_COMPLETE,modelLoaded);
        DAE(model2).load("assets/Z5.DAE", new MaterialsList ( { all:color2 } ));
        model3 = new DAE();
        model3.addEventListener(FileLoadEvent.LOAD_COMPLETE,modelLoaded);
        DAE(model3).load("assets/Z6.DAE", new MaterialsList ( { all:color3 } ));
        model = new DAE();
        model.addEventListener(FileLoadEvent.LOAD_COMPLETE,modelLoaded);
        DAE(model).load("assets/Z7.DAE", new MaterialsList ( { all:color } ));
        model2 = new DAE();
        model2.addEventListener(FileLoadEvent.LOAD_COMPLETE,modelLoaded);
        DAE(model2).load("assets/Z9.DAE", new MaterialsList ( { all:color2 } ));
        model3 = new DAE();
        model3.addEventListener(FileLoadEvent.LOAD_COMPLETE,modelLoaded);
        DAE(model3).load("assets/Z12-cockpit.DAE", new MaterialsList ( { all:color3 } ));
        model = new DAE();
        model.addEventListener(FileLoadEvent.LOAD_COMPLETE,modelLoaded);
        DAE(model).load("assets/Z13-barres.DAE", new MaterialsList ( { all:color } ));*/



    }

    private function modelLoaded(e:FileLoadEvent):void
    {

        scene.addChild(model);
        scene.addChild(model2);
        scene.addChild(model3);/*
        scene.addChild(model4);
        scene.addChild(model5);
        scene.addChild(model6);
        scene.addChild(model7);
        scene.addChild(model8);
        scene.addChild(model9);
        scene.addChild(model10);*/

    }




    private function stage_mouseDownHandler(event:MouseEvent):void
    {
        isMouseDown = true;
    }

    private function stage_mouseUpHandler(event:MouseEvent):void
    {
        isMouseDown = false;    
    }


    override protected function onRenderTick(e:Event=null):void
    {   

        var currentMousePoint:Point = new Point(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY);

        if(isMouseDown)
        {
            var difference:Point = currentMousePoint.subtract(previousMousePoint);
            var vector:Number3D = new Number3D(difference.x, difference.y, 0);
            var rotationAxis:Number3D = Number3D.cross(vector, FORWARD);
            rotationAxis.normalize();
            var distance:Number = Point.distance(currentMousePoint, previousMousePoint);
            var rotationMatrix:Matrix3D = Matrix3D.rotationMatrix(rotationAxis.x, -rotationAxis.y, rotationAxis.z, distance/250);

            model.transform.calculateMultiply3x3(rotationMatrix, model.transform);
            model2.transform.calculateMultiply3x3(rotationMatrix, model2.transform);
            model3.transform.calculateMultiply3x3(rotationMatrix, model2.transform);
        }

        previousMousePoint = currentMousePoint;
        super.onRenderTick();
    }

}

}

你知道为什么我的跟踪(“测试”)不起作用吗?

与对象拖动有冲突吗?

flash 不显示错误

谢谢

4

2 回答 2

2

SimpleButton 对象比“添加和忘记”更复杂,您需要填充其属性,这通常在设计时完成。有四个 DisplayObject 类型的对象,即upState, overState, downState, hitTestState确定该按钮的形状和内容。为了使按钮可见和可点击,您需要指定 4 个 Sprite(或其他 DisplayObjects)作为 SimpleButton 构造函数的参数。该hitTestState对象用于捕获事件,并且始终是不可见的,因此您只需要在其上绘制正确的形状即可。其他状态分别在按钮向上、鼠标悬停在按钮上以及鼠标在按钮上按下时可见。

var upSprite:Sprite=new Sprite();
upSprite.graphics.lineStyle(2,0,1);
upSprite.graphics.drawRect(0,0,50,20);
var hitSprite:Shape=new Shape();
hitSprite.graphics.beginFill(0,1);
hitSprite.graphics.drawRect(3,3,44,14); // a bit smaller rect to make the button narrow
var bouton1:SimpleButton=new SimpleButton(upSprite,upSprite,upSprite,hitSprite);
于 2012-09-28T05:17:15.490 回答
0
  1. 尝试 mouse_down 上的跟踪操作而不是单击。

  2. 您在最后几行中提到了 if(isMouseDown) 。如果这是错误的,我很抱歉,但你必须说 if(isMouseDown) = true

于 2012-09-28T01:59:28.263 回答