A guy on this forum told me to use this:
Frame skipping on Flash
Instead of doing like thing.x -= 4;
So I did it:
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);
}
}
}
}
But for some reason, my “Seamine” do take the others x posistion? So it doesn’t start at the beginning. Example.
1 Seamine start, it have the right speed. Next Seamine spawn but this time, it doesn’t start at the “starting x value” because it starts at Seamine 1.
I do spawn the Seamines like this:
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;
}
}
}
getTimer()gets the number of milliseconds since the SWF started running, not the object. So if you’re using that amount of time to calculate the distance to move the object, it will be the same for all of your objects. Hence, all of your objects will move to the same position regardless of when the object was created.A simple fix would be to initialize lastFrame with
getTimer()instead of0.