I created an xfl file called myfile.xfl and linked it to Main.as.
Because my script is kind of extensive, I planned to distribute it over several as-files.
The Main.as looks like this and is linked correctly from the xfl-file:
package {
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.xml.*;
import flash.events.Event;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.text.*;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
public class Main extends MovieClip
{
var bestand:String;
var titelFont:Font = new Titelfont();
var werkFont:Font = new Werkfont();
var titelfmt:TextFormat = new TextFormat();
var werkfmt:TextFormat = new TextFormat();
var inputfmt:TextFormat = new TextFormat();
var snd:Sound;
var cnl:SoundChannel = new SoundChannel();
var xmlLoader:URLLoader;
var myList:XML;
var introClip:Containerclip = new Containerclip();
var dicteeClip:Containerclip = new Containerclip();
public function Main()
{
//DE INFORMATIE UIT DE URL //
//bestand = LoaderInfo(this.root.loaderInfo).parameters["bestand"];
//bestand += ".xml";
bestand = "xml/bron.xml"
loadXml();
}
function loadXml():void
{
xmlLoader = new URLLoader(new URLRequest(bestand));
xmlLoader.addEventListener(Event.COMPLETE,xmlLoaded);
}
function xmlLoaded(event:Event):void
{
myList = new XML(event.target.data);
myList.ignoreWhite = true;
createTextFormat();
createIntro();
createDictee();
createResultaten();
}
include "CreateStuff.as"
include "Dictee.as"
}
}
You can see my includes at the end: createstuff.as and dictee.as.
I was working on a third include, called Sound.as, and tried to preview my file when error 5007 popped up. "5007: An ActionScript file must have at least one externally visible definition"
I removed the include Sound.as line from my Main.as, thinking that must have cause the problem for some reason. When I republished the error popped up again, and flash automatically reopened the Sound.as file. While there is no link from anywhere to it anymore.
My other files are CreateStuff.as :
function createTextFormat() : void {
titelfmt.size = 32;
titelfmt.color = 0x003399;
titelfmt.font = titelFont.fontName;
werkfmt.size = 24;
werkfmt.color = 0x000000;
werkfmt.font = werkFont.fontName;
inputfmt.size = 32;
inputfmt.color = 0x333333;
inputfmt.font = werkFont.fontName;
//inputfmt.
}
function createTextfield (fmt) : TextField {
var tf:TextField = new TextField();
tf.defaultTextFormat = fmt;
if (fmt == titelfmt) {
tf.multiline = false;
tf.autoSize = "center";
tf.selectable = false;
tf.y = 10;
tf.x = stage.stageWidth / 2;
} else if (fmt == werkfmt) {
tf.autoSize = "left";
tf.multiline = true;
} else if (fmt == inputfmt) {
tf.multiline = true;
tf.width = 300;
tf.height = 40;
tf.border = true;
tf.borderColor = 0xCCCCCC;
tf.type = TextFieldType.INPUT;
}
return (tf);
}
function createIntro() : void {
introClip.x = 0;
introClip.y = 0;
introClip.name = "introClip";
var nttf:TextField = createTextfield(titelfmt);
nttf.text = "Instructie"
introClip.addChild(nttf);
var itf:TextField = createTextfield(werkfmt);
itf.text = "Wat weet jij nog?\nLuister naar de woorden.\nSchrijf ze op.";
itf.width = 300;
itf.x = stage.stageWidth / 2 - 150;
itf.y = 120;
itf.selectable = false;
introClip.addChild(itf);
var startBtn:Beginknop = new Beginknop();
startBtn.x = stage.stageWidth / 2 - startBtn.width / 2;
startBtn.y = 300;
startBtn.addEventListener(MouseEvent.CLICK, showDictee);
introClip.addChild(startBtn);
addChild(introClip);
}
function createResultaten() : void {
}
And Dictee.as :
function createDictee () : void {
dicteeClip.x = 0;
dicteeClip.y = 0;
var nttf:TextField = createTextfield(titelfmt);
nttf.text = "Dictee"
dicteeClip.addChild(nttf);
var iptf:TextField = createTextfield(inputfmt);
iptf.x = 135;
iptf.y = 205;
dicteeClip.addChild(iptf)
var dicteeSpeaker:Luidspreker = new Luidspreker();
dicteeSpeaker.x = stage.stageWidth / 2 - dicteeSpeaker.width/2;
dicteeSpeaker.y = iptf.y - dicteeSpeaker.height - 40;
//dicteeSpeaker.addEventListener (MouseEvent.CLICK, playSnd);
dicteeSpeaker.name = dicteeSpeaker;
dicteeClip.addChild(dicteeSpeaker);
var volgendeKnop:Controleknop = new Controleknop();
volgendeKnop.x = iptf.x + iptf.width + 30;
volgendeKnop.y = iptf.y;
dicteeClip.addChild(volgendeKnop);
}
function showDictee(event:MouseEvent) : void {
addChild(dicteeClip);
removeChild(introClip);
speelDictee();
}
function speelDictee() : void {
//bepaal geluid
//afspelen geluid
//volgende knop-actie
}
I initially called the file Sound.as but thought the error might come from that Sound is a reserved word, so I changed it to Sounds.as
I'm well out of my depth here and I have no clue why the error pops up. Or the file Sound.as while it is not linked.
My flash version is Flash-CC but the tag doesn't exist yet so I couldn't specify it in the tags.
#5007 is commonly caused by
Not correctly using a package
Including an import statement any level below the package level
I'm answering this question because for some reason the error disappeared and I have no idea why. I don't want to leave it open, I'd prefer to delete it as it's of no use to anyone but I'm not sure how to do that.
I create several textfields when the click event is fired. Now I want to change the text format of any selected textfield. But the format is just applied to the last created textfield. I tried the following:
function _txtbtn(e:*):void
{
myText = new TextField();
mc3 = new MovieClip();
myText.text = "text...";
myFormat.font = "Arial";
myFormat.color = txt_color()
myText.setTextFormat(myFormat);
mc3.addChild(myText);
addChild(mc3);
mc3.x = _can.x;
mc3.y = p;
p= mc3.y+mc3.height+10;
mc3.addEventListener(MouseEvent.MOUSE_DOWN,_select)
}
function _select(e:MouseEvent):void
{
tool_stage.combo.addEventListener(Event.CHANGE,_font)
}
function _font(e:Event):void
{
format.font = tool_stage.combo.selectedLabel;
myText.setTextFormat(format);
}
It is right, because the variable myText refers to the last Object.
Instead of this you can get the current TextField object from the Event.
Each event has currentTarget value, which refers to the Object that has fired the Event.
You then can cast the currentTarget to your type and do the action with it.
Unfortunately I don't have your whole code, that is why I have my own version.
Have a look at it, I think it can help you.
//Main.as
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
public class Main extends Sprite
{
private var button:Sprite;
private var p:int = 50;
private var x0:int = 20;
public function Main()
{
init();
}
private function init():void
{
button = new Sprite();
button.graphics.beginFill(0xFFCC00);
button.graphics.drawRect(0, 0, 80, 20);
button.graphics.endFill();
button.addEventListener(MouseEvent.CLICK, onBtnClick);
this.addChild(button);
}
private function onBtnClick(e:*):void
{
var myFormat:TextFormat = new TextFormat();
var myText:TextField = new TextField();
var mc3:MovieClip = new MovieClip();
myText.text = "text...";
myFormat.font = "Arial";
myFormat.color = 0x000000;
myText.setTextFormat(myFormat);
mc3.addChild(myText);
addChild(mc3);
mc3.x = x0;
mc3.y = p;
p= mc3.y+mc3.height+10;
myText.addEventListener(MouseEvent.CLICK, onTextClick)
}
private function onTextClick(evt:MouseEvent):void
{
var newFormat:TextFormat = new TextFormat();
newFormat.size = 30;
newFormat.font = "Verdana";
(evt.currentTarget as TextField).setTextFormat(newFormat);
}
}
}
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
I want to be able to write a paragraph in a textfield, and then click and hold, or do some similar gesture, and have the entire paragraph selected. I'm then going to drag it (using bitmapdata or whatever) to another textfield.
In order to do this, I need to be able to detect where a given paragraph ends. So I'm trying to do that with the following code, which searches for "\n" in the text. It isn't finding it. Can anyone see why (or suggest another technique for doing this)?
TIA,
David
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldType;
import view.controls.CustomButton;
public class Main extends Sprite
{
private var bt:CustomButton;
private var tf:TextField;
public function Main()
{
tf = new TextField();
tf.name = "tfield";
tf.type = TextFieldType.INPUT;
tf.width = 400;
tf.height = 200;
tf.x = 100;
tf.y = 100;
tf.selectable = true;
tf.border = true;
tf.background = true;
tf.backgroundColor = 0xFFFFFF;
tf.multiline = true;
tf.text = "Like most of the things I get excited about and share with you, this technique really doesn’t have much to it, but I love its elegance, how it works in the background and gets out of your way. While it’s really simple I think this one is a real gem, ’cause when you look at a class that uses it, it looks like magic!\n\nOkay, so you know how when you’re writing a site or app that’s of a small to medium scale, you default to storing data in XML, and you map that XML to model classes, usually pretty directly? Or, maybe you use a configuration file for your site to load in some constants or something, and XML is a pretty easy choice for this. With E4X you can really parse through that XML quickly.";
tf.wordWrap = true;
addChild(tf);
bt = new CustomButton("Detect", 0xFFFFFF, 0x000000,50,20);
bt.x = 250;
bt.y = 350;
addChild(bt);
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void
{
bt.addEventListener(MouseEvent.CLICK, clickHandler);
}
private function clickHandler(e:MouseEvent):void
{
var lineBreak:int = tf.text.indexOf("\n");
trace(lineBreak);
}
}
}
//custom button class
package view.controls
{
import flash.display.GradientType;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
public class CustomButton extends Sprite
{
private var textColor:uint = 0x000000;
private var myColor:uint = 0xFEEE9E9;
private var btnWidth:Number;
private var btnHeight:Number;
public function CustomButton(buttonText:String, gradientColor:uint, borderColor:uint, myBtnWidth:Number, myBtnHeight:Number)
{
var colors:Array = new Array();
var alphas:Array = new Array(1, 1);
var ratios:Array = new Array(0, 255);
var gradientMatrix:Matrix = new Matrix();
var lineThickness:Number = 1;
//var myColor:uint = 0xFF0000;
gradientMatrix.createGradientBox(myBtnWidth, myBtnHeight, Math.PI/2, 0, 0);
this.btnWidth = myBtnWidth;
this.btnHeight = myBtnHeight;
var ellipseSize:int = 20;
var btnUpState:Sprite = new Sprite();
colors = [0xFFFFFF, myColor];
//btnUpState.graphics.lineStyle(1, brightencolor(myColor, -500));
btnUpState.graphics.lineStyle(lineThickness, borderColor);
btnUpState.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, gradientMatrix);
btnUpState.graphics.drawRoundRect(0, 0, myBtnWidth, myBtnHeight, ellipseSize, ellipseSize);
btnUpState.addChild(createButtonTextField(buttonText, textColor));
//
var btnOverState:Sprite = new Sprite();
colors = [0xFFFFFF, brightencolor(myColor, 50)];
//btnOverState.graphics.lineStyle(1, brightencolor(myColor, -50));
btnOverState.graphics.lineStyle(lineThickness, borderColor);
btnOverState.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, gradientMatrix);
btnOverState.graphics.drawRoundRect(0, 0, myBtnWidth, myBtnHeight, ellipseSize, ellipseSize);
btnOverState.addChild(createButtonTextField(buttonText, textColor))
//
var btnDownState:Sprite = new Sprite();
//colors = [brightencolor(myColor, -15), brightencolor(myColor, 50)];
btnDownState.graphics.lineStyle(lineThickness, borderColor);
btnDownState.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, gradientMatrix);
btnDownState.graphics.drawRoundRect(0, 0, myBtnWidth, myBtnHeight, ellipseSize, ellipseSize);
btnDownState.addChild(createButtonTextField(buttonText, textColor))
//
this.btnWidth = myBtnWidth;
this.btnHeight = myBtnHeight;
var myButton:SimpleButton = new SimpleButton(btnUpState, btnOverState, btnDownState, btnOverState);
myButton.name = buttonText;
addChild(myButton);
}
private function createButtonTextField(Text:String, textcolor:uint):TextField {
var myTextField:TextField = new TextField();
myTextField.textColor = textcolor;
myTextField.selectable = false;
myTextField.width = btnWidth;
myTextField.height = btnHeight;
var myTextFormat:TextFormat = new TextFormat();
myTextFormat.align = TextFormatAlign.CENTER;
myTextFormat.font = "Arial";
myTextFormat.size = 12;
myTextField.defaultTextFormat = myTextFormat;
myTextField.text = Text;
myTextField.x = (btnWidth/2)-(myTextField.width/2);
myTextField.y = 1;
return myTextField;
}
private function brightencolor(color:int, modifier:int):int {
var hex:Array = hexToRGB(color);
var red:int = keepInBounds(hex[0]+modifier);
var green:int = keepInBounds(hex[1]+modifier);
var blue:int = keepInBounds(hex[2]+modifier);
return RGBToHex(red, green, blue);
}
private function hexToRGB (hex:uint):Array {
var colors:Array = new Array();
colors.push(hex >> 16);
var temp:uint = hex ^ colors[0] << 16;
colors.push(temp >> 8);
colors.push(temp ^ colors[1] << 8);
return colors;
}
private function keepInBounds(number:int):int {
if (number < 0) number = 0;
if (number > 255) number = 255;
return number;
}
private function RGBToHex(uR:int, uG:int, uB:int):int {
var uColor:uint;
uColor = (uR & 255) << 16;
uColor += (uG & 255) << 8;
uColor += (uB & 255);
return uColor;
}
}
}
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.display.Sprite;
import flash.events.MouseEvent;
var tf:TextField = new TextField();
tf.type = TextFieldType.INPUT;
tf.width = 400;
tf.height = 200;
tf.x = stage.stageWidth / 2 - tf.width / 2;
tf.y = stage.stageHeight / 2 - tf.height / 2;
tf.selectable = true;
tf.border = true;
tf.background = true;
tf.backgroundColor = 0xCCCCCC;
tf.multiline = true;
tf.wordWrap = true;
tf.text = "Enter text here, hit enter to create new paragraphs."
stage.addChild(tf);
var pushme:Sprite = new Sprite();
pushme.graphics.beginFill(0xCCCCCC, 1);
pushme.graphics.drawRect(0, 0, 100, 25);
pushme.graphics.endFill();
pushme.buttonMode = true;
pushme.addEventListener(MouseEvent.CLICK, pushedme);
pushme.x = tf.x + tf.width - pushme.width;
pushme.y = tf.y + tf.height + 15;
stage.addChild(pushme);
function pushedme(e:MouseEvent):void {
var paragraphCounter:int = 1;
var curParagraphOffset:int = -1;
for (var i:int = 0; i < tf.numLines; i++) {
if (tf.getFirstCharInParagraph(tf.getLineOffset(i)) == tf.getLineOffset(i) && tf.getFirstCharInParagraph(tf.getLineOffset(i)) > curParagraphOffset) {
trace("Paragraph " + paragraphCounter + " text: \n" + tf.text.substr(tf.getFirstCharInParagraph(tf.getLineOffset(i)), tf.getParagraphLength(tf.getFirstCharInParagraph(tf.getLineOffset(i)))));
paragraphCounter++;
curParagraphOffset = tf.getFirstCharInParagraph(tf.getLineOffset(i));
}
}
}
Paragraph Detection
Step by step
You can probably take it from here and ignore the empty paragraphs, but to explain:
Setup textfield, input more text manually (as shown)
Create the button to check for paragraphs
Set paragraph counter to 1 so it outputs properly for the first detected paragraph
Paragraph offset needs to be -1 for the if statement to be satisfied for the first iteration as the first paragraph should be at character 0
If the first character in the paragraph is also the first character on this line and we're not part of the previous paragraph, we have a new paragraph on this line.
Output the paragraph text based on the first character and the length of the paragraph by taking a substring of the tf.text property.
Increment paragraphCounter and set the curParagraphOffset to the new first character index.
If you want to be able to click and hold on a given paragraph, all you need to do is call:
tf.getCharIndexAtPoint(x, y)
on click of your text field. You can then find the first character in that paragraph, grab the substring based on the paragraph length, and go from there.
Hope this helps!
I have tried to do this here http://wonderfl.net/c/9Kdv but what I want is not this
alt text http://reboltutorial.com/images/flash-banner-trial.png
but rather the equivalent of this. As I'm flash newbie I don't see how:
(source: reboltutorial.com)
action script 3 code below:
package {
import flash.display.*;
import flash.text.*;
import flash.net.URLRequest;
import flash.filters.*;
import flash.geom.Rectangle;
public class FlashTest extends Sprite {
public function FlashTest() {
var mc:MovieClip = new MovieClip();
mc.graphics.beginFill(0x400000);
mc.graphics.drawRoundRect(0, 0, 278, 170,25,25);
mc.graphics.endFill();
mc.x = 80;
mc.y = 60;
addChild(mc);
//from tut http://blog.0tutor.com/post.aspx?id=116
var filt:GlowFilter = new GlowFilter();
var filt_shadow:DropShadowFilter = new DropShadowFilter();
//here we add some properties to the two filters, the glow filter we give a color.
filt.color = 0xFF0000;
//and how much it should blur.
filt.blurX = 7;
filt.blurY = 7;
//then the dropshadow filter, also how much it should blur on the object.
filt_shadow.blurX = 4;
filt_shadow.blurY = 4;
//and finally an alpha, the alpha goes from 1 to 0, 1 being fully visible and 0 is transparent, then of cause .5 is just in between.
filt_shadow.alpha = .4;
mc.filters = [filt,filt_shadow];
var theTextField:TextField = new TextField();
theTextField.border = false;
theTextField.x = 30;
theTextField.y = 50;
theTextField.autoSize = TextFieldAutoSize.LEFT;
theTextField.text = "Experiment";
var myformat:TextFormat = new TextFormat();
myformat.color = 0xFFFFFF;
myformat.size =24;
myformat.align="center";
myformat.font = "Impact";
theTextField.setTextFormat(myformat);
mc.addChild(theTextField);
var url:String = "//www.rebol.com/graphics/reb-logo.gif";
var urlReq:URLRequest = new URLRequest(url);
var ldr:Loader = new Loader();
ldr.load(urlReq);
ldr.x=30;
ldr.y=88;
mc.addChild(ldr);
}
}
}
Instead of this line:
mc.graphics.beginFill(0x400000);
you can use beginGradientFill with the fillType set to GradientType.RADIAL. You would just need to adjust the focalPointRatio to make it offcenter. Check out the example in the docs for how to do this.