2

我在 ActionScript 移动项目上工作,我正在使用带有 AS3 的八哥,我尝试添加事件侦听器以检测八哥类中的 ORIENTATION_CHANGE 事件但它不会检测到更改,如果我在 AS3 类(主类)中添加事件侦听器将检测并再次调用八哥类。

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();
    }
}

}

八哥类:我添加了新的 AS 类并使其成为八哥类

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永远不会检测到方向变化可能是因为我在八哥类中检测到一个闪光事件..任何帮助

4

2 回答 2

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(); 
        }
    }
于 2013-05-12T08:55:02.197 回答
0

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

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

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

于 2013-05-08T15:32:36.720 回答