How can I make my device vibrate?

2019-04-23 18:09发布

I'm making a game in Flash for Android with AS3. I want the user to know that he pressed a button by making the device vibrate for a brief second. Can someone explain to me how I can make this happen? Do I need to import a specific class and what should the code look like?

Thanks in advance!

2条回答
ら.Afraid
2楼-- · 2019-04-23 18:36

Thanks for the answers. They made me research a bit further into this subject. What I found was this:

Before you can use:

var vibe:Vibration;
if(Vibration.isSupported){
     vibe = new Vibration();
     vibe.vibrate(500);
}

You have to go to "Edit ActionScript Settings" > "Libary Path" > "Browse to SWC file"

There you have to put these 2 file in:

VibrationActionScriptLibary.swc - You can get with this zip file

com.adobe.extensions.Vibration.swc - In the same zip file and directory. You have to change the .ane to .swc

Next you have to import com.adobe.nativeExtensions.Vibration; Just put that line with the other imports.

This was all you have to do in Flash. (Besides coding the actual game...)

Next up, open the myApp-app.xml. In the xml, scroll-up and check if the link to ns.adobe.com ends with 3.0 (or higher). If not, change it to 3.0. Adobe AIR 3.0 or higher is required for native extensions.

Scroll further down to the part where you see <initialWindow>. After the closing tag you add :

<extensions>
    <extensionID>com.adobe.Vibration</extensionID>
</extensions>

If you scroll further down you should see something like <android>. In the manifestAdditions you have to add a permission for the device to make it actually allow that it can use the vibrate function.

If there is not yet a <![CDATA[manifest> line there, you have to add it. If you added it/or if it's already there you have to add in the permission. This is done with android:name="android.permission.VIBRATE" In the end you want to make sure it looks something like this:

<![CDATA[<manifest>
    <uses-permission android:name="android.permission.VIBRATE"/>
</manifest>]]>

After this you can save and close the xml file.

After you have done all this your .fla may fail to publish in Flash since Flash doesn't support the stuff we have just adjusted. However, we can still publish the .fla with CMD (for Windows). This is also where my support for Mac users end, sadly. In cmd you browse to your project folder. My project folder is on my desktop, so I typed cd desktop, cd games, cd tilt android game. After I browsed to my folder I typed this command line...

AdobeAIRSDK\bin\adt -package -target apk -storetyp pkcs12 -keystore YOUR_LICENCE.p12 myApp.apk myapp-app.xml myapp.swf -extdir extensionDir

Make sure you have the AdobeAIRSDK folder in your project folder. If all the above (with cmd) is a bit vague go here and watch his video and his video about how to compile an ANE app with the command line for your OS. (That's what I did in the first place.)

If you have and icon for your app, make sure they are in a folder named "icons" in your project folder. If your icons are in such a folder just paste the folder name at the end of the command line.

The app should publish in your projects folder.

If you have any questions about this, make sure you watch the video I linked.

查看更多
Summer. ? 凉城
3楼-- · 2019-04-23 18:56

To use the Vibration extension, an AIR application does the following:

Checks if the extension is supported by calling isSupported. Causes the device to vibrate by calling vibrate(), specifying the duration of the vibration in milliseconds as a parameter.

var vibe:Vibration;
if (Vibration.isSupported)
{
    vibe = new Vibration();
    vibe.vibrate(2000);
}

Android applications For an Android application, include the Vibration permission in your application descriptor file:

Android Permission : <uses-permission android:name="android.permission.VIBRATE"/>

Reference : http://www.adobe.com/devnet/air/native-extensions-for-air/extensions/vibration.html

查看更多
登录 后发表回答