A problem with DrawRoundRect size - actionscript-3

I'm trying to create a custom button with ActionScript 3.0. I'm suing a round rect as background, but I have a problem with it size.
This is my custom button class:
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.Shape;
public class customButton extends Sprite
{
private var background:Shape;
public var bgColor:uint;
public var borderColor:uint;
public var borderSize:uint;
public var cornerRadius:uint;
private var label:TextField;
public function customButton(text:String)
{
super();
this.opaqueBackground = 0xFF0000;
background = new Shape();
borderSize = 1;
borderColor = 0x666666;
bgColor = 0xFFCC00;
cornerRadius = 9;
label = new TextField();
label.text = text;
var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = 0;
format.size = 38;
format.underline = true;
label.defaultTextFormat = format;
addChild(background);
addChild(label);
buttonMode = true;
mouseChildren = false;
}
public function draw():void
{
background.graphics.lineStyle(borderSize, borderColor);
background.graphics.beginFill(bgColor);
background.graphics.drawRoundRect(0, 0, this.width, this.height cornerRadius);
background.graphics.endFill();
}
}
}
And this is the code used to show the button:
public function Test01()
{
super();
// support autoOrients
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
button = new customButton("Button");
button.x = 200;
button.y = 300;
button.width = 200;
button.height = 100;
button.draw();
addChild(button);
}
If I set this size to the button:
button.width = 200;
button.height = 100;
I get the following:
But I set it to button size:
button.width = 40;
button.height = 20;
(This size is the same used in customButton class). I get:
I don't know why when I use a size of (40, 20) I get a smaller rectangle than that size.
Any advice?

it is happening because you are setting the width directly to the Sprite, it changes the size of the sprite no the size of the background you are drawing.
in your customButton class add some code:
private var _width:Number = 10;
private var _height:Number = 10;
override public function get width():Number { return _width; }
override public function set width(value:Number):void
{
_width = value;
draw ();
}
override public function get height():Number { return _height; }
override public function set height(value:Number):void
{
_height = value;
draw ();
}
private function draw():void
{
background.graphics.clear ()
background.graphics.lineStyle(borderSize, borderColor);
background.graphics.beginFill(bgColor);
background.graphics.drawRoundRect(0, 0, _width, _height, cornerRadius);
background.graphics.endFill();
}
with this code you will be able to chnage the background size everytime, and you will not be affecting the other components.

Your roundRect class size has been fixed, so that it was making no changes when u r increasing the width.
Make two public parameters for both width(Bwidth) and height(Bheight) and access it.
like
button.Bwidth = 100;

package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.Shape;
public class customButton extends Sprite
{
private var background:Shape;
public var bgColor:uint;
public var borderColor:uint;
public var borderSize:uint;
public var cornerRadius:uint;
public var bWidth:Number;
public var bHeight:Number;
private var label:TextField;
public function customButton(text:String, bW:Number, bH:Number)
{
super();
this.bWidth = bW;
this.bHeight = bH;
background = new Shape();
borderSize = 1;
borderColor = 0x666666;
bgColor = 0xFFCC00;
cornerRadius = 9;
label = new TextField();
label.text = text;
var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = 0;
format.size = 38;
format.underline = true;
label.defaultTextFormat = format;
addChild(background);
addChild(label);
buttonMode = true;
mouseChildren = false;
background.graphics.lineStyle(borderSize, borderColor);
background.graphics.beginFill(bgColor);
background.graphics.drawRoundRect(0, 0, bWidth, bHeight, cornerRadius);
background.graphics.endFill();
}
}
}
TRy this

Related

Duplicating video Stream Actionscript 3

