create new movieclip from class [duplicate] - actionscript-3

This question already exists:
Flash Action Script 3 design custom MovieClip as Button
Closed 9 years ago.
I am new to Flash AS3.I am trying to create a movie clip> I have created basic code but i need to convert it t package so i can create XX number of clips from it.
Can you help in making this simple package so I can use it like var btn:Button = new Button("title");
// mc_main
var mc_main:MovieClip = new MovieClip();
//mc.graphics.lineStyle(1,0x0000CC);
mc_main.graphics.beginFill(0x0000CC);
mc_main.graphics.drawRect(0, 0, 400, 40);
mc_main.graphics.endFill();
mc_main.x = 80;
mc_main.y = 60;
addChild(mc_main);
//mc_txt
var mc_txt:TextField = new TextField();
mc_main.addChild(mc_txt);
mc_txt.text = 'Hello!';
mc_txt.x = 50;
mc_txt.y = 8;
//mc_txt Color
var tf:TextFormat = new TextFormat();
tf.size = 18;
tf.bold = true;
tf.font = "Arial"
tf.color = 0xFFFFFF;
mc_txt.setTextFormat(tf);
I came up with code this but doesnt work
package com.fladev.button
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Button extends MovieClip
{
private var Title:String;
private var Image:String;
public static var mc_main:MovieClip = new MovieClip();
public function Button(Title:String, Image:String) {
this.Title = Title;
this.Image = Image;
Draw_main();
}
private function Draw_main()
{
mc_main.graphics.beginFill(0x0000CC);
mc_main.graphics.drawRect(0, 0, 400, 40);
mc_main.graphics.endFill();
mc_main.x = 80;
mc_main.y = 60;
this.stage.addChild(mc_main);
}
}
}

Your mc_main variable has no reason to be static.
Also, don't add it to the stage; add it to the instance of Button.

Related

Add movieclips within another movieclip

I am having trouble embedding a child object to a movieClip with code using AS3 and I am not sure what I am doing wrong.
I am trying to create a new movieclip with code and add it to the stage. I am then trying to embed dynamically created objects inside that movieclip. When I trace the objects it shows that they are on the stage and not embedded into the movieclip. I looked through the forums but I didn't find an answer. Below is a watered down version of the code I have. Any help is appreciated.
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Sprite;
import flash.text.*;
import flash.net.URLRequest;
import flash.display.Loader;
var btnAImage:Loader = new Loader();
var image:URLRequest = new URLRequest("btn_A.png");
public function TextWithImage()
{
var TextField1:TextField = new TextField;
var myText1:String = "TEXT FIELD 1";
var BtnMovieClip:MovieClip = new MovieClip();
var btnPadding = 5;
addChild(BtnMovieClip); //add new MC to stage
btnAImage.load(image);
BtnMovieClip.addChild(btnAImage); // no error gets thrown. I am trying to add the btnAImage inside of my BtnMovieClip.
btnAImage.x = btnPadding; //I am able to reference the btnAImage without referencing the BtnMovieClip object.
btnAImage.y = btnPadding;
var screenW = stage.stageWidth;
TextField1.height = 40;
TextField1.width = 250;
var textPadding = 5;
var TextField1_fontFormat:TextFormat = new TextFormat ;
TextField1_fontFormat.size = 40;
TextField1_fontFormat.font = "TestFont";
TextField1.defaultTextFormat = TextField1_fontFormat;
BtnMovieClip.addChild(TextField1); // no error gets thrown. I am trying to add the text field inside of my BtnMovieClip.
TextField1.text = myText1;
TextField1.x = (btnAImage.width + textPadding);
TextField1.y = textPadding;
}
I wasn't preloading my image so here's how I fixed it. Thank you to Organis for the help!!!
public var btnAImage:Loader;
private function Question8_Answer()
{
trace("Question8_Answer() was called");
btnAImage = new Loader();
btnAImage.load(new URLRequest("btn_A.png"));
btnAImage.contentLoaderInfo.addEventListener(Event.COMPLETE, TextWithImage);
}
public function TextWithImage()
{
var TextField1:TextField = new TextField;
var myText1:String = "TEXT FIELD 1";
var BtnContainer:Sprite = new Sprite();
var btnPadding = 5;
addChild(BtnMovieClip);
BtnMovieClip.addChild(btnAImage);
btnAImage.x = btnPadding;
btnAImage.y = btnPadding;
var screenW = stage.stageWidth;
TextField1.height = 40;
TextField1.width = 250;
var textPadding = 5;
var TextField1_fontFormat:TextFormat = new TextFormat ;
TextField1_fontFormat.size = 40;
TextField1_fontFormat.font = "TestFont";
TextField1.defaultTextFormat = TextField1_fontFormat;
BtnMovieClip.addChild(TextField1); // no error gets thrown. I am trying to add the text field inside of my BtnMovieClip.
TextField1.text = myText1;
TextField1.x = (btnAImage.width + textPadding);
TextField1.y = textPadding;
BtnContainer.width = 300;
}

Updating and displaying a variable in AS3

I have been following an AS3 tutorial for an avoider game, the trouble is the tutorial is for CS5 and I am using Flash Develop. I am completely stuck trying to update and display the score. I have read through several posts on SO and elsewhere and still can't seem to fix it.
Here is the Score class that I call to display the score and it contains the function that updates the "score" value.
package
{
import flash.display.Sprite;
import flash.events.TextEvent;
import flash.text.TextField
import flash.text.TextFormat
public class Score extends Sprite
{
public var scoreDisplay:String;
public var currentValue:int;
public function Score()
{
updateDisplay()
}
public function updateDisplay():void
{
scoreDisplay = currentValue.toString();
var format:TextFormat = new TextFormat();
format.size = 25;
format.font = "Verdana"
var myText:TextField = new TextField();
myText.defaultTextFormat = format;
myText.text = scoreDisplay;
myText.background = true;
myText.autoSize
myText.width = 50;
myText.height = 35;
addChild(myText)
myText.x = 340
myText.y = 10
myText.mouseEnabled = false
}
public function addToValue( amountToAdd:Number ):void
{
trace("addToValue")
trace(currentValue)
currentValue = currentValue + amountToAdd;
trace(currentValue + " 1")
}
}
}
and here is the Game class I am running to increase the "score" per tick.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import Score;
public class Game extends Sprite
{
public var army:Array;
public var gameTimer:Timer;
public var player:Player;
public var background:Sprite;
public function Game()
{
army = new Array();
var newenemy:Enemy = new Enemy(100, -15);
army.push(newenemy);
addChild(newenemy);
player = new Player();
addChild(player);
player.x = mouseX;
player.y = mouseY;
gameTimer = new Timer(20);
gameTimer.addEventListener(TimerEvent.TIMER, move);
gameTimer.addEventListener(TimerEvent.TIMER, onTick);
gameTimer.start();
}
private function move(timerEvent:TimerEvent):void
{
player.x = mouseX;
player.y = mouseY;
for each (var enemy:Enemy in army)
{
enemy.moveDownABit();
if (player.hitTestObject(enemy))
{
gameTimer.stop();
dispatchEvent( new AvatarEvent( AvatarEvent.DEAD));
}
}
}
public function onTick(e:Event):void
{
if (Math.random() < .1)
{
var randomX:Number = Math.random() * 400;
var newEnemy:Enemy = new Enemy(randomX, -15);
army.push(newEnemy);
addChild(newEnemy);
var instanceScore:Score = new Score();
instanceScore.addToValue(10)
}
}
}
}
My trace function outputs "addToValue, 0, 10 1" and repeats that, never changing the 'curentValue' variable past 0. I can tell that the 'amountToAdd' is functioning properly but have no idea why 'currentValue' is always reset to 0.
questions on SO that I viewed;
Display dynamic variable on next key frame AS3
Avoider Game Tutorial: Score not working
The problem is this:
var instanceScore:Score = new Score();
This means that every time there is a tick, you create new score. When you create new score, it starts from 0. You update it to 10, and on the next tick, you ignore the old value of 10 by creating a whole new class. The new class has a value of 0, as it never has it's value increased before. Then you increase it again.. and a new tick starts (looping)
What you need to do is to instantiate score instance ONLY ONCE and save it as a class member variable. Then you can call increase to it.

Change format of any selected textfield in AS3?

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);
}
}
}

Can't access public properties of my Class

I'm still learning about object oriented programming...
I made my own simple button class to do horizontal buttons lists. I works fine, but...
package as3Classes {
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.text.TextField;
import flash.events.MouseEvent;
import fl.motion.MotionEvent;
import flash.text.TextFormat;
public class simpleButton extends MovieClip {
//var nome:String = new String();
var sub:Sprite = new Sprite();
public var realce:Sprite = new Sprite();
public var titulo:String;
public function simpleButton(nomeId:String, texto:String) {
this.name = nomeId;
this.titulo = texto;
this.useHandCursor = true;
this.buttonMode = true;
this.mouseChildren = false;
var txtf:TextFormat = new TextFormat();
txtf.font = "Arial";
var txt:TextField = new TextField();
txt.wordWrap = false;
txt.multiline = false;
txt.text = texto;
txt.setTextFormat(txtf);
txt.width = txt.textWidth+4;
txt.height = 22;
txt.x = 4;
txt.y = 2;
txt.cacheAsBitmap = true;
var fundo:Sprite = new Sprite();
fundo.graphics.beginFill(0xCCCCCC);
fundo.graphics.drawRect(0, 0, txt.width+8, 22);
fundo.graphics.endFill();
//var realce:Sprite = new Sprite();
this.realce.graphics.beginFill(0x00CC00);
this.realce.graphics.drawRect(0, 0, txt.width+8, 22);
this.realce.graphics.endFill();
this.sub.graphics.beginFill(0xFF0000);
this.sub.graphics.drawRect(0, 0, txt.width+8, 2);
this.sub.graphics.endFill();
this.addChild(fundo);
this.addChild(this.realce);
this.addChild(this.sub);
this.addChild(txt);
this.sub.alpha = 0;
this.sub.y = 22;
this.addEventListener(MouseEvent.MOUSE_OVER, mouseover);
this.addEventListener(MouseEvent.MOUSE_OUT, mouseout);
}
private function mouseover(e:MouseEvent){
sub.alpha = 1;
}
private function mouseout(e:MouseEvent){
sub.alpha = 0;
}
}
}
... when I try to access the "titulo" or set "realce" as alpha=1 (to display it as clicked) it returns undefined. I can only set or read proprieties inherited, as the name, alpha, etc. What is my conceptual mistake?
Yes! I found a way to avoid this issue: Since (as it seams) I can't access the internal public objects of "simpleButton" by display objects list, I create a private object (btns:Object) in the root of the main class (can be seen everywhere) and add the simpleButtons simultaneously as adding at display. The name is the same, so I can change the realce.alpha by btns and it will reflect in the display.
This is bizarre, because the e.target leads to the child instead of the object itself!
If somebody knows a better way, please let me know.

How to create a light effect inside a rectangle in Actionscript 3?

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.