Example of Coding on haXe – Part 10

In this example you will see: feffects, feffects.Tween, flash.external.ExternalInterface.

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

Code Example: Example_10_Sprite2D.hx:

/**
 * @author: Alejandro G. Carlstein R. M.
 * @description: In order to use Tweener you have to install the feffects
 * Install: neko-dev (Ubuntu: sudo apt-get Eneko-dev)
 * Execute: haxelib install feffects
 *	 Compile with flag: -lib feffects
 *	 Tweens available: feffects.easing.
 *	 Quint.easeIn, Quint.easeOut, Quint.easeInOut
 *	 Sine.easeIn, Sine.easeOut, Sine.easeInOut
 *	 Back.easeIn, Back.easeOut, Back.easeInOut
 *	 Bounce.easeIn, Bounce.easeOut, Bounce.easeInOut
 *	 Circ.easeIn, Circ.easeOut, Circ.easeInOut
 *	 Cubic.easeIn, Cubic.easeOut, Cubic.easeInOut
 *	 Elastic.easeIn, Elastic.easeOut, Elastic.easeInOut
 *	 Expo.easeIn, Expo.easeOut, Expo.easeInOut
 *	 Linear.easeIn, Linear.easeOut, Linear.easeInOut, Linear.easeNone
 *	 Quad.easeIn, Quad.easeOut, Quad.easeInOut
 *	 Quart.easeIn, Quart.easeOut, Quad.easeInOut
 *
 */

import feffects.Tween;
import feffects.easing.Bounce;
import feffects.easing.Circ;
import feffects.easing.Elastic;
import flash.text.TextField;
import flash.display.Sprite;

class NumericBall2D extends Sprite{
	private var text_field : TextField;
	private static var LINE_SIZE : Float = 1.0;
	private static var LINE_COLOR : Int = 0x000000;
	private static var FILL_COLOR : Int = 0x11FF33;
	private static var CIRCLE_X : Int = 0;
	private static var CIRCLE_Y : Int = 0;
	public function new(size : Float = 25){
		super();
		graphics.lineStyle(LINE_SIZE, LINE_COLOR);
    graphics.beginFill(FILL_COLOR);
	  graphics.drawCircle(CIRCLE_X, CIRCLE_Y, size);
    graphics.endFill();

	  text_field = new TextField();
		flash.Lib.current.addChild(text_field);
	}

	public function setText(text : String){
		text_field.text = text;
	  text_field.x = this.x - 12;
	  text_field.y = this.y - 40;
	}
}

class Example_10_Sprite2D{

    private var numericBall2D : NumericBall2D;
    private var tween : Tween;
		private static var MIN_Y : Int = 100;
		private static var MAX_Y : Int = 300;
		private static var EFFECT_DURATION : Int = 5000;

    public function new(?x : Float){
        numericBall2D = new NumericBall2D();
        numericBall2D.x = x;
        flash.Lib.current.addChild(numericBall2D);

        tween = new Tween(MIN_Y, MAX_Y, EFFECT_DURATION, Elastic.easeOut );
        tween.setTweenHandlers(moveY, msgFinished );
        trace ( 'tween staring at position: ' + MIN_Y);
        tween.start();
    }

    function moveY( position : Float ){
        numericBall2D.setText(Std.string(Math.round(position)));
        numericBall2D.y = position;
    }

    function msgFinished( position : Float ){
        trace ( 'tween finished at position: ' + position );
    }

}

example_10.hx:

/**
 * @author: Alejandro G. Carlstein R. M.
 * @description:
 */

import flash.external.ExternalInterface;
import flash.Lib;

class Example_10{

  static function main(){
		var happy = new Example_10_Sprite2D(100);
  }	

}

If you encounter any problems or errors, please let me know by providing an example of the code, input, output, and an explanation. Thanks.

Share

Example of Coding on haXe – Part 9


