Skip to content

Instantly share code, notes, and snippets.

@joshtynjala
Last active March 4, 2016 01:28
Show Gist options
  • Save joshtynjala/e296515846f0fc7bff0a to your computer and use it in GitHub Desktop.
Save joshtynjala/e296515846f0fc7bff0a to your computer and use it in GitHub Desktop.
ActionScript transpiled by FlexJS 0.5 RC1 using CreateJS library
package
{
import createjs.Bitmap;
import createjs.LoadQueue;
import createjs.Shape;
import createjs.Sprite;
import createjs.SpriteSheet;
import createjs.Stage;
import createjs.Ticker;
import createjs.TickerEvent;
public class CreateJS_RunningGrant
{
public function CreateJS_RunningGrant()
{
document.body.onload = init;
}
private var stage:Stage;
private var w:Number;
private var h:Number;
private var loader:LoadQueue;
private var sky:Shape;
private var ground:Shape;
private var hill:Bitmap;
private var hill2:Bitmap;
private var grant:Sprite;
private var tileW:Number;
private function init():void
{
stage = new Stage("exampleCanvas");
w = stage.canvas.width;
h = stage.canvas.height;
var manifest:Array =
[
{src: "spritesheet_grant.png", id: "grant"},
{src: "sky.png", id: "sky"},
{src: "ground.png", id: "ground"},
{src: "hill1.png", id: "hill"},
{src: "hill2.png", id: "hill2"},
];
loader = new LoadQueue(false);
loader.addEventListener("complete", handleComplete);
loader.loadManifest(manifest, true, "images/");
}
private function handleComplete(event:Event):void
{
sky = new Shape();
sky.graphics.beginBitmapFill(loader.getResult("sky")).drawRect(0, 0, w, h);
var groundImg:Object = loader.getResult("ground");
ground = new Shape();
ground.graphics.beginBitmapFill(groundImg).drawRect(0, 0, w + groundImg.width, groundImg.height);
ground.y = h - groundImg.height;
tileW = groundImg.width;
hill = new Bitmap(loader.getResult("hill"));
hill.setTransform(Math.random() * w, h - hill.image.height * 4 - groundImg.height, 4, 4);
hill.alpha = 0.5;
hill2 = new Bitmap(loader.getResult("hill2"));
hill2.setTransform(Math.random() * w, h - hill2.image.height * 3 - groundImg.height, 3, 3);
var spriteSheet:SpriteSheet = new SpriteSheet(
{
framerate: 30,
"images": [loader.getResult("grant")],
"frames": {"regX": 82, "height": 292, "count": 64, "regY": 0, "width": 165},
// define two animations, run (loops, 1.5x speed) and jump (returns to run):
"animations": {
"run": [0, 25, "run", 1.5],
"jump": [26, 63, "run"]
}
});
grant = new Sprite(spriteSheet, "run");
grant.y = 35;
stage.addChild(sky, hill, hill2, ground, grant);
stage.addEventListener("stagemousedown", handleJumpStart);
Ticker.timingMode = Ticker.RAF;
Ticker.addEventListener("tick", tick);
}
private function handleJumpStart(event:Event):void
{
grant.gotoAndPlay("jump");
}
private function tick(event:TickerEvent):void
{
var deltaS:Number = event.delta / 1000;
var position:Number = grant.x + 150 * deltaS;
var grantW:Number = grant.getBounds().width * grant.scaleX;
grant.x = (position >= w + grantW) ? -grantW : position;
ground.x = (ground.x - deltaS * 150) % tileW;
hill.x = (hill.x - deltaS * 30);
if (hill.x + hill.image.width * hill.scaleX <= 0) {
hill.x = w;
}
hill2.x = (hill2.x - deltaS * 45);
if (hill2.x + hill2.image.width * hill2.scaleX <= 0) {
hill2.x = w;
}
stage.update(event);
}
}
}
/**
* Generated by Apache Flex Cross-Compiler from CreateJS_RunningGrant.as
* CreateJS_RunningGrant
*
* @fileoverview
*
* @suppress {checkTypes}
*/
goog.provide('CreateJS_RunningGrant');
goog.require('org.apache.flex.utils.Language');
/**
* @constructor
*/
CreateJS_RunningGrant = function() {
document.body.onload = org.apache.flex.utils.Language.closure(this.init, this, 'init');
};
/**
* @private
* @type {createjs.Stage}
*/
CreateJS_RunningGrant.prototype.stage;
/**
* @private
* @type {number}
*/
CreateJS_RunningGrant.prototype.w;
/**
* @private
* @type {number}
*/
CreateJS_RunningGrant.prototype.h;
/**
* @private
* @type {createjs.LoadQueue}
*/
CreateJS_RunningGrant.prototype.loader;
/**
* @private
* @type {createjs.Shape}
*/
CreateJS_RunningGrant.prototype.sky;
/**
* @private
* @type {createjs.Shape}
*/
CreateJS_RunningGrant.prototype.ground;
/**
* @private
* @type {createjs.Bitmap}
*/
CreateJS_RunningGrant.prototype.hill;
/**
* @private
* @type {createjs.Bitmap}
*/
CreateJS_RunningGrant.prototype.hill2;
/**
* @private
* @type {createjs.Sprite}
*/
CreateJS_RunningGrant.prototype.grant;
/**
* @private
* @type {number}
*/
CreateJS_RunningGrant.prototype.tileW;
/**
* @private
*/
CreateJS_RunningGrant.prototype.init = function() {
this.stage = new createjs.Stage("exampleCanvas");
this.w = this.stage.canvas.width;
this.h = this.stage.canvas.height;
var /** @type {Array} */ manifest = [{src:"spritesheet_grant.png", id:"grant"}, {src:"sky.png", id:"sky"}, {src:"ground.png", id:"ground"}, {src:"hill1.png", id:"hill"}, {src:"hill2.png", id:"hill2"}];
this.loader = new createjs.LoadQueue(false);
this.loader.addEventListener("complete", org.apache.flex.utils.Language.closure(this.handleComplete, this, 'handleComplete'));
this.loader.loadManifest(manifest, true, "images/");
};
/**
* @private
* @param {Event} event
*/
CreateJS_RunningGrant.prototype.handleComplete = function(event) {
this.sky = new createjs.Shape();
this.sky.graphics.beginBitmapFill(this.loader.getResult("sky")).drawRect(0, 0, this.w, this.h);
var /** @type {Object} */ groundImg = this.loader.getResult("ground");
this.ground = new createjs.Shape();
this.ground.graphics.beginBitmapFill(groundImg).drawRect(0, 0, this.w + groundImg.width, groundImg.height);
this.ground.y = this.h - groundImg.height;
this.tileW = groundImg.width;
this.hill = new createjs.Bitmap(this.loader.getResult("hill"));
this.hill.setTransform(Math.random() * this.w, this.h - this.hill.image.height * 4 - groundImg.height, 4, 4);
this.hill.alpha = 0.5;
this.hill2 = new createjs.Bitmap(this.loader.getResult("hill2"));
this.hill2.setTransform(Math.random() * this.w, this.h - this.hill2.image.height * 3 - groundImg.height, 3, 3);
var /** @type {createjs.SpriteSheet} */ spriteSheet = new createjs.SpriteSheet({framerate:30, "images":[this.loader.getResult("grant")], "frames":{"regX":82, "height":292, "count":64, "regY":0, "width":165}, "animations":{"run":[0, 25, "run", 1.5], "jump":[26, 63, "run"]}});
this.grant = new createjs.Sprite(spriteSheet, "run");
this.grant.y = 35;
this.stage.addChild(this.sky, this.hill, this.hill2, this.ground, this.grant);
this.stage.addEventListener("stagemousedown", org.apache.flex.utils.Language.closure(this.handleJumpStart, this, 'handleJumpStart'));
createjs.Ticker.timingMode = createjs.Ticker.RAF;
createjs.Ticker.addEventListener("tick", org.apache.flex.utils.Language.closure(this.tick, this, 'tick'));
};
/**
* @private
* @param {Event} event
*/
CreateJS_RunningGrant.prototype.handleJumpStart = function(event) {
this.grant.gotoAndPlay("jump");
};
/**
* @private
* @param {createjs.TickerEvent} event
*/
CreateJS_RunningGrant.prototype.tick = function(event) {
var /** @type {number} */ deltaS = event.delta / 1000;
var /** @type {number} */ position = this.grant.x + 150 * deltaS;
var /** @type {number} */ grantW = this.grant.getBounds().width * this.grant.scaleX;
this.grant.x = (position >= this.w + grantW) ? -grantW : position;
this.ground.x = (this.ground.x - deltaS * 150) % this.tileW;
this.hill.x = (this.hill.x - deltaS * 30);
if (this.hill.x + this.hill.image.width * this.hill.scaleX <= 0) {
this.hill.x = this.w;
}
this.hill2.x = (this.hill2.x - deltaS * 45);
if (this.hill2.x + this.hill2.image.width * this.hill2.scaleX <= 0) {
this.hill2.x = this.w;
}
this.stage.update(event);
};
/**
* Metadata
*
* @type {Object.<string, Array.<Object>>}
*/
CreateJS_RunningGrant.prototype.FLEXJS_CLASS_INFO = { names: [{ name: 'CreateJS_RunningGrant', qName: 'CreateJS_RunningGrant'}] };
// Ensures the symbol will be visible after compiler renaming.
goog.exportSymbol('CreateJS_RunningGrant', CreateJS_RunningGrant);
@kennylerma
Copy link

Hey Josh, I'm very interested in CreateJS with FlexJS. I've tried dts2as to create a swc for createjs, but I just get the error "Definition with name createjs.Event already exists. Cannot create class" Any chance you could share your createjs swc?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment