Alert.png

Notice: This page contains information for the legacy Phidget21 Library.

Phidget21 is out of support. Bugfixes may be considered on a case by case basis.

Phidget21 does not support VINT Phidgets, or new USB Phidgets released after 2020. We maintain a selection of legacy devices for sale that are supported in Phidget21.

We recommend that new projects be developed against the Phidget22 Library.


Click on the 2phidget22.jpg button in the menu bar to go to the Phidget22 version of this page.

Alert.png

Language - Flash AS3: Difference between revisions

From Phidgets Legacy Support
Line 85: Line 85:
====Use Our Examples====
====Use Our Examples====


This section will assume that the device is plugged into the host computer, and the following steps are intended for a developer role.
This section will assume that the device is plugged into the host computer, and that the development computer Flash Professional installed.


As the Flash ActionScript library only supports communication with Phidgets through the PhidgetWebService, begin by [[starting the PhidgetWebService]] on the host computer with the default port(5001).
As the Flash ActionScript library only supports communication with Phidgets through the PhidgetWebService, begin by [[starting the PhidgetWebService]] on the host computer with the default port(5001).


To run the examples, on the development computer, you download the [http://www.phidgets.com/downloads/examples/Flash_2.1.8.20120131.zip examples] and unpack them into a folder.  Here, you will find example programs for all devices.  If you aren't sure what the software example for your device is called, check the software object listed in the [[Device List | Getting Started Guide for your Device]].
To run the examples on a development computer, download the [http://www.phidgets.com/downloads/examples/Flash_2.1.8.20120131.zip examples] and unpack them into a folder.  Here, you will find example programs for all devices.  If you aren't sure what the software example for your device is called, check the software object listed in the [[Device List | Getting Started Guide for your Device]].


When you have found your example, open that {{Code|.fla}} file in the Adobe Professional Flash environment. The only thing left to do is to run the examples! Click on Control → Test Movie.  
When you have found your example, open that {{Code|.fla}} file in the Adobe Professional Flash environment. The only thing left to do is to run the examples! Click on Control → Test Movie.  

Revision as of 22:37, 22 February 2012

Flash AS3Flash, developed by Adobe is used to build and deploy dynamic multimedia applications to the Internet.

Introduction

If this is your first time working with a Phidget, we suggest starting with the Getting Started page for your specific device. This can be found in the user guide for your device. That page will walk you through installing drivers and libraries for your operating system, and will then bring you back here to use Flash ActionScript specifically.

Flash ActionScript is capable of using Phidgets only over the PhidgetWebService, and it is unlike the majority of the other programming languages we support where the device can be used without the PhidgetWebservice. The complete Phidget API, including events are supported. We also provide example code in Flash ActionScript for all Phidget devices.

Flash ActionScript can be developed with Windows and OS X.

Only ActionScript 3 is supported. Interaction with Phidgets is made possible as the library uses web sockets to communicate with Phidgets over the PhidgetWebService.

You can compare Flash ActionScript with our other supported languages.

Quick Downloads

Just need the Flash ActionScript documentation, drivers, libraries, and examples? Here they are:

Documentation

Example Code

Libraries and Drivers


Getting started with Flash ActionScript

If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:

  • Make sure your libraries are properly linked
  • Go from source code to a test application as quickly as possible
  • Ensure your Phidget is hooked up properly

Instructions are divided up by operating system. Choose:

Windows(2000/XP/Vista/7)

Description of Library

Unlike the majority of the programming languages we support where applications can be used without the PhidgetWebservice, Flash can only control Phidgets over the PhidgetWebservice using web sockets. The computer that has the device/s connected to it is called the host computer. The host computer must already have the PhidgetWebservice started in order for other client computers to connect to the devices attached to the host computer. Note that the host computer can also connect to the devices that are physically connected to it. Please see the PhidgetWebservice section for more information.

There are potentially three roles that a computer can belong to: host, development and an end user computer.

  • Host: The computer that the device is connected to, and can broadcast device information to any computer over the network.
  • Development: The computer that is used to develop Flash applications.
  • End user: The computer that is used to run the compiled flash application(i.e., .swf).

It is possible for a computer to belong to any two or all three roles.

Depending on which role a computer belongs to, it will need different Phidget libraries.

Flash ActionScript on Windows depend on the following files and folders. The installers in the Quick Downloads section put only the phidget21.dll and PhidgetWebservice21.exe into your system. You will need to manually put the com folder into your system.

  • phidget21.dll contains the actual Phidget library, which is used at run-time. This needs to be installed on the computer hosting the Phidget. By default, it is placed in C:\Windows\System32.
  • PhidgetWebservice21.exe allows for controlling Phidgets remotely across the network. This needs to be installed on the computer hosting the Phidget.
  • com folder is the Phidget ActionScript library. This computer that is used for Flash development will need this folder. It is to be placed in the same directory as your project root.

If you do not want to use our installer, you can download the phidget21.dll and manually install them where you want; refer to our Manual Installation Instructions.

The host computer requires the phidget21.dll and PhidgetWebservice21.exe. These files are automatically installed if you are using the installer. The development computer only requires the com folder. The end user computer does not need any mandatory files to run a compiled Flash application. If a computer acts as a host, development and end user computer, it will need the phidget21.dll, PhidgetWebservice21.exe and com folder.

Here is a table summarizing what files/folders are needed for each computer role:

Computer Role phidget21.dll Phidget21WebService.dll com folder
Host Checkmark.png Checkmark.png
Development Checkmark.png
End User

Flash Professional

Use Our Examples

This section will assume that the device is plugged into the host computer, and that the development computer Flash Professional installed.

As the Flash ActionScript library only supports communication with Phidgets through the PhidgetWebService, begin by starting the PhidgetWebService on the host computer with the default port(5001).

To run the examples on a development computer, download the examples and unpack them into a folder. Here, you will find example programs for all devices. If you aren't sure what the software example for your device is called, check the software object listed in the Getting Started Guide for your Device.

When you have found your example, open that .fla file in the Adobe Professional Flash environment. The only thing left to do is to run the examples! Click on Control → Test Movie.

Run

Once you have the Flash ActionScript examples running, we have a teaching section below to help you follow them.

You may also run the examples by navigating to Control → Test Scene. If you are running the examples with Debug → Debug Movie, you will have to change the Flash Global Security Settings in order for the example to run. More information will be provided about the Flash Global Security Settings in the Running Compiled Code section.

Write Your Own Code

When you are building a project from scratch, or adding Phidget function calls to an existing project, you'll need to configure your development environment to properly link the Phidget ActionScript library. To begin:

1. Place a copy of the com folder in the root directory of your Flash project.

2. Generate a new ActionScript 3 Flash file.

New Project

3. Then, in your code, you will need to include the Phidget ActionScript library. Navigate to Window → Actions to bring up the Actions window and enter in the following:

import com.phidgets.*;
import com.phidgets.events.*;

The project now has access to the Phidget function calls and you are ready to begin coding.

The same teaching section which describes the examples also has further resources for programming your Phidget.

Running Compiled Code

Running a compiled .swf application on an end user computer will prompt the Flash player to display a dialog box mentioning that the application will block all communications with the Internet.

Flash Player Security Warning

1. Click on the Settings button to bring up the Flash Global Security Settings Manager in your default web browser. Alternatively, you can access the manager with the following URL: http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html.

Flash Global Security Settings

2. In the Global Security Settings tab, navigate to Edit locations ...Add locations.

Browse for File/Folder

3. Then, browse and add the application or the folder containing the application.

Added file to the trusted locations

This will allow the Flash Player to allow the application to accept any communication with the Internet.

OS X

Flash ActionScript has excellent support on OS X over the PhidgetWebService.

The first step in using Flash ActionScript on Mac is to install...

Follow the Examples

By following the instructions for your operating system and compiler above, you probably now have a working example and want to understand it better so you can change it to do what you want. This teaching section has resources for you to learn from the examples and write your own.

Next comes our ActionScript API information, with syntax for all of our functions:

  • ActionScript API (This is the complete set of functions you have available for all Phidgets)
  • Device Specific APIs - The one for your Phidget can be found in its user guide.

To learn the details behind opening, configuring, using, and closing your Phidget, try the General Phidget Programming page. That page also describes using the Phidget in an event-driven manner and in a traditional manner, both of which are available in C/C++.

Example Flow

The Hello World example has this general structure so you can follow along. We also have an in-depth general introduction to writing Phidget code (like open, read data, etc), as well as the {{{3}}} for specific syntax:

// ----- Event and Other Functions -----

Create any Language-Specific Functions (exception handling)

Create General Attach, Detach, and Error Handling Functions:

On attach: Print Hello Message
On detach: Print Goodbye Message

 

In flash ActionScript, you can name these event functions whatever you like. You will then pass them as function pointers to the Phidget library below in the Main Code section. This hooks them into the actual events when they occur.
In the example code, the event functions common to all Phidgets are called things like AttachHandler() and DetachHandler(), etc.

Some event functions will be specific to each device, like when a tag is read on an RFID board, or when a sensor value changes on an Interface Kit. Other functions are given in the examples to show you more detail on using your Phidget. For example, DeviceInitialize() will show what needs to be set up for your Phidget before using it.

// ----- Main Code -----

Create Manager Software Object
Hook Event Functions created above to Device
Open Device

Wait for 'Enter' key character input
Handle on-going attach and detach events
Print Hello and Goodbye messages
Exit upon input

Close Device

Delete Device

 

Creating a Phidget software object in Flash ActionScript is specific to the Phidget. For a Phidget Spatial, for example, this would involve creating a PhidgetSpatial object. The examples show how to do this and other API functions.

Code Snippets

a single Phidget to be opened by multiple applications - something that cannot be done with the regular interface

Common Problems and Solutions/Workarounds

Problem: My compiled application is experiencing the following security error upon launching: "SecurityError: Error #2010: Local-with-filesystem SWF files are not permitted to use sockets".

Socket Error

Solution: Please see Running Compiled Code section for steps in resolving this issue.