Created
June 9, 2012 17:43
-
-
Save wvdschel/2901919 to your computer and use it in GitHub Desktop.
Drawing some land
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 nme.display.Bitmap; | |
import nme.display.BitmapData; | |
import nme.display.Sprite; | |
import nme.display.StageAlign; | |
import nme.display.StageScaleMode; | |
import nme.display.Graphics; | |
import nme.events.Event; | |
import nme.Assets; | |
import nme.Lib; | |
/** | |
* ... | |
* @author Wim Vander Schelden | |
*/ | |
class Main extends Sprite | |
{ | |
private var blue:Bitmap; | |
private var red:Bitmap; | |
private var dirt:BitmapData; | |
private var land:Sprite; | |
private var sky:Bitmap; | |
private var skyWidth:Float; | |
private var skyHeight:Float; | |
private var skyAspect:Float; | |
public function new() | |
{ | |
super (); | |
addEventListener (Event.ADDED_TO_STAGE, this_onAddedToStage); | |
} | |
private function regenerateLand():Void | |
{ | |
var g:Graphics; | |
g = land.graphics; | |
g.clear(); | |
g.beginBitmapFill(dirt); | |
//g.beginFill(0xFF0000); | |
//g.drawRect(0, 0, 100, 100); | |
g.drawRect(0, 3*stage.stageHeight / 4, stage.stageWidth, 3*stage.stageHeight / 4); | |
g.endFill(); | |
} | |
private function stage_onResize (event:Event):Void | |
{ | |
red.x = 3*(stage.stageWidth - red.width) / 4; | |
red.y = 3*(stage.stageHeight - red.height) / 4; | |
blue.x = (stage.stageWidth - blue.width) / 4; | |
blue.y = 3*(stage.stageHeight - blue.height) / 4; | |
// Sky background should be scaled to maintain correct aspect ratio. | |
var stageAspect:Float; | |
stageAspect = stage.stageWidth / cast(stage.stageHeight, Float); | |
if ( stageAspect > skyAspect ) | |
{ | |
sky.width = stage.stageWidth; | |
sky.height = stage.stageWidth / skyAspect; | |
} else { | |
sky.width = stage.stageHeight * skyAspect; | |
sky.height = stage.stageHeight; | |
} | |
regenerateLand(); | |
} | |
private function this_onAddedToStage (event:Event):Void | |
{ | |
land = new Sprite(); | |
land.x = 0; land.y = 0; | |
addChild(land); | |
sky = new Bitmap (Assets.getBitmapData ("img/sky.png")); | |
skyWidth = sky.width; skyHeight = sky.height; | |
sky.width = stage.stageWidth; sky.height = stage.stageHeight; | |
skyAspect = skyWidth / skyHeight; | |
blue = new Bitmap (Assets.getBitmapData ("img/blue.png")); | |
blue.width = blue.width / 8; blue.height = blue.height / 8; | |
red = new Bitmap (Assets.getBitmapData ("img/red.png")); | |
red.width = red.width / 8; red.height = red.height / 8; | |
dirt = Assets.getBitmapData ("img/dirt.jpg"); | |
stage_onResize(null); | |
addChild(sky); | |
addChild (blue); | |
addChild(red); | |
stage.addEventListener (Event.RESIZE, stage_onResize); | |
} | |
static public function main() | |
{ | |
var stage = Lib.current.stage; | |
stage.scaleMode = nme.display.StageScaleMode.NO_SCALE; | |
stage.align = nme.display.StageAlign.TOP_LEFT; | |
Lib.current.addChild(new Main()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment