I have this AS3 code that creates tiles and it also adds a rectangle border to them and I’m having a imposible time trying to figure out in the code where to get it to draw the top and bottom border but not the left and the right border.
Here’s the code:
package com.war.display {
import flash.display.MovieClip;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.geom.Matrix;
import flash.display.JointStyle;
public class ImageTiling extends MovieClip {
/********************
***Class Constants***
********************/
public static const TT_TILE_REPEAT = "$%^TileRepeat^%$";
public static const TT_ANIMATION = "$%^Animation^%$";
/********************
***Class Variables***
********************/
private static var tilingTypes:Array = new Array(TT_TILE_REPEAT, TT_ANIMATION);
/************************
***Instance Properties***
************************/
private var _width:Number = 0;
private var _height:Number = 0;
private var _tilingType:String = ImageTiling.TT_TILE_REPEAT;
private var _animationAlignment:String = "center";
private var _offsetX:Number = 0;
private var _offsetY:Number = 0;
private var _flipHorizontal:Boolean = false;
private var _flipVertical:Boolean = false;
private var _tileAngle:Number = 0;
private var _sourceScaleX:Number = 1;
private var _sourceScaleY:Number = 1;
private var _smoothing:Boolean = true;
private var _tileSourcePath:String;
private var _useBorder:Boolean = false;
private var _borderColor:uint = 0x000000;
private var _borderThickness:Number = 1;
private var _borderAlpha:Number = 1;
/**********************
***Setters & Getters***
**********************/
public function set tilingType(newTilingType:String) {
if(ImageTiling.tilingTypes.indexOf(newTilingType) != -1) {
_tilingType = newTilingType;
if(sourceSetup) {
resetAnimationHolder();
}
resetTilingHolder();
setupTile();
}
}
public function get tilingType():String {
return _tilingType;
}
public function set animationAlignment(newAnimationAlignment:String) {
_animationAlignment = newAnimationAlignment;
generateTiling();
}
public function get animationAlignment():String {
return _animationAlignment;
}
public function set offsetX(newOffsetX:Number):void {
_offsetX = newOffsetX;
generateTiling();
}
public function get offsetX():Number {
return _offsetX;
}
public function get relOffsetX():Number {
return offsetX - (offsetXDir * Math.floor(Math.abs(offsetX) / sourceWidth) * sourceWidth);
}
public function get offsetXDir():int {
if(offsetX > 0) {
return 1;
}
else if(offsetX < 0) {
return -1;
}
else {
return 0;
}
}
public function set offsetY(newOffsetY:Number):void {
_offsetY = newOffsetY;
generateTiling();
}
public function get offsetY():Number {
return _offsetY;
}
public function get relOffsetY():Number {
return offsetY - (offsetYDir * Math.floor(Math.abs(offsetY) / sourceHeight) * sourceHeight);
}
public function get offsetYDir():int {
if(offsetY > 0) {
return 1;
}
else if(offsetY < 0) {
return -1;
}
else {
return 0;
}
}
public function set flipHorizontal(newFlipHorizontal:Boolean):void {
_flipHorizontal = newFlipHorizontal;
generateTiling();
}
public function get flipHorizontal():Boolean {
return _flipHorizontal;
}
public function set flipVertical(newFlipVertical:Boolean):void {
_flipVertical = newFlipVertical;
generateTiling();
}
public function get flipVertical():Boolean {
return _flipVertical;
}
public function set tileAngle(newTileAngle:Number):void {
if(newTileAngle > 180) {
_tileAngle = 180;
}
else if(newTileAngle < -180) {
_tileAngle = -180;
}
else {
_tileAngle = newTileAngle;
}
generateTiling();
}
public function get tileAngle():Number {
return _tileAngle;
}
public function set sourceScaleX(newTileScaleX:Number):void {
_sourceScaleX = newTileScaleX;
generateTiling();
}
public function get sourceScaleX():Number {
return _sourceScaleX;
}
public function set sourceScaleY(newTileScaleY:Number):void {
_sourceScaleY = newTileScaleY;
generateTiling();
}
public function get sourceScaleY():Number {
return _sourceScaleY;
}
public function set smoothing(newSmoothing:Boolean):void {
_smoothing = newSmoothing;
setupTile();
}
public function get smoothing():Boolean {
return _smoothing;
}
public function set tileSourcePath(newTileSourcePath:String):void {
_tileSourcePath = newTileSourcePath;
loadTileSource();
}
public function get tileSourcePath():String {
return _tileSourcePath;
}
public function set useBorder(newUseBorder:Boolean):void {
_useBorder = newUseBorder;
if(newUseBorder) {
if(borderGenerated) {
borderSPR.visible = true;
}
else {
generateBorder();
}
}
else {
if(borderGenerated) {
borderSPR.visible = false;
}
}
}
public function get useBorder():Boolean {
return _useBorder;
}
public function set borderColor(newBorderColor:uint):void {
_borderColor = newBorderColor;
generateBorder();
}
public function get borderColor():uint {
return _borderColor;
}
public function set borderThickness(newBorderThickness:Number):void {
_borderThickness = newBorderThickness;
generateBorder();
}
public function get borderThickness():Number {
return _borderThickness;
}
public function set borderAlpha(newBorderAlpha:Number):void {
if(newBorderAlpha > 1) {
_borderAlpha = 1;
}
else if(newBorderAlpha < 0) {
_borderAlpha = 0;
}
else {
_borderAlpha = newBorderAlpha;
}
generateBorder();
}
public function get borderAlpha():Number {
return _borderAlpha;
}
private function get sourceWidth():Number {
if(tileSourceSetup && tilingType == ImageTiling.TT_TILE_REPEAT) {
return tileBMPD.width;
}
else if(sourceLoaded) {
return sourceObject.contentLoaderInfo.width;
}
else {
return 0;
}
}
private function get sourceHeight():Number {
if(tileSourceSetup && tilingType == ImageTiling.TT_TILE_REPEAT) {
return tileBMPD.height;
}
else if(sourceLoaded) {
return sourceObject.contentLoaderInfo.height;
}
else {
return 0;
}
}
private function get tilingMatrix():Matrix {
var tempMatrix:Matrix = new Matrix();
var xScale:Number = sourceScaleX;
var yScale:Number = sourceScaleY;
var xOffset:Number = relOffsetX;
var yOffset:Number = relOffsetY;
if(flipHorizontal) {
yOffset = sourceHeight * yScale;
yScale *= -1;
}
if(flipVertical) {
xOffset = sourceWidth * xScale;
xScale *= -1
}
tempMatrix.scale(xScale, yScale);
tempMatrix.translate(xOffset, yOffset);
if(tileAngle != 0) {
tempMatrix.rotate(tileAngle * (Math.PI / 180));
}
return tempMatrix;
}
public function get tilingReady():Boolean {
if(sourceSetup && tilingGenerated && (useBorder && borderGenerated || !useBorder)) {
return true;
}
else {
return false;
}
}
/*********************
***Override Setters***
*********************/
override public function set width(newWidth:Number):void {
_width = newWidth;
generateTiling();
}
override public function get width():Number {
return _width;
}
override public function set height(newHeight:Number):void {
_height = uint(newHeight);
generateTiling();
}
override public function get height():Number {
return _height;
}
/***********************
***Instance Variables***
***********************/
private var sourceLoaded:Boolean = false;
private var sourceSetup:Boolean = false;
private var tileSourceSetup:Boolean = false;
private var animationSourceSetup:Boolean = false;
private var tilingGenerated:Boolean = false;
private var animationGenerated:Boolean = false;
private var borderGenerated:Boolean = false;
private var animationMaskGenerated:Boolean = false;
public var tilingReadyCBF:Function;
/*********************
***Instance Objects***
*********************/
private var sourceObject:Loader;
private var tileBMPD:BitmapData;
private var tilingGraphicsHolderMC:MovieClip = new MovieClip();
private var animationHolderMC:MovieClip;
private var tilingSPR:Sprite;
private var borderSPR:Sprite;
private var animationMSK:Sprite;
/****************
***CONSTRUCTOR***
****************/
public function ImageTiling(tileSourcePath_:String, tilingType_:String = ImageTiling.TT_TILE_REPEAT, tilingReadyCBF_:Function = null) {
tileSourcePath = tileSourcePath_;
tilingType = tilingType_;
tilingReadyCBF = tilingReadyCBF_;
addChild(tilingGraphicsHolderMC);
}
/*********************
***Internal Methods***
*********************/
private function loadTileSource():void {
if(tileSourcePath != "") {
var tileSourceRequest:URLRequest = new URLRequest(tileSourcePath);
sourceObject = new Loader();
sourceObject.contentLoaderInfo.addEventListener(Event.COMPLETE, tileSourceHandler);
sourceObject.load(tileSourceRequest);
}
}
private function setupTile():void {
if(sourceLoaded) {
switch(tilingType) {
case ImageTiling.TT_TILE_REPEAT:
tileBMPD = new BitmapData(sourceWidth, sourceHeight, true, 0x000000);
tileBMPD.draw(sourceObject, null, null, null, null, smoothing);
tileSourceSetup = true;
break;
case ImageTiling.TT_ANIMATION:
if(!animationHolderMC) {
animationHolderMC = new MovieClip();
}
animationHolderMC.addChild(sourceObject);
sourceObject.x = -sourceWidth / 2;
sourceObject.y = -sourceHeight / 2;
animationSourceSetup = true;
break;
}
sourceSetup = true;
generateTiling();
}
}
private function generateTiling():void {
if(sourceSetup) {
var borderOffset:Number = 0;
if(useBorder) {
borderOffset = borderThickness;
}
switch(tilingType) {
case ImageTiling.TT_TILE_REPEAT:
if(tileSourceSetup) {
if(tilingGenerated) {
tilingSPR.graphics.clear();
}
else {
tilingSPR = new Sprite();
}
tilingGraphicsHolderMC.addChild(tilingSPR);
tilingSPR.graphics.beginBitmapFill(tileBMPD, tilingMatrix, true, smoothing);
tilingSPR.graphics.drawRect(borderOffset, borderOffset, width - (borderOffset * 2), height - (borderOffset * 2));
tilingSPR.graphics.endFill();
removeAnimationMask();
}
break;
case ImageTiling.TT_ANIMATION:
if(animationSourceSetup) {
tilingGraphicsHolderMC.addChild(animationHolderMC);
var xScale:Number = sourceScaleX;
var yScale:Number = sourceScaleY;
var alignmentX:Number;
var alignmentY:Number = (sourceHeight * yScale) / 2;
switch(animationAlignment) {
case "left":
alignmentX = (sourceWidth * xScale) / 2;
break;
case "center":
alignmentX = width / 2;
break;
case "right":
alignmentX = width - ((sourceWidth * xScale) / 2);
break;
}
animationHolderMC.x = alignmentX + borderOffset + offsetX;
animationHolderMC.y = alignmentY + borderOffset + offsetY;
if(flipHorizontal) {
yScale *= -1;
}
if(flipVertical) {
xScale *= -1;
}
animationHolderMC.scaleX = xScale;
animationHolderMC.scaleY = yScale;
animationHolderMC.rotation = tileAngle;
animationGenerated = true;
generateAnimationMask();
}
break;
}
tilingGenerated = true;
generateBorder();
if(tilingReadyCBF != null) {
tilingReadyCBF(this);
tilingReadyCBF = null;
}
}
}
private function generateAnimationMask():void {
if(animationGenerated) {
if(animationMaskGenerated) {
animationMSK.visible = true;
animationMSK.graphics.clear();
}
else {
animationMSK = new Sprite();
addChild(animationMSK);
}
tilingGraphicsHolderMC.mask = animationMSK;
var borderOffset:Number = 0;
if(useBorder) {
borderOffset = borderThickness;
}
animationMSK.graphics.beginFill(0x000000);
animationMSK.graphics.drawRect(borderOffset, borderOffset, width - (borderOffset * 2), height - (borderOffset * 2));
animationMSK.graphics.endFill();
animationMaskGenerated = true;
}
}
private function generateBorder():void {
if(useBorder) {
if(borderGenerated) {
borderSPR.graphics.clear();
}
else {
borderSPR = new Sprite();
addChild(borderSPR);
}
borderSPR.graphics.beginFill(0x000000, 0.0);
borderSPR.graphics.lineStyle(borderThickness, borderColor, borderAlpha, false, "normal", null, JointStyle.MITER);
borderSPR.graphics.drawRect(borderThickness / 2, borderThickness / 2, width - borderThickness, height - borderThickness);
borderSPR.graphics.endFill();
borderGenerated = true;
}
}
private function removeAnimationMask():void {
if(animationMaskGenerated) {
tilingGraphicsHolderMC.mask = null;
animationMSK.visible = false;
}
}
private function resetTilingHolder():void {
if(tilingGenerated) {
while(tilingGraphicsHolderMC.numChildren > 0) {
tilingGraphicsHolderMC.removeChildAt(0);
}
}
}
private function resetAnimationHolder():void {
if(animationSourceSetup && animationHolderMC) {
while(animationHolderMC.numChildren > 0) {
animationHolderMC.removeChildAt(0);
}
}
}
/*******************
***Event Handlers***
*******************/
private function tileSourceHandler(evt:Event):void {
sourceLoaded = true;
if(width == 0 || height == 0) {
width = sourceWidth;
height = sourceWidth;
}
setupTile();
}
}
}
Please help.
Thank you