`

How to use the Feathers ScreenNavigator component

 
阅读更多

来自:http://wiki.starling-framework.org/feathers/start

原文:http://wiki.starling-framework.org/feathers/screen-navigator

 

The ScreenNavigator class offers a powerful system for displaying screens or menus in your application and navigating between them. It supports navigation based on events (or as3-signals, if you prefer), and it can inject properties into screens as they are shown.

 

The Basics

Create a ScreenNavigator like this:

this._navigator = new ScreenNavigator();
this.addChild( this._navigator );

You may also set its width and height, but that's optional because the ScreenNavigator will automatically resize itself to fill the entire stage if you don't provide explicit dimensions.

To add a new screen to the navigator, call addScreen() and pass in a String indentifier for the screen along with a ScreenNavigatorItem:

this._navigator.addScreen( "mainMenu", new ScreenNavigatorItem( MainMenuScreen ) );

The first argument required by the ScreenNavigatorItem constructor is either a Class to instantiate or a DisplayObject that has already been instantiated. In general, your screen class, whether you instantiate it directly or allow the ScreenNavigatorItem to instantiate it, should extend Screen. This is not required, though.

In this case, MainMenuScreen is another class in our project. There are a couple more arguments that the constructor supports, and we'll look at them later.

To show a specific screen, call showScreen() and pass in the screen's identifier:

this._navigator.showScreen( "mainMenu" );

To access the currently visible screen, use the activeScreen property.

var mainMenu:MainMenuScreen = MainMenuScreen( this._navigator.activeScreen );

You can also use activeScreenID to get the String identifier for the active screen.

To make the ScreenNavigator show nothing, call clearScreen().

this._navigator.clearScreen();
 

Navigation

If a particular screen dispatches an event or a signal, the ScreenNavigator can use it to automatically navigate to another screen.

Before we get to that, let's make a couple of changes. First, let's move the main menu screen's identifier to a constant. Then, let's add a second screen.

private static const MAIN_MENU:String = "mainMenu";
private static const OPTIONS:String = "options";

The constants will help us avoid typing mistakes. Now, let's add the options screen.

this._navigator.addScreen( OPTIONS, new ScreenNavigatorItem( OptionsScreen ) );

Now that we have a second screen, let's allow the main menu to navigate to it.

 

With Signals

The second argument to the ScreenNavigatorItem constructor is an event map. We can map event types (or signals) to screen identifiers. Let's change the call where we added the main menu screen.

this._navigator.addScreen( MAIN_MENU, new ScreenNavigatorItem( MainMenuScreen,
{
	onOptions: OPTIONS
}));

Inside MainMenuScreen, there's a signal called onOptions that will automatically be detected when the ScreenNavigator reads the event map.

protected var _onOptions:Signal = new Signal( MainMenuScreen );
 
public function get onOptions():ISignal
{
	return this._onOptions;
}

The MainMenuScreen might dispatch onOptions like this:

protected function optionsButton_triggeredHandler( event:Event ):void
{
	this._onOptions.dispatch( this );
}

Now we can navigate to the options screen from the main menu screen. Let's modify the options screen item to navigate back.

 

With Events

Let's have the options screen dispatch an event instead of a signal to see how events may be used for navigation too. The ScreenNavigator automatically checks for a signal first, but if none is found, it will assume that an event is dispatched instead. Let's add a “complete” event to the event map for OptionsScreen.

this._navigator.addScreen( OPTIONS, new ScreenNavigatorItem( OptionsScreen,
{
	complete: MAIN_MENU
}));

Inside OptionsScreen, we'll dispatch an event when a button is pressed.

protected function optionsButton_triggeredHandler( event:Event ):void
{
	this.dispatchEventWith( Event.COMPLETE );
}

Now we can navigate to the options screen from the main menu and then we can navigate back.

 

With the owner property

The Screen class has an owner property that points to the ScreenNavigator. You can use this to call showScreen() directly, if you prefer not to use events or signals.

this.owner.showScreen( "anotherScreenID" );
 

Property Injection

The third argument you can pass to the ScreenNavigatorItem constructor is an initializer object. This is a set of key-value pairs that map to properties on the screen being defined. When the screen is shown, each of these properties will be passed to the screen. If you have multiple screens that need to share some data, this is a useful way to ensure that they each have access to it. For instance, you might have an OptionsData class that stores things like audio volume and other common options.

In our main app, we store the OptionsData instance.

this._optionsData = new OptionsData();

Then, when we add our OptionsScreen, we pass it the OptionsData instance in using the initializer.

this._navigator.addScreen( OPTIONS, new ScreenNavigatorItem( OptionsScreen,
{
	complete: MAIN_MENU
},
{
	optionsData: this._optionsData
}));

In OptionsScreen, we need to add a variable or a getter and setter for this data:

protected var _optionsData:OptionsData;
 
public function get optionsData():OptionsData
{
	return this._optionsData;
}
 
public function set optionsData( value:OptionsData ):void
{
	this._optionsData = value;
}

If you want to redraw when optionsData changes, you should invalidate the screen, and the draw() function will be called again:

public function set optionsData( value:OptionsData ):void
{
	this._optionsData = value;
	this.invalidate( INVALIDATION_FLAG_DATA );
}
 

Transitions

The ScreenNavigator class supports transition animations when changing screens. A number of useful transition “manager” classes are pre-defined in the feathers.motion.transitions package. Some of these track the history of screens to add a little extra context to their animations. For instance, the ScreenSlidingStackTransitionManager stores a stack of recent screens to intelligently choose the direction a screen slides in from when it is shown (either the left side or the right side).

To use the ScreenSlidingStackTransitionManager, we simply need to pass a ScreenNavigator instance to the constructor.

this._transitionManager = new ScreenSlidingStackTransitionManager( this._navigator );

Many of the transition managers expose some properties that can be used to adjust the animation. For instance, the ScreenSlidingStackTransitionManager exposes the duration of the animation and the easing function.

this._transitionManager.duration = 0.4;
this._transitionManager.ease = Transitions.EASE_OUT_BACK;
 

Transition Events

The ScreenNavigator dispatches FeathersEventType.TRANSITION_START and FeathersEventType.TRANSITION_COMPLETE events when the transition starts and ends. You might listen to FeathersEventType.TRANSITION_COMPLETE to delay the initialization of your screen until the transition has completed. For instance, you might have other animations that need to play, but once the screen is fully visible.

this.owner.addEventListener( FeathersEventType.TRANSITION_COMPLETE, owner_transitionCompleteHandler );

The event listener might look like this:

private function owner_transitionCompleteHandler( event:Event ):void
{
    this.owner.removeEventListener( FeathersEventType.TRANSITION_COMPLETE, owner_transitionCompleteHandler );
 
    // finish initialization here
}

Note that the ScreenNavigator will dispatch the FeathersEventType.TRANSITION_START and FeathersEventType.TRANSITION_COMPLETE events when a screen is being removed too. You will need to remove event listeners meant to be called only when the screen is added if you don't want them to run again when the screen is removed.

 

Advanced Functionality

The ScreenNavigatorItem event map can be used for more than just navigation. You can also call a function when an event or signal is dispatched. Let's add a new signal to the main menu that will be dispatched when a “About Our Product” button is clicked. We want it to open a website in the browser.

this._navigator.addScreen( MAIN_MENU, new ScreenNavigatorItem( MainMenuScreen,
{
	onOptions: OPTIONS,
	onHomePage: mainMenuScreen_onHomePage
}));

The function receives the signal or event listener arguments.

protected function mainMenuScreen_onHomePage( sender:MainMenuScreen ):void
{
	navigateToURL( new URLRequest( "http://www.example.com/" ), "_blank" );
}
 

Related Links

分享到:
评论

相关推荐

    strling + feathersUI

    Strling和FeathersUI是两个在游戏开发和用户界面设计中常用的库,特别是在Adobe AIR和Flash平台上。这两个库结合使用可以创建高性能、丰富的图形和交互式应用。 **Strling** Strling是一个基于ActionScript 3的2D...

    Color Imaging

    The intimate relationship between energy levels, orbital states, and electromagnetic waves helps to explain why diamonds shimmer, rubies are red, and the feathers of the Blue Jay are blue. Then, ...

    Starling-feathers API

    Starling-feathers API的文档,下载及可打开浏览。很实用

    feathers-2.0.0 Starling UI组件

    Feathers 2.0.0 是一款专为Starling框架设计的UI组件库,它旨在为Flash开发者提供一套高效、高性能的用户界面解决方案。利用Starling的硬件加速特性,Feathers能够帮助开发者创建出流畅、丰富的2D图形界面,尤其适用...

    As3 feathers

    **As3 Feathers** 是一个专门针对移动端UI设计的组件库,它在Adobe生态系统中扮演着重要的角色。这个库提供了一系列美观且功能丰富的用户界面元素,适用于开发Android和iOS等移动平台的应用程序。由于其出色的性能和...

    feathers 2.0.1

    feathers-2.0.1 This is the newest stable version of the open source Feathers user interface components for Starling Framework. Most people will want to download this version of Feathers.

    feathers文档

    feathers是一个JavaScript框架, 用于构建Web应用。。

    feathers, Starling框架的用户界面组件.zip

    feathers, Starling框架的用户界面组件 羽毛 3.4.0 -prerelease警告: 这是羽毛UI的预发布版本。 它可能包含 Bug 或者未完成的功能。 它不推荐用于生产应用,因为它被认为是潜在的不稳定的产品。 使用自己的风险。 ...

    feathers-hooks, 用于轻松授权和处理的服务方法钩子.zip

    feathers-hooks, 用于轻松授权和处理的服务方法钩子 羽毛钩重要: feathers-hooks 包含在羽毛( @feathersjs/feathers ) v3,而且不需要单独装载和配置。 面向羽毛服务方法的中间件文档有关更多详细信息,请参阅羽毛...

    Coloration strategies in peacock feathers

    ### 孔雀羽毛色彩策略研究 #### 摘要与背景 本文主要探讨了孔雀羽毛中的色彩形成机制,尤其关注其独特的结构色彩表现形式。研究人员发现,孔雀羽毛中色彩的表现并非仅通过色素来实现,而更多的是依赖于一种二维...

    feathers-mongodb:mongodb的羽毛服务

    羽毛mongodb 用于数据库适配器,使用用于。 $ npm install --save mongodb feathers-mongodb 重要提示: feathers-mongodb实现了和。 该适配器还需要一个数据库服务器。... use ( '/messages' ,

    Feathers:下一代应用的实时JavaScript框架.zip

    Feathers:下一代应用的实时JavaScript框架.zip,A framework for real-time applications and REST APIs with JavaScript and TypeScript

    feathers-swagger:将文档添加到您的FeatherJS服务中,并将其提供给Swagger UI

    此版本配置为与Swagger UI 3.x一起使用安装npm install feathers-swagger --save原料药swagger(options) 初始化模块。 服务注册之前应提供给app.configure。 const feathers = require ( '@feathersjs/feathers' ) ...

    Starling Feathers:Starling专属UI框架

    Starling Feathers是一款专为Adobe Starling框架设计的UI库,它允许开发者创建美观、高性能的2D用户界面。Starling本身是一个跨平台的游戏开发框架,基于ActionScript 3.0,利用硬件加速来实现高效的2D图形渲染,...

    vue-cli-plugin-feathers-vuex:搭建FeathersVuex样板并通过cli为@vuecli添加服务

    copy template to feathers folder -您可以自定义自己的模板,并将其用于创建新服务。 它将放置在./src/store/feathers/templates/${templateName} ,并将按以下方式调用: add service from custom templ

    aor-feathers-client, rest的rest客户端管理.zip

    aor-feathers-client, rest的rest客户端管理 Admin-on-rest的 REST客户端基于REST服务构建后台和前端管理的完美匹配。 用于使用 和 admin-on-rest 。特性GET_MANY_REFERENCEGET_MANYGET_LISTGET_ONE创

    feathers-sync:在Feathers应用程序实例之间同步服务事件

    羽毛同步 在应用程序实例之间同步服务事件编写自定义适配器执照 关于当运行Feathers应用程序的多个实例(例如,在多个Heroku Dynos上)时,服务事件( created , updated , patched , removed和任何自定义事件)...

    feathers-express-ip:将请求者的IP地址公开给您的Feathers服务

    将请求者的IP地址公开给您的Feathers服务 使用feathers-express-ip,您可以针对以下情况制定自己的解决方案: 每个IP地址的速率限制 通过IP跟踪请求 安装 npm install feathers-express-ip --save yarn add ...

    feathers-vuex-是Feathers Client和Vuex的一流集成。 它在后台实现了许多Redux最佳实践,消除了很多样板代码,并且仍然允许您轻松自定义Vuex存储。-Vue.js开发

    Feathers-Vuex Feathers-Vuex是FeathersJS和Vuex的一流集成。 它在引擎盖下实现了许多Redux最佳实践,消除了Feathers-Vuex Feathers-Vuex是FeathersJS和Vuex的一流集成。 它在后台实现了许多Redux最佳实践,通过灵活...

    feathersui-starling-sdk:使用MXML构建Feathers UI(Starling)应用程序的SDK

    Feathers SDK 4.1.1 提供了自定义的ActionScript和MXML编译器,这些编译器专门为使用和组件构建的应用程序而设计。 通过简化受支持的IDE中的项目设置并提供更适当的默认值(例如使用“直接”渲染模式),此SDK可以...

Global site tag (gtag.js) - Google Analytics