Warning: Illegal string offset 'delimiter' in /homepages/8/d126622285/htdocs/Websites/elblender/wordpress/wp-content/plugins/swfobject-reloaded/wp_swf_shortcode.php on line 123

Warning: Illegal string offset 'delimiter' in /homepages/8/d126622285/htdocs/Websites/elblender/wordpress/wp-content/plugins/swfobject-reloaded/wp_swf_shortcode.php on line 123

In this example you will see: TextField, TextFormat, TextFormatAlign.

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

Actual Example:

Sorry, either Adobe flash is not installed or you do not have it enabled

Code example: Example_9.hx:

/**
 * @author: Alejandro G. Carlstein R. M.
 * @description:
 */

import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.text.TextFormatDisplay;

class MyTextField extends flash.display.MovieClip{

	private var text_field : TextField;
	private var text_format : TextFormat;
	private var text_font : String;
	private var text_size : Int;
	private var text_color : Int;
	private var text : String;

	private function initTextFormat(){
		text_format = new TextFormat();
		text_format.font = text_font;
    text_format.size = text_size;
    text_format.color = text_color;
	}

	private function initTextField(){
		var text_field = new TextField();
		text_field.defaultTextFormat = text_format;
		text_field.text = text;
		text_field.x = x;
		text_field.y = y;
		flash.Lib.current.addChild(text_field);
	}

	public function new(){
		super();
		text_font = 'Helvetica';
		text_size = 12;
		text_color = 0x000000;
		initTextFormat();
		text = 'Default';
		initTextField();
	}
}

class MyTextField2D extends TextField{

	private var text_format : TextFormat;			

	public static var LEFT : TextFormatAlign = TextFormatAlign.LEFT;
	public static var RIGHT : TextFormatAlign = TextFormatAlign.RIGHT;
	public static var CENTER : TextFormatAlign = TextFormatAlign.CENTER;
	public static var JUSTIFY : TextFormatAlign = TextFormatAlign.JUSTIFY;	

	public function new(?text : String, ?font : String, ?size : Int, ?color : Int){
		super();
		text_format = new TextFormat();
		text_format.font = (font == null) ? 'Helvetica' : font;
		text_format.size = (size == null) ? 16 : size;
		text_format.color = (color == null) ? 0x000000 : color;
 		this.defaultTextFormat = text_format;
		this.text = 'Default 2D';
	}

	public function setFont(font : String) : Void{
		text_format.font = font;
		this.setTextFormat(text_format);
	}

	public function getFont() : String{
		return text_format.font;
	}

	public function setSize(size : Float) : Void{
		text_format.size = size;
		this.setTextFormat(text_format);
	}

	public function getSize() : Float {
		return text_format.size;
	}

	public function setColor(color : Int) : Void{
		text_format.color = color;
		this.setTextFormat(text_format);
	}

	public function getColor() : Int {
		return text_format.color;
	}

	public function setBold(boolean : Bool) : Void {
		text_format.bold = boolean;
		this.setTextFormat(text_format);
	}

	public function isBold() : Bool{
		return text_format.bold;
	}

	public function setItalic (boolean : Bool) : Void{
		text_format.italic = boolean;
		this.setTextFormat(text_format);
	}

	public function isItalic() : Bool{
		return text_format.italic;
	}

	public function setUnderline(boolean : Bool) : Void{
		text_format.underline = boolean;
	}

	public function isUnderline() : Bool{
		return text_format.underline;
	}

	public function setAlign(align : TextFormatAlign) : Void {
		text_format.align = align;
		this.setTextFormat(text_format);
	}

	public function getAlign() : TextFormatAlign{
		return text_format.align;
	}

	public function setIndent(indentation : Float) : Void{
		text_format.indent = indentation;
		this.setTextFormat(text_format);
	}

	public function getIndent() : Float {
		return text_format.indent;
	}

	public function setURL(url : String) : Void{
		text_format.url = url;
		this.setTextFormat(text_format);
	}

	public function getURL() : String{
		return text_format.url;
	}

	public function setLeftMargin(margin : Float) : Void{
		text_format.leftMargin;
		this.setTextFormat(text_format);
	}

	public function getLeftMargin() : Float{
		return text_format.leftMargin;
	}

	public function setRightMargin(margin : Float) : Void{
		text_format.rightMargin;
		this.setTextFormat(text_format);
	}

	public function getRightMargin() : Float{
		return text_format.rightMargin;
	}

	public function setLetterSpacing(spacing : Float) : Void{
		text_format.letterSpacing = spacing;
		this.setTextFormat(text_format);
	}

	public function getLetterSpacing() : Float{
		return text_format.letterSpacing;
	}

	public function setLeading(leading : Float) : Void{
		text_format.leading = leading;
		this.setTextFormat(text_format);
	}

	public function getLeading() : Float{
		return text_format.leading;
	}

	public function setKerning(boolean : Bool) : Void{
		text_format.kerning = boolean;
		this.setTextFormat(text_format);
	}

	public function isKerning() : Bool{
		return text_format.kerning;
	}

	public function setBullet(boolean : Bool) : Void{
		text_format.bullet = boolean;
		this.setTextFormat(text_format);
	}

	public function isBullet() : Bool{
		return text_format.bullet;
	}

	public function setBlockIndent(blockIndent : Float) : Void{
		text_format.blockIndent = blockIndent;
		this.setTextFormat(text_format);
	}

	public function getBlockIndex() : Float{
		return text_format.blockIndent;
	}

	public function setDisplay(display : TextFormatDisplay) : Void{
		text_format.display = display;
		this.setTextFormat(text_format);
	}

	public function getDisplay() : TextFormatDisplay{
		return text_format.display;
	}	

	public function setTarget(target : String) : Void{
		text_format.target = target;
		this.setTextFormat(text_format);
	}	

	public function getTartet() : String{
		return text_format.target;
	}

	public function setTabStops(tabStops : Array<UInt>) : Void{
		text_format.tabStops = tabStops;
		this.setTextFormat(text_format);
	}

	public function getTabStops() : Array<UInt>{
		return text_format.tabStops;
	}

	public function addAsChild() : Void {
		flash.Lib.current.addChild(this);
	}
} 

class Example_9 {

	static function main(){
		var myTextField : MyTextField = new MyTextField();

		var myTextField2D : MyTextField2D = new MyTextField2D();
		myTextField2D.x = 20.0;
		myTextField2D.y = 20.0;
		flash.Lib.current.addChild(myTextField2D);
		haxe.Timer.delay(function(){myTextField2D.setColor(0xAAFF22);}, 2000);
                haxe.Timer.delay(function(){myTextField2D.setColor(0x0000FF);}, 5000);
                haxe.Timer.delay(function(){myTextField2D.setColor(0x00FF00);}, 10000);
                haxe.Timer.delay(function(){myTextField2D.setColor(0xFF0000);}, 15000);
	}


Error: Attachment is not available or is not Flash content.
}

If you encounter any problems or errors, please let me know by providing an example of the code, input, output, and an explanation. Thanks.

Share

Example of Coding on haXe – Part 8


Warning: Illegal string offset 'delimiter' in /homepages/8/d126622285/htdocs/Websites/elblender/wordpress/wp-content/plugins/swfobject-reloaded/wp_swf_shortcode.php on line 123

In this example you will see: Class, extends, flash.display.MovieClip, flash.display.MovieClip.graphics, haxe.Timer.delay, flash.Lib.current.addChild

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

Actual Example:

Sorry, either Adobe flash is not installed or you do not have it enabled

Code example: Example_8.hx:

/**
 * @author: Alejandro G. Carlstein R. M.
 * @description:
 */

import flash.display.MovieClip;

class Rectangle2D{
	private static var DEFAULT_BORDER_COLOR = 0x000000;
	private static var DEFAULT_BORDER_THICKNESS = 1.0;
	private var x : Int;
	private var y : Int;
	private var width : Int;
	private var height : Int;
	private var background_color : Null<Int>;
	private var border_color : Null<Int>;
	private var border_thickness : Null<Float>;
	private var movie_clip : flash.display.MovieClip;

	public function new(x : Int,
											y : Int,
											width : Int,
											height : Int,
											?background_color : Null<Int>,
											?border_color : Null<Int>,
											?border_thickness : Null<Float>){
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.background_color = (background_color == null) ? null : background_color;
		this.border_color = (border_color == null) ? DEFAULT_BORDER_COLOR : border_color;
		this.border_thickness = (border_thickness == null) ? DEFAULT_BORDER_THICKNESS : border_thickness;
	}

	public function draw(){
		movie_clip = flash.Lib.current;
		moveTo(x, y);
	}

	public function moveTo(x : Int, y : Int){
		this.x = x;
		this.y = y;
		resize(width, height);
	}

	public function resize(width : Int, height : Int){
		movie_clip.graphics.clear();

    if (background_color != null)
    	movie_clip.graphics.beginFill(background_color);

		if (border_thickness != null)
	    movie_clip.graphics.lineStyle(border_thickness, border_color);

		movie_clip.graphics.moveTo(x, y);
    movie_clip.graphics.lineTo(width,y);
    movie_clip.graphics.lineTo(width, height);
    movie_clip.graphics.lineTo(x, height);

    movie_clip.graphics.lineTo(x, y);

    if (background_color > -1)
    	movie_clip.graphics.endFill();

	}

}

class Shape2D extends MovieClip{
	private static var DEFAULT_BORDER_COLOR = 0x000000;
	private static var DEFAULT_BORDER_THICKNESS = 1.0;
	private var radius : Float;
	private var background_color : Null<Int>;
	private var border_color : Null<Int>;
	private var border_thickness : Null<Float>;

	public function new(x : Int,
											y : Int,
											?background_color : Null<Int>,
											?border_color : Null<Int>,
											?border_thickness : Null<Float>){
		super();
		this.x = x;
		this.y = y;
		this.background_color = (background_color == null) ? null : background_color;
		this.border_color = (border_color == null) ? DEFAULT_BORDER_COLOR : border_color;
		this.border_thickness = (border_thickness == null) ? DEFAULT_BORDER_THICKNESS : border_thickness;
	}

	public function drawCircle(radius : Float, ?x : Int, ?y : Int){
		if (x != null) this.x = x;
		if (x != null) this.y = y;
		this.radius = radius;
		beginDraw();
		graphics.drawCircle(x, y, radius);
		endDraw();
	}

	public function drawRectangle(width : Int, height : Int, ?x : Int, ?y : Int){
		if (x != null) this.x = x;
		if (x != null) this.y = y;
		this.width = width;
		this.height = height;
		beginDraw();
		graphics.drawRect(x, y, width, height);
		endDraw();
	}

	private function beginDraw(){
		graphics.clear();
		graphics.moveTo(x, y);

    if (background_color != null)
    	graphics.beginFill(background_color);

		if (border_thickness != null)
	    graphics.lineStyle(border_thickness, border_color);

	}

	private function endDraw(){
		 if (background_color > -1)
    	graphics.endFill();
	}

}

class Example_8{

 	static function main(){

		var rectangle : Rectangle2D = new Rectangle2D(100,100,100, 100, 0x334433, 0x44444);
		rectangle.draw();
		rectangle.resize(200, 200);
		haxe.Timer.delay(function(){rectangle.moveTo(0, 0);}, 2000);

		var shape2D : Shape2D = new Shape2D(100,100, null, 0x000000, 3.0);
		flash.Lib.current.addChild(shape2D);
		shape2D.drawCircle(45.0);

		haxe.Timer.delay(function(){shape2D.drawRectangle(100, 100);}, 2000);

 	}
}

If you encounter any problems or errors, please let me know by providing an example of the code, input, output, and an explanation. Thanks.

Share