移植flash游戏到android

来源:互联网 发布:贵金属数据下载 编辑:程序博客网 时间:2024/06/07 20:54
网上有很多现成的Flash游戏,一直想在手机上玩,虽说Android2.2支持Flash,但是大多数游戏需要键盘控制,最多的就是方向键,而手机大多数没有键盘(有键盘的好像也不行),所以实际上就没办法玩了。

现在Rexsee提供了一个很简单的解决方案,可以迅速将Flash游戏移植到Android手机上(2.2以上)。(目前Rexsee已经开源,在社区可以查看全部的开发手册、API说明与源码,www.rexsee.com) 

下面是一个例子,把经典的美羊羊卡丁车移植到了手机上,而且有两个版本,一个用虚拟键盘,一个用方向传感器。

所有代码是用HTML和JS写的,包括全部HTML,总共只有100多行。步骤如下:

  1. 准备键盘图片,这里用到了6个键,左、右、空格(跳跃)、退出、消息和Rexsee市场键,Rexsee提供了几套现成的键可以下载。将键的图片放到asset/keyboard/default/文件夹中,这样用"keyboard/default"来指定theme参数即可,否则要用放图片的文件夹的完整路径(本地SD卡或网络地址均可)来制定theme参数。文件名就是键值,后缀名是.png
  2. 将美羊羊卡丁车的swf文件放在asset/文件夹中
  3. 将横幅图片(这里是banner.png)和启动按钮图片(这里是startNow.png)放在asset/文件夹中
  4. 当然还有图标要放在res中,还要将string.xml中的首页地址设为file:///android_asset/index.html。这是唯一的一个程序文件,100多行。
  5. 这是离线版的,所以要下载Rexsee的JAR包,自己用Eclipse编译,如果嫌麻烦,可以在线编译一个首页地址指向服务器某个网页地址,然后将下面的index.html的代码放在这个文件中(有些路径要改,比如图片什么的),如果也想离线玩,可以在首次载入后将swf文件和图片自己或者用rexseeClient下载到本地。当然也可以不下载,就在线玩。
标签:Rexsee Android SDK

代码片段(3)[全屏查看所有代码]

1. [代码]先看一下res/values/string.xml     

1<?xml version="1.0" encoding="utf-8"?>
2<resources>
3<string name="app_name">美羊羊卡丁车</string>
4<string name="app_home">file:///android_asset/index.html</string>
5<string name="rexsee_home">http://www.rexsee.com/flash/index.php</string>
6</resources>

2. [代码]再看一下AndroidManifest.xml     

01<?xml version="1.0" encoding="utf-8"?>
02<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="MeiYangYangKaDingChe.rexsee" android:versionCode="1"android:versionName="1.0">
03<supports-screens android:largeScreens="true" android:normalScreens="true"android:smallScreens="true" android:anyDensity="true" />
04<application android:icon="@drawable/icon" android:label="@string/app_name"android:theme="@android:style/Theme.NoTitleBar">
05<activity android:name="rexsee.activity.RexseeActivity"android:configChanges="orientation|keyboardHidden" android:launchMode="singleTop">
06<intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter>
07 
08</activity>
09</application>
10<uses-permission android:name="android.permission.INTERNET"/>
11<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
12</manifest>

3. [代码]最后是asset/index.html的源代码     

