Ios AS3每秒速度像素
这个论坛上的一个家伙告诉我使用这个: 而不是像这样做。x-=4 所以我做到了:Ios AS3每秒速度像素,ios,actionscript-3,optimization,timer,air,Ios,Actionscript 3,Optimization,Timer,Air,这个论坛上的一个家伙告诉我使用这个: 而不是像这样做。x-=4 所以我做到了: package { import flash.display.MovieClip; import flash.events.Event; import flash.events.MouseEvent; import flash.utils.getTimer; public class Seamine extends MovieClip { privat
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.getTimer;
public class Seamine extends MovieClip {
private var core:Object;
private var lastFrame:int = 0;
private var thisFrame:int;
private var pixelsPerSecond:Number = 100;
private var percentToMove:Number;
public function Seamine():void{
addEventListener(Event.ADDED_TO_STAGE, onadd);
}
private function onadd(e:Event){
stage.addChild(this);
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void{
// Get the miliseconds since the last frame
thisFrame = getTimer();
percentToMove = (thisFrame - lastFrame) / 1000;
// Save the value for next frame.
lastFrame = thisFrame;
// Update your system based on time, not frames.
this.x -= pixelsPerSecond * percentToMove;
if(this.x < 0 - this.width/2)
{
stage.removeChild(this);
removeEventListener(Event.ENTER_FRAME, loop);
removeEventListener(Event.ADDED_TO_STAGE, onadd);
}
if(GlobalVariables.GameOver == 1)
{
this.alpha -= .03;
}
if(this.alpha == 0)
{
this.alpha = 0;
stage.removeChild(this);
removeEventListener(Event.ENTER_FRAME, loop);
removeEventListener(Event.ADDED_TO_STAGE, onadd);
}
}
}
}
getTimer获取自SWF开始运行以来的毫秒数,而不是对象。因此,如果使用该时间量来计算移动对象的距离,则所有对象的距离都是相同的。因此,无论何时创建对象,所有对象都将移动到同一位置
一个简单的修复方法是使用getTimer而不是0初始化lastFrame
var tm_Seamine:Timer = new Timer (2000);
tm_Seamine.addEventListener(TimerEvent.TIMER, Spawn_Seamine);
tm_Seamine.start();
function Spawn_Seamine(e:Event){
var SM:Seamine = new Seamine();
var RandomYValue_1_Box:Number = Math.ceil(Math.random()*500);
addChild(SM);
SM.y = RandomYValue_1_Box;
SM.x = 1000;
addEventListener(Event.ENTER_FRAME, Hit_SM)
function Hit_SM (e:Event){
if(SM.hitTestPoint(Player.x,Player.y, true))
{
GlobalVariables.GameOver = 1;
}
}
}