Good Morning,
i am working on a video class, using a CRTMP Server for streaming. This works fine, but for my solution i need to duplicate the video stream (for some effects).
I googled for duplicate MovieClips and tried to duplicate the video like this.
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.media.*;
import flash.system.*;
import flash.utils.ByteArray;
public class Main extends MovieClip
{
public var netStreamObj:NetStream;
public var nc:NetConnection;
public var vid:Video;
public var vid2:Video;
public var streamID:String;
public var videoURL:String;
public var metaListener:Object;
public function Main()
{
init_RTMP();
}
private function init_RTMP():void
{
streamID = "szene3.f4v";
videoURL = "rtmp://213.136.73.230/maya";
vid = new Video(); //typo! was "vid = new video();"
vid2 = new Video();
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
nc.client = {onBWDone: function():void
{
}};
nc.connect(videoURL);
}
private function onConnectionStatus(e:NetStatusEvent):void
{
if (e.info.code == "NetConnection.Connect.Success")
{
trace("Creating NetStream");
netStreamObj = new NetStream(nc);
metaListener = new Object();
metaListener.onMetaData = received_Meta;
netStreamObj.client = metaListener;
netStreamObj.play(streamID);
vid.attachNetStream(netStreamObj);
//vid2.attachNetStream(netStreamObj); // wont work
addChild(vid);
// addChild(vid2); // wont work either
//intervalID = setInterval(playback, 1000);
}
}
private function asyncErrorHandler(event:AsyncErrorEvent):void
{
trace("asyncErrorHandler.." + "\r");
}
private function received_Meta(data:Object):void
{
var _stageW:int = stage.stageWidth;
var _stageH:int = stage.stageHeight;
var _videoW:int;
var _videoH:int;
var _aspectH:int;
var Aspect_num:Number; //should be an "int" but that gives blank picture with sound
Aspect_num = data.width / data.height;
//Aspect ratio calculated here..
_videoW = _stageW;
_videoH = _videoW / Aspect_num;
_aspectH = (_stageH - _videoH) / 2;
vid.x = 0;
vid.y = _aspectH;
vid.width = _videoW;
vid.height = _videoH;
vid2.x = 0;
vid2.y = _aspectH ;
}
}
It should be possible to duplicate the video stream. 2 Instance of the same videoStream. What am i doing wrong ?
Thanks for help.
"This means that i have to double the netstream. This is not what i want."
"I tried to duplicate the video per Bitmap.clone. But i got an sandbox violation."
You can try the workaround suggested here: Netstream Play(null) Bitmapdata Workaround
I'll show a quick demo of how it can be applied to your code.
This demo code assumes your canvas is width=550 & height=400. Keep that ratio if scaling up.
package
{
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.media.*;
import flash.system.*;
import flash.utils.ByteArray;
import flash.geom.*;
import flash.filters.ColorMatrixFilter;
public class Main extends MovieClip
{
public var netStreamObj:NetStream;
public var nc:NetConnection;
public var vid:Video;
public var streamID:String;
public var videoURL:String;
public var metaListener:Object;
public var vid1_sprite : Sprite = new Sprite();
public var vid2_sprite : Sprite = new Sprite();
public var vid2_BMP : Bitmap;
public var vid2_BMD : BitmapData;
public var colMtx:Array = new Array(); //for ColorMatrix effects
public var CMfilter:ColorMatrixFilter;
public function Main()
{
init_RTMP();
}
private function init_RTMP():void
{
streamID = "szene3.f4v";
videoURL = "rtmp://213.136.73.230/maya";
vid = new Video();
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
nc.client = { onBWDone: function():void { } };
//nc.connect(null); //for file playback
nc.connect(videoURL); //for RTMP streams
}
private function onConnectionStatus(e:NetStatusEvent):void
{
if (e.info.code == "NetConnection.Connect.Success")
{
trace("Creating NetStream");
netStreamObj = new NetStream(nc);
metaListener = new Object();
metaListener.onMetaData = received_Meta;
netStreamObj.client = metaListener;
//netStreamObj.play("vid.flv"); //if File
netStreamObj.play(streamID); //if RTMP
vid.attachNetStream(netStreamObj);
}
}
private function asyncErrorHandler(event:AsyncErrorEvent):void
{
trace("asyncErrorHandler.." + "\r");
}
private function received_Meta(data:Object):void
{
trace("Getting metadata");
var _stageW:int = stage.stageWidth;
var _stageH:int = stage.stageHeight;
var _videoW:int = data.width;
var _videoH:int = data.height;
var _aspectH:int = 0;
var Aspect_num:Number;
Aspect_num = data.width / data.height;
//Aspect ratio calculated here..
_videoW = _stageW;
_videoH = _videoW / Aspect_num;
_aspectH = (_stageH - _videoH) / 2;
trace("_videoW : " + _videoW);
trace("_videoW : " + _videoH);
trace("_aspectH : " + _aspectH);
vid.x = 0;
vid.y = 0;
vid.width = _videoW;
vid.height = _videoH;
setup_Copy(); //# Do this after video resize
}
public function setup_Copy () : void
{
vid2_BMD = new BitmapData(vid.width, vid.height, false, 0);
vid2_BMP = new Bitmap( vid2_BMD );
vid1_sprite.addChild(vid);
vid1_sprite.x = 0;
vid1_sprite.y = 0;
addChild( vid1_sprite );
vid2_sprite.addChild( vid2_BMP );
vid2_sprite.x = 0;
vid2_sprite.y = vid.height + 5;
addChild( vid2_sprite );
stage.addEventListener(Event.ENTER_FRAME, draw_Video);
}
public function draw_Video (evt:Event) : void
{
if ( netStreamObj.client.decodedFrames == netStreamObj.decodedFrames ) { return; } // Here we skip multiple readings
//# Get bitmapdata directly from container of video
if ( vid1_sprite.graphics.readGraphicsData().length > 0 )
{
vid2_BMD = GraphicsBitmapFill(vid1_sprite.graphics.readGraphicsData()[0]).bitmapData;
}
effect_BitmapData(); //# Do an effect to bitmapdata
}
public function effect_BitmapData ( ) : void
{
//# Matrix for Black & White effect
colMtx = colMtx.concat([1/3, 1/3, 1/3, 0, 0]); // red
colMtx = colMtx.concat([1/3, 1/3, 1/3, 0, 0]); // green
colMtx = colMtx.concat([1/3, 1/3, 1/3, 0, 0]); // blue
colMtx = colMtx.concat([0, 0, 0, 1, 0]); // alpha
CMfilter = new ColorMatrixFilter(colMtx);
vid2_BMP.bitmapData.applyFilter(vid2_BMD, new Rectangle(0, 0, vid.width, vid.height), new Point(0, 0), CMfilter);
}
}
}

Simplebutton image is larger than it actually is

I'm creating a button I use beginBitmapFill method to add image to it. Everything works normally, the problem is that the image loaded by a Loader () is greater than it actually is
Class that creates the button
package mode {
import flash.display.Sprite;
import org.osmf.net.StreamingURLResource;
public class LoadButton extends Sprite
{
public var Save;
public function LoadButton(x:uint,save:String,url:String)
{
var button:CustomSimpleButton = new CustomSimpleButton(url);
button.x = x;
Save = save;
addChild(button);
}
}
}
import flash.display.*;
import flash.display.Bitmap;
import flash.display.DisplayObject;
import flash.display.Shape;
import flash.display.SimpleButton;
import flash.events.*;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.geom.Matrix;
class CustomSimpleButton extends SimpleButton
{
private var upColor:uint = 0xFFCC00;
private var overColor:uint = 0xCCFF00;
private var downColor:uint = 0x00CCFF;
private var sizew:uint = 100;
private var sizeh:uint = 88;
public function CustomSimpleButton(url:String)
{
downState = new ButtonDisplayState(downColor, sizew,sizeh,url);
overState = new ButtonDisplayState(overColor, sizew,sizeh,url);
upState = new ButtonDisplayState(upColor, sizew,sizeh,url);
hitTestState = new ButtonDisplayState(upColor, sizew * 2,sizeh,url);
hitTestState.x = -(sizew / 4);
hitTestState.y = hitTestState.x;
useHandCursor = true;
}
}
class ButtonDisplayState extends Shape
{
private var bgColor:uint;
private var size:uint;
private var sizeh:uint;
public function ButtonDisplayState(bgColor:uint, sizew:uint,sizeh:uint,url:String)
{
this.bgColor = bgColor;
this.size = sizew;
this.sizeh = sizeh;
draw(url);
}
private function draw(url:String):void
{
var myLoader:Loader = new Loader();
var image:Bitmap;
var uri = new URLRequest(url);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event)
{
image = new Bitmap(e.target.content.bitmapData);
graphics.beginBitmapFill(image.bitmapData);
graphics.drawRect(0, 0, 100, 88);
graphics.endFill();
});
myLoader.load(uri);
}
}
How the image is 100x88
as it is
The reason it is showing up cropped like that, is because the BitmapData is larger than the area you are filling.
Before filling the shape with the loaded BitmapData, you need to sale it down to the appropriate size.
Based on the answer to this question, you could do the following:
var scaleAmount:Number;
if(e.target.content.width >= e.target.content.height){
scaleAmount = size / e.target.content.width;
}else{
scaleAmount = sizeh / e.target.content.height;
}
var scaledBitmapData:BitmapData = scaleBitmapData(e.target.content.bitmapData, scaleAmount);
graphics.beginBitmapFill(scaledBitmapData);
graphics.drawRect(0, 0, size, sizeh);
graphics.endFill();
function scaleBitmapData(bitmapData:BitmapData, scale:Number):BitmapData {
scale = Math.abs(scale);
var width:int = (bitmapData.width * scale) || 1;
var height:int = (bitmapData.height * scale) || 1;
var transparent:Boolean = bitmapData.transparent;
var result:BitmapData = new BitmapData(width, height, transparent);
var matrix:Matrix = new Matrix();
matrix.scale(scale, scale);
result.draw(bitmapData, matrix);
return result;
}
It might be easier though, to just make your button state a Sprite and add the loaded Bitmap object and scale it directly:
class ButtonDisplayState extends Sprite
{
private var bgColor:uint;
private var image:Bitmap;
private var size:uint;
private var sizeh:uint;
public function ButtonDisplayState(bgColor:uint, sizew:uint,sizeh:uint,url:String)
{
this.bgColor = bgColor;
this.size = sizew;
this.sizeh = sizeh;
loadImage(url);
}
private function loadImage(url:String):void
{
var myLoader:Loader = new Loader();
var uri = new URLRequest(url);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event)
{
image = new Bitmap(e.target.content.bitmapData);
//scale the bitmap while retaining aspect ratio
//scale based off which dimension (width or height) is bigger
if(image.width >= image.height){
image.scaleX= size / image.width; //scale it to the width specified
image.scaleY = image.scaleX; //make the height aspect ratio match the widths
}else{
image.scaleY = sizeh /image.height;
image.scaleX = image.scaleY;
}
addChild(image); //add it to the display list of this object
});
myLoader.load(uri);
}
}

Any obvious reason why the font color is not ending up what I set it as in AS3?

I'm setting a font in a tab as 0x999999, but it's coming out a green/grey.
Here's my theme code:
import flash.text.Font;
import flash.text.TextFormat;
import flash.utils.getDefinitionByName;
public class Theme
{
[Embed(source = "fonts/Volter__28Goldfish_29.ttf", fontName="Volter (Goldfish)",mimeType="application/x-font", unicodeRange='U+0020-U+007E')]
static private var EmbededVolter:Class;
static private var inited:Boolean;
static public function init():void {
if (inited) return;
try{
Font.registerFont(getDefinitionByName("Volter") as Class);
}catch(e:Error){
Font.registerFont(EmbededVolter);
}
inited = true;
}
static public var fontVolterSize:uint = 9;
static public var textColor:uint = 0x999999
static public var embedFonts:Boolean = false;
static public var defaultTextFormat:TextFormat = new TextFormat("Volter (Goldfish)", fontVolterSize, textColor);
}
The code for setting the tab properties:
public function TabTitle(name:String) {
this.name = name;
this.mouseChildren = false;
this.buttonMode = true;
bg = new Shape();
bg.alpha = 0;
bg.graphics.beginFill(0x2A2A2A);
bg.graphics.drawRect(0, 0, 100, 30);
bg.graphics.endFill();
addChild(bg);
t = new TextField();
t.x = 10;
t.autoSize = TextFieldAutoSize.LEFT;
t.defaultTextFormat = Theme.defaultTextFormat;
t.text = name;
t.y = 7;
bg.width = t.width + 20;
addChild(t);
addEventListener(MouseEvent.MOUSE_OVER, handleOver);
addEventListener(MouseEvent.MOUSE_OUT, handleOver);
}
And the difference in color between the above dynamic set font (top text) and the same font set to 0x999999 in a static text field (bottom):
I don't have any transparent alpha setting on the font, but if I did, the background is a grey shade anyway.
Thanks for taking a look.
Since you're embedding the font, you need to tell it to your TextField :
t.embedFonts = true;

Null object reference actionscript 3

i make a class like this:
package
{
import flash.display.Sprite;
import flash.text.TextField;
public class Hint extends Sprite
{
public static var _instance:Hint = null;
public function Hint()
{
_instance = this;
}
public function DrawHintText():void
{
Const._scoreText = new TextField();
Const._scoreTextHolder = new TextField();
Const._highScoreText = new TextField();
Const._highScoreTextHolder = new TextField();
Const._timeLeft = new TextField();
Const._timeLeftHolder = new TextField();
Const._scoreTextHolder.textColor = 0xFFFFFF;
Const._scoreTextHolder.x = stage.stageWidth - 350;
Const._scoreTextHolder.y = 100;
Const._scoreTextHolder.text = "Score: ";
Const._scoreTextHolder.selectable = false;
Const._scoreText.textColor = 0xFFFFFF;
Const._scoreText.x = stage.stageWidth - 250;
Const._scoreText.y = 100;
Const._scoreText.text = "--";
Const._scoreText.selectable = false;
Const._highScoreTextHolder.textColor = 0xFFFFFF;
Const._highScoreTextHolder.x = stage.stageWidth - 350;
Const._highScoreTextHolder.y = 150;
Const._highScoreTextHolder.text = "High Score: ";
Const._highScoreTextHolder.selectable = false;
Const._highScoreText.textColor = 0xFFFFFF;
Const._highScoreText.x = stage.stageWidth - 250;
Const._highScoreText.y = 150;
Const._highScoreText.text = "--";
Const._highScoreText.selectable = false;
Const._timeLeftHolder.textColor = 0xFF0000;
Const._timeLeftHolder.x = stage.stageWidth - 350;
Const._timeLeftHolder.y = 200;
Const._timeLeftHolder.text = "Time Left: ";
Const._timeLeftHolder.selectable = false;
Const._timeLeft.textColor = 0xFF0000;
Const._timeLeft.x = stage.stageWidth - 275;
Const._timeLeft.y = 200;
Const._timeLeft.text = "00:00";
Const._timeLeft.selectable = false;
addChild(Const._scoreText);
addChild(Const._scoreTextHolder);
addChild(Const._highScoreText);
addChild(Const._highScoreTextHolder);
addChild(Const._timeLeft);
addChild(Const._timeLeftHolder);
}
}
}
and i called on the GameManager:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
[SWF(width='1366',height='768',backgroundColor='#000000',frameRate='30')]
public class GameManager extends Sprite
{
public function GameManager():void
{
DrawHintText();
GenerateField();
ShowField();
GenerateGems();
}
private function GenerateField():void
{
Const._gridField = new Array();
for (var i:uint = 0; i < Const._gridSizeY; i++)
{
Const._gridField[i] = new Array();
for (var j:uint = 0; j < Const._gridSizeX; j++)
{
Const._gridField[i][j] = 0;
}
}
}
private function ShowField():void
{
Const._fieldSprite = new Sprite();
addChild(Const._fieldSprite);
Const._fieldSprite.graphics.lineStyle(1, 0xFFFFFF);
for (var i:uint = 0; i < Const._gridSizeY; i++)
{
for (var j:uint = 0; j < Const._gridSizeX; j++)
{
Const._fieldSprite.graphics.beginFill(0x666666);
Const._fieldSprite.graphics.drawRect(25 + 65 * j, 80 + 60 * i, 65, 60);
Const._fieldSprite.graphics.endFill();
}
}
}
private function DrawHintText():void
{
Hint._instance.DrawHintText();
}
private function GenerateGems():void
{
}
}
}
and here is the Const:
package
{
import flash.display.Sprite;
import flash.text.TextField;
public class Const
{
public static var _gridField:Array;
public static var _fieldSprite:Sprite;
public static var _scoreText:TextField;
public static var _scoreTextHolder:TextField;
public static var _highScoreText:TextField;
public static var _highScoreTextHolder:TextField;
public static var _timeLeft:TextField;
public static var _timeLeftHolder:TextField;
public static const _gridSizeX:Number = 10;
public static const _gridSizeY:Number = 10;
public function Const()
{
}
}
}
when i run the code, i got the error:
Where do i miss some code?
Invoking function DrawHintText() in Hint._instance.DrawHintText(); doesn't create instance of class Hint.
Try to substite your public variable with public getter.
private static var __instance:Hint = null;
public static function get _instance():Hint {
if (!__instance) {
__instance = new Hint();
}
return __instance;
}
And drop constructor, because getter makes it unnecessary.
Might be a silly question, but are you actually creating instance of Hint somewhere?
Because as long as you don't do new Hint() that Hint._instance will be null.
Try something like:
public class Hint extends Sprite
{
private static var _instance:Hint;
public function Hint()
{
if (_instance) throw new Error("Hint... use getInstance()");
_instance = this;
}
public static function getInstance():Hint
{
if (!_instance) new Hint();
return _instance;
}
//DrawHintText >> rename to drawHintText
public function drawHintText():void
{
//your code here
}
}
And to use:
Hint.getInstance().drawHintText();
Use Flash Builder, which costs some money, or Flash Develop. which is free. Both will show you compile time errors, with line numbers and location file.
Also at runtime will show you line number and the .as file where you have the problem.
Also autocomplete cuts your coding time by at least half time.

How to change a type Number variable through a text field

I was working on a little practice project in AS3 and I ran into a problem. I'm making a MS Paint style drawing program and I want the user to be able to change the brush size with an input field. I have two issues. The first is that I don't know how to make an input field through code, and the second is I don't know how to pass the variable from the input field on the stage to the variable controlling the brush size.
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.events.KeyboardEvent;
public class DrawingApp extends Sprite
{
var square0:Sprite;
var textField: TextField;
var textField2: TextField;
var textField3: TextField;
var square1:Sprite;
var square2:Sprite;
var square3:Sprite;
var lineColor:uint = 0x000000;
var brushSize:Number = 1;
public function DrawingApp()
{
square0 = new Sprite();
addChild(square0);
square0.graphics.lineStyle(1,0x000000);
square0.graphics.beginFill(0x000000);
square0.graphics.drawRect(0,0,30,20);
square0.graphics.endFill();
square0.x = 500;
square0.y = 10;
square1 = new Sprite();
addChild(square1);
square1.graphics.lineStyle(1,0x000000);
square1.graphics.beginFill(0x0000FF);
square1.graphics.drawRect(0,0,30,20);
square1.graphics.endFill();
square1.x = 500;
square1.y = 40;
square2 = new Sprite();
addChild(square2);
square2.graphics.lineStyle(1,0x000000);
square2.graphics.beginFill(0xff0000);
square2.graphics.drawRect(0,0,30,20);
square2.graphics.endFill();
square2.x = 500;
square2.y = 70;
textField = new TextField;
addChild(textField);
textField.x = 500;
textField.y = 100;
textField.width = 30;
textField.height = 20;
textField.text = "Eraser";
textField.selectable = false;
textField.border = true;
textField2 = new TextField;
addChild(textField2);
textField2.x = 500;
textField2.y = 130;
textField2.width = 30;
textField2.height = 20;
textField2.text = "Clear";
textField2.selectable = false;
textField2.border = true;
textField3 = new TextField;
addChild(textField3);
textField3.x = 500;
textField3.y = 160;
textField3.width = 30;
textField3.height = 20;
textField3.text = brushSize;
textField3.selectable = true;
textField3.border = true;
init();
}
private function init():void
{
graphics.lineStyle(1, lineColor, 1);
square0.addEventListener(MouseEvent.CLICK, changetoDefault);
square1.addEventListener(MouseEvent.CLICK, changetoBlue);
square2.addEventListener(MouseEvent.CLICK, changetoRed);
textField.addEventListener(MouseEvent.CLICK, changetoEraser);
textField2.addEventListener(MouseEvent.CLICK, clearAll);
textField3.addEventListener(KeyboardEvent.KEY_DOWN, adjustSize);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseIsDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseIsUp);
}
private function changetoEraser(event: MouseEvent):void
{
lineColor = 0xffffff;
graphics.lineStyle(brushSize, lineColor, 1);
}
private function adjustSize(event: KeyboardEvent)
{
brushSize = textField3.text;
graphics.lineStyle(brushSize, lineColor, 1);
}
private function clearAll(event:MouseEvent):void
{
graphics.clear();
graphics.lineStyle(brushSize, lineColor, 1);
}
private function changetoDefault(event: MouseEvent):void
{
lineColor = 0x000000;
graphics.lineStyle(brushSize, lineColor, 1);
}
private function changetoBlue(event: MouseEvent):void
{
lineColor = 0x0000ff;
graphics.lineStyle(brushSize, lineColor, 1);
}
private function changetoRed(event: MouseEvent):void
{
lineColor = 0xff0000;
graphics.lineStyle(brushSize, lineColor, 1);
}
private function mouseIsDown(event: MouseEvent):void
{
graphics.moveTo(mouseX, mouseY);
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveMyMouse);
}
private function mouseIsUp(event: MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveMyMouse);
}
private function moveMyMouse(event:MouseEvent):void
{
graphics.lineTo(mouseX, mouseY);
}
}
}
I realized that setting brushSize to textField3.text wouldn't work because it's converting string to number. I have no clue how I can make it so that way this works. Thanks to anyone who can shed some light on this for me.
You can make an input field from code in Flash by making a TextField and setting its type to TextFieldType.INPUT.
In order to convert a string to a number you will have to use parseFloat() or parseInt() (I don't know whether you need an integer or a float by heart).
Though minimal, I do think this should be enough to help you get you going again.