使用事件监听器检测方向变化

时间:2013-05-08 07:15:44

标签: actionscript-3 flash-builder starling-framework

我正在使用ActionScript移动项目,我使用starling和AS3,我尝试添加事件监听器来检测starling类中的ORIENTATION_CHANGE事件但是它不会检测到更改,如果我在AS3类中添加事件监听器(主类)将检测并再次调用starling类。

AS3的主要类:

package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.StageOrientationEvent;

import starling.core.Starling;

public class mainClass extends Sprite
{
    private var myStarling:Starling;
    public function mainClass()
    {
        super();
        stage.addEventListener(Event.RESIZE,onResize);
        stage.addEventListener(flash.events.StageOrientationEvent.ORIENTATION_CHANGE,shangedOr);
    }
    private function shangedOr(e:StageOrientationEvent):void
    {
        // code 
    }
    private function onResize(e:Event):void
    {
        myStarling = new Starling(Main,stage);
        myStarling.start();
    }
}

}

starling class:我添加新的AS类并使其成为starling类

package
 {
import feathers.controls.ScrollContainer;
import feathers.layout.VerticalLayout;
import feathers.themes.AeonDesktopTheme;
import flash.events.Event;
import feathers.controls.Label;
import feathers.controls.TextInput;
import flash.events.StageOrientationEvent;
import org.osmf.layout.VerticalAlign;

import starling.display.DisplayObject;
import starling.display.Sprite;
import starling.display.Stage;
import starling.events.Event;

public class starlingClass extends starling.display.Sprite
{
    private var newLayout:VerticalLayout = new VerticalLayout();
    private var container:ScrollContainer = new ScrollContainer();
    public function starlingClass()
    {
        super();
        this.addEventListener(starling.events.Event.ADDED_TO_STAGE,addToStage);
        this.addEventListener(flash.events.StageOrientationEvent.ORIENTATION_CHANGING,orientationChange);

    }
    private function addToStage(e:starling.events.Event):void
    {
        this.theme = new AeonDesktopTheme( this.stage );
        drawComponent();
    }
    private function orientationChange(evt:StageOrientationEvent):void
    {
        // orientation will not responce to change orientation.
    }
    private function drawComponent():void
    {
        var button:feathers.controls.Button = new feathers.controls.Button();
        button.label = "Click Me";
        var newLabel:Label = new Label();
        newLabel.text = "hello world";
        var txt:TextInput = new TextInput();
        var layout:VerticalLayout = new VerticalLayout();
        var container:ScrollContainer = new ScrollContainer();
        container.layout = layout;
        container.addChild(button);
        container.addChild(newLabel);
        container.addChild(txt);
        this.addChild( container );
        layout.paddingTop = 10;
        layout.paddingRight = 15;
        layout.paddingBottom = 10;
        layout.paddingLeft = 15;
        layout.gap = 5;
    }
}
}

问题:mainClass.as会检测方向更改,并会调用starlingClass.as并且不会显示任何组件。 starlingClass.as永远不会检测到方向变化,因为我在starling类中检测到了一个flash事件。 任何帮助

2 个答案:

答案 0 :(得分:1)

回答是:  我的代码中有一个错误,这个函数存在问题:

private function onResize(e:Event):void
{
    myStarling = new Starling(Main,stage);
    myStarling.start();
}

因为每个更改方向都会调用onResize方法,再次调用starling。 解决方案非常简单:

private function onResize(e:flash.events.Event):void
    {
        if (myStarling == null)
        {
            myStarling = new Starling(Main,stage);
            myStarling.start(); 
        }
    }

答案 1 :(得分:0)

您应该在nativeStage上添加事件监听器,如下所示:

this.nativeStage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING,onOrientationChanging);

因为您使用的“舞台”是Starling舞台。