0011:  <HTML>
0022:  <HEAD>
0033:      <TITLE>美羊羊卡丁车</TITLE>
0044:      <META http-equiv=Content-Type content=text/html charset=GB2312> 
0055:      <META http-equiv=Expires content=0>
0066:      <META http-equiv=Cache-Control content=no-cache>
0077:      <META http-equiv=Pragma content=no-cache>
0088:      <SCRIPT type=text/javascript>
0099:         
01010:         function startFlashGame(para){
01111:             //开始Flash游戏
01212:             rexseeScreen.setScreenOrientation(para.orientation);     //将屏幕方向设为水平
01313:             if ( para.sensor ) {
01414:                 rexseeOrientation.setRate('fastest');                //将传感器敏感程度设为最高
01515:                 rexseeOrientation.start();                            //启动传感器
01616:             } else {
01717:                 rexseeOrientation.setRate('normal');                //将传感器敏感程度设为正常
01818:                 rexseeOrientation.stop();                            //停止传感器
01919:             }
02020:             rexseeFlash.clearKeyboard();                            //清空键盘
02121:             if ( para.leftKeyboard ) rexseeFlash.setLeftKeyboard(para.leftKeys,para.leftKeyTheme,para.leftKeyboardStyle,para.leftKeyRowStyle,para.leftKeyStyle,para.leftKeyPressedStyle);           //设置左键盘
02222:             if ( para.rightKeyboard ) rexseeFlash.setRightKeyboard(para.rightKeys,para.rightKeyTheme,para.rightKeyboardStyle,para.rightKeyRowStyle,para.rightKeyStyle,para.rightKeyPressedStyle);   //设置右键盘
02323:             rexseeFlash.setSwfStyle(para.swfStyle);            //设置Flash游戏区域的样式
02424:             rexseeFlash.show(para.swf,para.dialogStyle);    //设置对话框样式并启动对话框
02525:         }
02626:         var paraKeyboardVersion = {
02727:                 orientation:             "landscape",        //屏幕方向
02828:                 sensor:                 false,                //是否启用方向传感器
02929:                 //设置左边键盘
03030:                 leftKeyboard:             false,                //是否启用左键盘
03131:                 leftKeys:                 "",                    //左键盘的按键,数字序列,同一行按键使用逗号分隔,不同行使用分号分隔,Rexsee自定义按键包括-2,-3,-4,-5,其余按键请参考http://developer.android.com/reference/android/view/KeyEvent.html
03232:                 leftKeyTheme:             "",                    //左键盘的样式,字符串,应在asset目录下创建以该字符串命名的文件夹,然后将按键图片(键值.png)放在该文件夹
03333:                 leftKeyboardStyle:         "",                    //左键盘的样式
03434:                 leftKeyRowStyle:         "",                    //左键盘中每一行的样式
03535:                 leftKeyStyle:             "",                    //左键盘中每个按键的样式
03636:                 leftKeyPressedStyle:         "",                //左键盘中每个按键被按下时的样式
03737:                 //设置右边键盘,参看上面左键盘说明
03838:                 rightKeyboard:             true,
03939:                 rightKeys:                 "-1,-5;-2,62;21,22",
04040:                 rightKeyTheme:             "keyboard/default",
04141:                 rightKeyboardStyle:     "border-left-width:5px;border-left-color:#aaaaaa+#000000/2;background-color:#000000;",
04242:                 rightKeyRowStyle:         "",
04343:                 rightKeyStyle:             "",
04444:                 rightKeyPressedStyle:    "",
04545:                 //设置Flash区域的样式
04646:                 swfStyle:                "",
04747:                 //设置对话框的样式
04848:                 dialogStyle:             "",
04949:                 //设置Flash游戏的地址
05050:                 swf:                     "game.swf"
05151:         };
05252:         var paraSensorVersion = {
05353:                 orientation:             "landscape",
05454:                 sensor:                 true,
05555:                 leftKeyboard:             false,
05656:                 leftKeys:                 "",
05757:                 leftKeyTheme:             "",
05858:                 leftKeyboardStyle:         "",
05959:                 leftKeyRowStyle:         "",
06060:                 leftKeyStyle:             "",
06161:                 leftKeyPressedStyle:         "",
06262:                 rightKeyboard:             true,
06363:                 rightKeys:                 "-1;-5;-2;62",
06464:                 rightKeyTheme:             "keyboard/default",
06565:                 rightKeyboardStyle:     "border-left-width:5px;border-left-color:#aaaaaa+#000000/2;background-color:#000000;",
06666:                 rightKeyRowStyle:         "",
06767:                 rightKeyStyle:             "",
06868:                 rightKeyPressedStyle:    "",
06969:                 swfStyle:                "",
07070:                 dialogStyle:             "",
07171:                 swf:                     "game.swf"
07272:         };
07373:
07474:         window.onRexseeReady = function(){
07575:             //在页面装载完毕时运行
07676:             if ( rexseeBuild.getSdk()<8) {
07777:                 alert("对不起,您的手机软件版本太低,不能运行该游戏,请使用Android2.2以上版本运行该游。");
07878:                 rexseeApplication.quit();
07979:             }
08080:             if (!rexseeFlash.isReady()){
08181:                 rexseeDialog.confirm("Adobe Flash Player", "Adobe Flash Player没有安装,无法运行该游戏。是否现在安装?","现在安装","退出程序","javascript:rexseeBrowser.open('http://www.rexsee.com/flash/flashplayer/download.php');rexseeApplication.exit();","javascript:rexseeApplication.exit();");
08282:             }
08383:             rexseeScreen.setScreenAlwaysOn(true);                  //总是点亮屏幕
08484:             rexseeScreen.setScreenOrientation('portrait');      //将将屏幕方向设为垂直
08585:             rexseeStatusBar.setStyle("visibility:hidden;");     //隐藏状态栏
08686:                     }
08787:         window.onAjaxReturned = function(ajaxUrl,response){
08888:             response = unescape(response);
08989:             if ( ajaxUrl == headerUrl) document.getElementById("header").innerHTML = response;
09090:             else if ( ajaxUrl == footerUrl) document.getElementById("footer").innerHTML = response;
09191:             else if ( ajaxUrl == jsUrl) eval(response);
09292:         }
09393:         window.onFlashDialogDismissed = function(){
09494:             //在退出Flash游戏返页面时运行
09595:             rexseeScreen.setScreenOrientation('portrait');        //将屏幕方向设为垂直
09696:         }
09797:         window.onbeforeunload = function(){
09898:             //在退出页面时运行
09999:             rexseeScreen.setScreenOrientation('auto');            //将屏幕方向设为自动
100100:                rexseeOrientation.stop();                            //停止方向传感器
101101:            }
102102:           
103103:            var version = "";
104104:            //Rexsee新增按键的回调函数
105105:            window.onKeyboardHelpPressed = function(){
106106:                //对应键值:-2
107107:                if ( version == "keyboard" ) alert("左右键向左或向右,空格键跳跃。");
108108:                else if ( version == "sensor" ) alert("左右倾斜手机向左或向右,虚拟键盘的空格键跳跃。");
109109:            }
110110:            window.onKeyboardQaPressed = function(){
111111:                //对应键值:-3
112112:                alert("QA pressed.");
113113:            }
114114:            window.onKeyboardHomePressed = function(){
115115:                //对应键值:-4
116116:                if ( window.confirm("要退出游戏吗?") ) {
117117:                    rexseeFlash.hide();
118118:                    rexseeApplication.quit();
119119:                }
120120:            }
121121:            window.onKeyboardCancelPressed = function(){
122122:                //对应键值:-5
123123:                if ( window.confirm("要退出游戏吗?") ) {
124124:                    rexseeFlash.hide();
125125:                }
126126:            }
127127:           
128128:            window.onOrientationChanged = function(){
129129:                //如果启用方向传感器,在回调函数中向Flash游戏发送键值
130130:                var x = Math.round(rexseeOrientation.getLastKnownX());
131131:                var y = Math.round(rexseeOrientation.getLastKnownY());
132132:                var z = Math.round(rexseeOrientation.getLastKnownZ());
133133:                if ( y > 10 ) {
134134:                    rexseeFlash.keyDown(21);
135135:                } else if ( y<-10){
136136:                    rexseeFlash.keyDown(22);
137137:                } else {
138138:                    rexseeFlash.keyUp(21);
139139:                    rexseeFlash.keyUp(22);
140140:                }
141141:            }
142142:        </SCRIPT>
143143:        <STYLE>
144144:            body { margin:0px; background-color:#eeeeee;}
145145:            .imageButton { width:100px;height:100px; border:none; margin: 0 0 0 10; }
146146:            .section {border:1px solid;border-color:#f7f7f7 #7f9db5 #7f9db5 #f7f7f7;background-color:#ffffff;padding:15px;}
147147:            .gameTitle { font-size:24px;font-weight:bold; color:#F01D17;margin:15 0 15 0;}
148148:            .gameIntro {}
149149:            .gameVersion { font-size:20px;font-weight:bold; color:#F01D17;margin:10 0 15 0;}
150150:            .gameGuide { margin: 0 0 10 0; }
151151:            #copyright {background-color:#444444;padding:5px;color:white;font-size:10px;}
152152:        </STYLE>
153153:    </HEAD>
154154:    <BODY>
155155:    <a href="http://www.rexsee.com/flash/index.php"><img src='banner.png'width=100% border=0></a>
156156:   
157157:    <div class=section>
158158:        <div class=gameTitle>美羊羊卡丁车</div>
159159:        <div class=gameIntro>
160160:            可爱的美羊羊是赛车高手哦,来体验下美羊羊马路杀手的感觉吧,美羊羊开车的速度可是会越来越快哦,小心呀!
161161:            <br><br>游戏目标:帮助美羊羊躲避路障,安全抵达终点。
162162:            <br>&nbsp;
163163:        </div>
164164:    </div>
165165:    <div class=section>
166166:        <table width=100%><tr>
167167:            <td valign=top>
168168:                <div class=gameVersion>虚拟键盘版</div>
169169:                <div class=gameGuide>左右键向左或向右,空格键跳跃。</div>
170170:            </td>
171171:            <td><img src="startNow.png"onclick="version='keyboard';startFlashGame(paraKeyboardVersion);" class=imageButton></td>
172172:        </tr></table>
173173:    </div>
174174:    <div class=section>
175175:        <table width=100%><tr>
176176:            <td valign=top>
177177:                <div class=gameVersion>重力感应版</div>
178178:                <div class=gameGuide>左右倾斜手机向左或向右,虚拟键盘的空格键跳跃。</div>
179179:            </td>
180180:            <td><img src="startNow.png"onclick="version='sensor';startFlashGame(paraSensorVersion);" class=imageButton></td>
181181:        </tr></table>
182182:    </div>
183183:    </BODY>
184184:    </HTML>
0 0