-
-
Save anonymous/6d1c78d21ae0859f8b8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import openfl.display.Sprite; | |
import openfl.Lib; | |
import openfl.events.Event; | |
class Main extends Sprite { | |
private var _sprites: Array<Sprite> = []; | |
private var _previousTime: Int = 0; | |
public function new() { | |
super(); | |
if (Lib.current.stage != null) { | |
init(); | |
} else { | |
addEventListener(Event.ADDED_TO_STAGE, handleAddedToStageEvent); | |
} | |
} | |
// | |
private function init() { | |
stage.frameRate = 60; | |
addSprites(); | |
addEventListeners(); | |
} | |
private function addSprites() { | |
var radius = 50.0; | |
for (spriteIndex in 0 ... 5) { | |
var circleSprite = getCircleSprite(radius); | |
circleSprite.y = spriteIndex * radius; | |
stage.addChild(circleSprite); | |
_sprites.push(circleSprite); | |
} | |
} | |
private function addEventListeners() { | |
addEventListener(Event.ENTER_FRAME, handleEnterFrameEvent); | |
} | |
private function getCircleSprite(radius: Float): Sprite { | |
var circleSprite = new Sprite(); | |
circleSprite.graphics.beginFill(0xC0C0C0); | |
circleSprite.graphics.drawEllipse(0.0, 0.0, radius, radius); | |
circleSprite.graphics.endFill(); | |
return circleSprite; | |
} | |
private function moveSprites(secondsSinceLastFrame: Float) { | |
var speed = 20.0; | |
for (sprite in _sprites) { | |
sprite.x += speed * secondsSinceLastFrame; | |
} | |
} | |
// event handlers | |
private function handleAddedToStageEvent(event: Event) { | |
removeEventListener(Event.ADDED_TO_STAGE, handleAddedToStageEvent); | |
init(); | |
} | |
private function handleEnterFrameEvent(event: Event) { | |
var currentTime = Lib.getTimer(); | |
var deltaTime = currentTime - _previousTime; | |
_previousTime = currentTime; | |
var secondsSinceLastFrame = deltaTime / 1000; | |
moveSprites(secondsSinceLastFrame); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment