Duplicate loader content in ActionScript 3

Sometimes you want to display the loaded image file twice. Here’s how to do it.

var loader:Loader = new Loader();
 
var sprite1:Sprite = new Sprite();
sprite1.addChild(getLoadedBitmap(loader.content));
 
var sprite 2:Sprite = new Sprite();
sprite2.addChild(getLoadedBitmap(loader.content));
 
function getLoadedBitmap(l:Loader):Bitmap {
  return new Bitmap(Bitmap(l.content).bitmapData);
}

Smooth movement of a TextField with Greensock tweens

Here is some code on how to get a TextField (or any other display object) to marquee/scroll across the stage smoothly. When it has scrolled out completely of the viewfield, it restarts from the right side of the stage.

If the animation is rough, try increasing the document’s FPS to get a smoother movement.

This function calls itself when it’s complete and passes itself as a variable back to the function, so it repeats continuously.

var movementSpeed:Number = 1.5;
var speed:int = 100;
var stageWidth:int = 1920;
 
private function moveMarquee(obj:DisplayObject):void {
	TweenMax.to(obj, movementSpeed, {x:obj.x - speed, ease: Linear.easeNone, onComplete:moveMarquee, onCompleteParams:[obj]});
	if(obj.x < -obj.width) {
		TweenMax.killTweensOf(obj);
		obj.x = stageWidth;
		TweenMax.to(obj, movementSpeed, {x:obj.x - speed, ease: Linear.easeNone, onComplete:moveMarquee, onCompleteParams:[obj]});
	}
}

To initialize the first start, use this:

moveMarquee(marqueeText);

And to kill it, use this:

TweenMax.killTweensOf(marqueeText);

Creating a timer in Unity3D

Create a timer (JavaScript) and display the end time on stage when the user has finished playing. The boolean won variable is set elsewhere in the game, but used here.

private var startTime;
private var endTime;
var textTime : String;
 
function Awake() {
	startTime = Time.time;
}
 
function OnGUI () {
 
	if(!won) {
               // while still playing
		var guiTime = Time.time - startTime;;
   		var minutes : int = guiTime / 60;
   		var seconds : int = guiTime % 60;
   		var fraction : int = (guiTime * 100) % 100;
   		//textTime = String.Format ("{0:00}:{1:00}:{2:000}", minutes, seconds, fraction);
   		textTime = String.Format ("{0:00}:{1:00}", minutes, seconds);
   		GUI.Label (Rect (0, 0, 100, 30), textTime);
   		endTime = textTime;
	} else {
               // display end time in the label field
   		GUI.Label (Rect (0, 0, 100, 30), endTime);
	}
}

Creating a scrolling text in Unity

Here’s how to make a scrolling textbox with word wrap in Unity.

public var scrollBoxWidth = 400;
public var scrollBoxHeight = 200;
private var str : String = "This is a multiline\nstring with two lines.";
 
var scrollPosition : Vector2 = Vector2.zero;
var style : GUIStyle;
 
private var textStyle : GUIStyle = new GUIStyle();
 
function OnGUI () {
 
    GUILayout.BeginArea(new Rect(35, 12, scrollBoxWidth, scrollBoxHeight));
 
    GUI.skin.scrollView = style;
    GUI.skin.textArea.wordWrap = true;
    scrollPosition = GUILayout.BeginScrollView (scrollPosition, GUILayout.Width(scrollBoxWidth), GUILayout.Height(scrollBoxHeight));
    GUILayout.Label(str);
    GUILayout.EndScrollView ();
    GUILayout.EndArea();
}