查看: 2391|回复: 0
打印 上一主题 下一主题

如何用Scoreoid为你的flash游戏创建一个排行榜

[复制链接]

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2013-6-19 11:48:52 |只看该作者 |倒序浏览
你应该知道Scoreoid一个好的特点是没有任何SDK,Scoreoid公开的Web API是基于标准的RESTful HTTP/HTTps协议。

好吧,这是新的游戏版本,前五名显示在舞台的右下角:游戏链接
你知道该怎么玩:鼠标左键启动发动机,躲避方块,尽可能走的更远并收集圆形。注意右下角的排行榜。

我创建了另一个sprite类Leaderboard,包含一个动态文本,并且对Main类做了一些修改。在脚本中,和以前版本不同的地方我加了高亮。(29行,46行,173-198行)
package {
import flash.display.Sprite;
import flash.filters.GlowFilter;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLLoader;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
public class Main extends Sprite {
private var shipFilter:GlowFilter=new GlowFilter(0x00ff00,0.8,4,4,2,3,false,false);
private var smokeFilter:GlowFilter=new GlowFilter(0xff0000,0.8,4,4,2,3,false,false);
private var fuelFilter:GlowFilter=new GlowFilter(0x00ffff,0.8,4,4,2,3,false,false);
private var rockFilter:GlowFilter=new GlowFilter(0xffffff,0.8,4,4,2,3,false,false);
private var gravity:Number=0.1;
private var thrust:Number=0.25;
private var yspeed:Number=0;
private var xspeed:Number=5;
private var distance:Number=0;
private var smokeInterval:Number=10;
private var framesPassed:Number=0;
private var fuelFrequency:Number=10;
private var gasoline:Number=500;
private var rockFrequency:Number=50;
private var engines:Boolean=false;
private var ship:Ship=new Ship();
private var score:Score=new Score();
private var leaderboardeaderboard=new Leaderboard();
private var rockCanvas:Sprite=new Sprite();
private var fuelCanvas:Sprite=new Sprite();
private var smokeCanvas:Sprite=new Sprite();
private var fuelVector:Vector.<Fuel>=new Vector.<Fuel>();
private var rockVector:Vector.<Rock>=new Vector.<Rock>();
private var smokeVector:Vector.<Smoke>=new Vector.<Smoke>();
public function Main() {
getBest();
addChild(ship);
ship.x=120;
ship.y=240;
ship.filters=new Array(shipFilter);
addChild(rockCanvas);
addChild(fuelCanvas);
addChild(smokeCanvas);
addChild(score);
addChild(leaderboard);
addEventListener(Event.ENTER_FRAME,update);
stage.addEventListener(MouseEvent.MOUSE_DOWN,engineOn);
stage.addEventListener(MouseEvent.MOUSE_UP,engineOff);
}
private function engineOn(e:MouseEvent):void {
engines=true;
framesPassed=smokeInterval;
}
private function engineOff(e:MouseEvent):void {
engines=false;
smokeInterval=10;
}
private function update(e:Event):void {
if (Math.random()*1000<fuelFrequency) {
var fuel:Fuel=new Fuel();
fuel.y=Math.random()*400+40;
fuel.x=650;
fuel.filters=new Array(fuelFilter);
fuelCanvas.addChild(fuel);
fuelVector.push(fuel);
}
if (Math.random()*1000<rockFrequency) {
var rock:Rock=new Rock();
rock.y=Math.random()*400+40;
rock.x=670;
rock.rotation=Math.random()*360;
rock.filters=new Array(rockFilter);
rockCanvas.addChild(rock);
rockVector.push(rock);
}
distance+=xspeed;
score.scoreText.text="Distance: "+distance+"\nFuel: "+gasoline;
if ((gasoline>0)&&(engines)) {
yspeed-=thrust;
smokeInterval-=0.25;
gasoline-=1;
framesPassed++;
if (smokeInterval<framesPassed) {
var smoke:Smoke=new Smoke();
smoke.x=ship.x;
smoke.y=ship.y;
smoke.filters=new Array(smokeFilter);
smokeCanvas.addChild(smoke);
smokeVector.push(smoke);
framesPassed=0;
smokeInterval-=0.01;
}
}
yspeed+=gravity;
ship.y+=yspeed;
angle=Math.atan2(yspeed,xspeed);
ship.rotation=angle*180/Math.PI;
for (var i:int=fuelVector.length-1; i>=0; i--) {
fuelVector.x-=xspeed*1.2;
var dist_x:Number=ship.x+28*Math.cos(angle)-fuelVector.x;
var dist_y:Number=ship.y+28*Math.sin(angle)-fuelVector.y;
var dist:Number = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
if (dist<10) {
gasoline+=100;
fuelCanvas.removeChild(fuelVector);
fuelVector.splice(i,1);
}
else {
if (fuelVector.x<-10) {
fuelCanvas.removeChild(fuelVector);
fuelVector.splice(i,1);
}
}
}
for (i=rockVector.length-1; i>=0; i--) {
rockVector.x-=xspeed;
if (rockVector.x<-20) {
rockCanvas.removeChild(rockVector);
rockVector.splice(i,1);
}
}
for (i=smokeVector.length-1; i>=0; i--) {
smokeVector.x-=xspeed;
smokeVector.width+=0.2;
smokeVector.height+=0.2;
smokeVector.alpha-=0.04;
if (smokeVector.alpha<0) {
smokeCanvas.removeChild(smokeVector);
smokeVector.splice(i,1);
}
}
if (ship.y>480 || ship.y<0 || rockCanvas.hitTestPoint(ship.x+28*Math.cos(angle), ship.y+28*Math.sin(angle), true) || rockCanvas.hitTestPoint(ship.x+8*Math.cos(angle+Math.PI/2), ship.y+8*Math.sin(angle+Math.PI/2), true) || rockCanvas.hitTestPoint(ship.x+8*Math.cos(angle-Math.PI/2), ship.y+8*Math.sin(angle-Math.PI/2), true)) {
postScore();
yspeed=0;
ship.y=200;
gasoline=500;
distance=0;
rockVector=new Vector.<Rock>();
removeChild(rockCanvas);
rockCanvas=new Sprite();
addChild(rockCanvas);
fuelVector=new Vector.<Fuel>();
removeChild(fuelCanvas);
fuelCanvas=new Sprite();
addChild(fuelCanvas);
smokeVector=new Vector.<Smoke>();
removeChild(smokeCanvas);
smokeCanvas=new Sprite();
addChild(smokeCanvas);
}
}
private function postScore():void {
var url:String="https://www.scoreoid.com/api/createScore";
var request:URLRequest=new URLRequest(url);
var requestVars:URLVariables = new URLVariables();
request.data=requestVars;
requestVars.api_key="ab6df379525fea384c690532df9550e38c948707";
requestVars.game_id="690bf9d6ab";
requestVars.response="XML";
requestVars.username="Demo name";
requestVars.score=distance;
request.method=URLRequestMethod.POST;
var urlLoader:URLLoader = new URLLoader();
urlLoader = new URLLoader();
urlLoader.dataFormat=URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, scorePosted);
urlLoader.load(request);
}
private function scorePosted(event:Event):void {
getBest();
}
private function loaderCompleteHandler(event:Event):void {
var xml:XML=new XML(event.target.data);
leaderboard.scoreText.text="TOP SCORES\n\n";
for (var i:Number=0; i<5; i++) {
var player:XML=xml.child("player");
leaderboard.scoreText.appendText((i+1)+") "+player.child("score").attribute("score")+"\n");
}
}
function getBest():void {
var url:String="https://www.scoreoid.com/api/getScores";
var request:URLRequest=new URLRequest(url);
var requestVars:URLVariables = new URLVariables();
request.data=requestVars;
requestVars.api_key="xxxxx";
requestVars.game_id="yyyyy";
requestVars.response="XML";
requestVars.order_by="score";
requestVars.order="desc";
requestVars.limit="5";
request.method=URLRequestMethod.POST;
var urlLoader:URLLoader = new URLLoader();
urlLoader = new URLLoader();
urlLoader.dataFormat=URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
urlLoader.load(request);
}
}
}


正如你所见,它只需使用getScores()方法,按特定顺序(requestVars.order)在期望的数据(requestVars.order_by)中检索一个号码(requestVars.limit)的游戏分数。

你会得到一个像这样的XML:
<scores>
<player username="Demo Name" email="" first_name="" last_name="" platform="">
<score score="42185" difficulty="0" platform="" leaderboard="" created="2013-06-07 18:32:42" data=""/>
</player>
<player username="Demo Name" email="" first_name="" last_name="" platform="">
<score score="42085" difficulty="0" platform="" leaderboard="" created="2013-06-05 21:14:17" data=""/>
</player>
<player username="Demo Name" email="" first_name="" last_name="" platform="">
<score score="41940" difficulty="0" platform="" leaderboard="" created="2013-06-11 00:00:33" data=""/>
</player>
<player username="Demo Name" email="" first_name="" last_name="" platform="">
<score score="39175" difficulty="0" platform="" leaderboard="" created="2013-06-06 13:10:19" data=""/>
</player>
<player username="Demo Name" email="" first_name="" last_name="" platform="">
<score score="38540" difficulty="0" platform="" leaderboard="" created="2013-06-11 00:44:44" data=""/>
</player>
</scores>


这很容易被loaderCompleteHandler函数解析。如果你不知道如何解析XML,请参阅了解AS3和XML


分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-23 09:51 , Processed in 0.094097 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部