Setting a TextField's text from a document class file - actionscript-3

I've searched for a while, and nothing is working for me. I'm trying to make an idle game with my friend (who's doing graphics), and I'm just learning AS3 so I thought it'd be an easy-ish project to start with. I don't know how to make a health textbox that outputs the remaining health left. I searched for at least an hour and nothing I found has worked. Here's all my coding. I have numbers embedded for the textbox. The textbox's instance name is "health" so it's not too hard to find.
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip {
public var originalValue:Number = 10;
public var remainingValue:Number = uint(originalValue);
public var clickDamage:Number = 1;
public var health:TextField = String(originalValue);
public var moneyCount:Number=0;
public var mineralValue:Number=5;
public var destroyedCount:Number=0;
mineral.addEventListener(MouseEvent.CLICK, takeDamage);
public function takeDamage(e:Event):void
{
remainingValue = remainingValue - clickDamage;
if (remainingValue <= 0) {
remainingValue = originalValue;
moneyCount += mineralValue;
destroyedCount++;
}
health.text=String(remainingValue);
}
}
}
All I have for symbols so far is my textbox(health) and my button(mineral). Also, what's the difference between an instance name for a textbox symbol, and the instance name for the textbox inside? Sorry if it's a bad question, I'm just really confused and it's ticking me off because I've been trying to work around this problem for a while.

I think your main issue is a lack of understanding of how class files work. I've reworked your class file below and added some comments:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip {
public var originalValue:Number = 10;
public var remainingValue:Number = 10; //no need to convert to uint
public var clickDamage:Number = 1;
public var health:TextField; //you can't assign a string to a text field var //health = String(originalValue);
public var moneyCount:Number=0;
public var mineralValue:Number=5;
public var destroyedCount:Number=0;
//mineral.addEventListener(MouseEvent.CLICK, takeDamage);
//in class files, you can't have lines of code outside of methods/functions create a constructor like below:
//the constructor function must match the name of class, and will run whenever you create an instance of this class
public function Main(){
mineral.addEventListener(MouseEvent.CLICK, takeDamage);
health.text = originalValue; //now assign the text to the text field
//this assumes you have a text field on the main timeline with the instance name of "health"
}
public function takeDamage(e:Event):void
{
remainingValue = remainingValue - clickDamage;
if (remainingValue <= 0) {
remainingValue = originalValue;
moneyCount += mineralValue;
destroyedCount++;
}
health.text= remainingValue.toString();
}
}
}
If (as may be gleamed from comment in your question), you have your health text field encapsulated in a symbol/movie clip, you'll need to access it like so:
myHealthSymbolInstanceName.myHealthTextBoxInstance.text = value;

Related

An if statement isn't checking if a variable has increased in amount or not

I am programming in AS3 on flash develop. I am using a program called flashpunk that adds in multiple presets for classes that i can use to make it easier to program. I have an if statement that is supposed to add a new graphic when a variable equals 1. If the user presses one on their keyboard then that variable does increase by 1. But when the variable increases by 1 and the if statement is supposed to check if it is one it doesn't add a new graphic like it's supposed too. I do have the if statement and the variable in different classes and i am not sure if i can do that, here is the code. The if statement that doesn't work is the one that is supposed to add a new background1.
Chapter
package
{
import net.flashpunk.Entity;
import net.flashpunk.World;
import net.flashpunk.utils.Input;
import net.flashpunk.utils.Key;
public class Chapter extends World
{
public var mainmenu:MainMenu;
public var background1:Background1;
public function Chapter()
{
mainmenu = new MainMenu();
add(mainmenu);
background1 = new Background1();
if(mainmenu.YesNo == 1)
{
add(background1);
}
}
}
}
MainMenu
package
{
import flash.events.TimerEvent;
import flash.utils.Timer;
import net.flashpunk.Entity;
import net.flashpunk.graphics.Image;
import net.flashpunk.utils.Input;
import net.flashpunk.utils.Key;
import net.flashpunk.FP;
public class MainMenu extends Entity
{
[Embed(source = "net/MainScreen.png")]
private const SPRITE1:Class;
public var YesNo:int = 0
private var sprite1:Image = new Image(SPRITE1);
public function MainMenu()
{
graphic = sprite1;
sprite1.centerOrigin();
x = 200
y = 150
layer = 150
}
override public function update():void
{
if (Input.pressed(Key.DIGIT_1))
{
YesNo = YesNo + 1;
}
trace(YesNo);
}
}
}
The problem is that your code is in a World class constructor. Flashpunk's idea of game objects is that Entities are added to Worlds. Once added, World runs update() method every frame in which it also runs every Entity's update() as well. Yours if statement in a constructor will always yield false as update wasn't yet executed.
If I understand correctly you want to add background entity after user will press 1. You can do it like this:
// in MainMenu class
override public function update():void
{
if (Input.pressed(Key.DIGIT_1))
{
world.add(new Background1());
}
}
If you want to hold reference to this entity you could:
override public function update():void
{
if (Input.pressed(Key.DIGIT_1))
{
Chapter(world).background1 = new Background1(); // note the casting of World to Chapter
world.add(Chapter(world).background1);
}
}

AS3 : Dynamic text field text doesnt update (returns to default text)

I have a movieclip with a dynamic TextField in it, it has the text "Software part" in it by default. In the class of this movieclip I have the following code:
package {
import flash.display.MovieClip;
import flash.text.TextField;
public class soft_4 extends MovieClip {
public static var software_part_4_text:TextField;
public static var software_part_4_text_bg:String;
public static var software_part_4_text_tu:String;
public static var software_part_4_text_li:String;
public static var software_part_4_text_de:String;
public function soft_4() {
// constructor code
software_part_4_text_bg = "Софтуерна част";
software_part_4_text_de = "Software-Teil";
software_part_4_text_tu = "Yazılım bölümü";
software_part_4_text_li = "Programinės įrangos dalis";
software_part_4_text = software_part_4;
software_part_4_text.selectable = false;
}
}
}
I have an instance of this class in my Main class and depending on a button press, the strings in the TextField are changed like so:
soft_4.software_part_4_text.text = soft_4.software_part_4_text_de;
For instance that changes the text in the TextField to be in German. It works for the first instance of this class (for example: public var firstInstance:soft_4 = new soft_4()) that I have on stage BUT the second instance (for example: public var secondInstance:soft_4 = new soft_4()) has the default text which is "Software part".
I've embedded the font that I am using in the text field.
You seem confused about the way static variables work. When you set the static variable software_part_4_text, all you are doing is setting a static reference to an instance object. Each time you call new soft_4(); you are making a new instance of that class. And each one of those instances has it's own TextField instance software_part_4. Each instance is unrelated to all the rest.
So this line software_part_4_text = software_part_4 is just setting a static reference to your instance variable. It does not make all of your instances to be the same. When you change the text property of the static reference, you are only updating that single instance. This will NOT have any effect on all the other instances.
What you need to do is update each particular instance separately. If you want to do this with a single call, you pretty much need to store a reference to all of the instances. You could do this with an array and a loop, like so:
public class soft_4 extends MovieClip {
//Make a static reference to a String, instead of a Textfield
//This can be private, as it will only be used internally
private static var software_part_4_text:String;
//Should these be Constants??
public static var software_part_4_text_bg:String = "Софтуерна част";
public static var software_part_4_text_tu:String = "Yazılım bölümü";
public static var software_part_4_text_li:String = "Programinės įrangos dalis";
public static var software_part_4_text_de:String = "Software-Teil";
//This is the Vector used to store all the instances.
//This can be private, as only this class will ever need to know about it.
private static var _instances:Vector.<TextField> = new Vector.<TextField>();
public function soft_4() {
// constructor code
software_part_4.selectable = false;
//Each time a new instance is created, store it in the vector.
_instances.push(software_part_4);
}
//This is the static function you would call from your main timeline or whatever.
//Pass in the string that you want the new Text to be
public static function UpdateAllTextFields(newText:String):void
{
software_part_4_text = newText;
_instances.forEach(updateTextField);
}
//This is the function that is executed over each instance in the Vector
private function updateTextField(tf:TextField, index:int, vector:Vector.<TextField>):void {
tf.text = software_part_4_text;
}
}
Then from your Main class you could write something like this:
soft_4.UpdateAllTextFields(soft_4.software_part_4_text_de);
I haven't tested this, nor I have I written any AS3 in a very long time, so let me know if it doesn't work

Adding eventlisteners in main document class for external classes

I have a small project I'm trying to help learn as3. It is a variation from the book Foundation Game Design with Actionscript 3.0. I am using an fla only to have a document class. All art is loaded within the as files. In the book, he just put all the code in the document class, I followed along and it worked as expected. I am trying to break out the code into separate classes to get a handle on OOP. One class makes a background - Background.as, one makes a character - Character.as, and one makes a button, which I instantiate 6 times for 6 different buttons - GameButton.as. And of course there is GameWorld.as which is the document class. Everything loads and shows up as expected. However when I try and add an eventListener for the buttons, I don't get any response. I have tried putting the eventListener in the GameButton.as and also tried it in the GameWorld.as neither of which has worked. Also I pass a reference to the stage when instantiating the various classes, because when I tried to addChild in the GameWorld.as, nothing would show up. I searched the site and found something similar, but it didn't seem to help. Thank you in advance for any advice you my have. Here is the code:
GameWorld.as
package
{
import flash.net.URLRequest;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.DisplayObject
import flash.events.MouseEvent;
import GameButton;
import Character;
import Background;
[SWR(width = "550", height = "400", backgroundColor = "#FFFFFF", frameRate = "60")]
public class GameWorld extends Sprite
{
//public variables
//Background
public var gameBackground:Background;
//Character
public var catCharacter:Character;
//Buttons
public var upButton:GameButton;
public var downButton:GameButton;
public var growButton:GameButton;
public var shrinkButton:GameButton;
public var vanishButton:GameButton;
public var spinButton:GameButton;
public function GameWorld ()
{
//Add the background to the stage
gameBackground = new Background("../images/background.png", stage);
//Add the character(s) to the stage
catCharacter = new Character("../images/character.png", stage);
//Set initial character position
catCharacter.CharacterPos(225, 150);
//Add the buttons to the stage
upButton = new GameButton("../images/up.png", stage, 25, 25);
downButton = new GameButton("../images/down.png", stage, 25, 85);
growButton = new GameButton("../images/grow.png", stage, 25, 145);
shrinkButton = new GameButton("../images/shrink.png", stage, 425, 25);
vanishButton = new GameButton("../images/vanish.png", stage, 425, 85);
spinButton = new GameButton("../images/spin.png", stage, 425, 145);
//Button event handlers
upButton.addEventListener(MouseEvent.CLICK, upButtonHandler);
}
public function upButtonHandler(event:MouseEvent)
{
trace("You clicked the up button!");
catCharacter.CharacterMove(15);
}
}
}
GameButton.as
package
{
import flash.net.URLRequest;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.MouseEvent;
public class GameButton extends Sprite
{
//public variables
public var stageRef:Stage;
public var urlRequest:URLRequest;
public var gameButtonLoader:Loader;
public var gameButtonSprite:Sprite;
//Constructor
public function GameButton (urlRequest:String, stageRef:Stage, xPos:Number, yPos:Number)
{
this.stageRef = stageRef
this.urlRequest = new URLRequest();
gameButtonLoader = new Loader();
gameButtonSprite = new Sprite();
this.urlRequest.url = urlRequest;
gameButtonLoader.load(this.urlRequest);
gameButtonSprite.addChild(gameButtonLoader);
this.stageRef.addChild(gameButtonSprite);
gameButtonSprite.buttonMode = true;
gameButtonSprite.x = xPos;
gameButtonSprite.y = yPos;
}
}
}
Character.as
package
{
import flash.net.URLRequest;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.Stage;
public class Character
{
//private variables
private var stageRef:Stage;
private var urlRequest:URLRequest;
private var characterLoader:Loader;
private var characterSprite:Sprite;
//public variables
public var character_x_pos:Number;
public var character_y_pos:Number;
//Constructor
public function Character (urlRequest:String, stageRef:Stage)
{
this.stageRef = stageRef;
this.urlRequest = new URLRequest();
characterLoader = new Loader();
characterSprite = new Sprite();
this.urlRequest.url = urlRequest;
characterLoader.load (this.urlRequest);
characterSprite.addChild (characterLoader);
this.stageRef.addChild (characterSprite);
characterSprite.mouseEnabled = false;
}
//Set the position of the character
public function CharacterPos(xPos:Number, yPos:Number):void
{
characterSprite.x = xPos;
characterSprite.y = yPos;
}
//Move the position of the character
public function CharacterMove( yPos:Number):void
{
characterSprite.y -= yPos;
}
}
}
Background.as
package
{
import flash.net.URLRequest;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.Stage;
public class Background
{
//Private variables
private var stageRef:Stage;
private var urlRequest:URLRequest;
private var backgroundLoader:Loader;
private var backgroundSprite:Sprite;
//Constructor
public function Background (urlRequest:String, stageRef:Stage)
{
this.stageRef = stageRef;
this.urlRequest = new URLRequest();
backgroundLoader = new Loader();
backgroundSprite = new Sprite();
this.urlRequest.url = urlRequest;
backgroundLoader.load (this.urlRequest);
backgroundSprite.addChild (backgroundLoader);
this.stageRef.addChild (backgroundSprite);
backgroundSprite.mouseEnabled = false;
}
}
}
All art is loaded within the as files.
This is not an approach I recommend. There's a reason God gave us the Flash IDE--and it's not to write code! Any time you're spending on layout and viduals in code is just wasted, unless you have an actual requirement to change the visuals at runtime. The fact that your paths are all hard-coded suggests that you don't have that requirement.
So let's step back and imagine that you have a Symbol that contains 6 Symbols that you've created as just Flash buttons (when you select Button as the Symbol type). These will be SimpleButtons, but in the Class below we're just going to type them as DisplayObject. The Class doesn't care what they are, but using Simplebutton gives them up, over, down and hit states that require no code.
Note that the below assumes you have "automatically declare stage instances" off, which is IMO the best way to do things.
package view {
public class NavBar extends Sprite {
//because you put these on stage in the Symbol, they will be available in the constructor
public var upButton:DisplayObject;
public var downButton:DisplayObject;
public var growButton:DisplayObject;
public var shrinkButton:DisplayObject;
public var rotateButton:DisplayObject;
public var vanishButton:DisplayObject;
//makes it easier to do the same setup on all buttons
protected var allButtons:Vector.<DisplayObject> = <DisplayObject>([upButton, downButton, growButton, shrinkButton, rotateButton, vanishButton]);
public function NavBar() {
super();
for each (var btn:DisplayObject in allButtons) {
btn.buttonMode = true;
btn.mouseChildren = false;
btn.addEventListener(MouseEvent.CLICK, onButtonClick);
}
}
protected function onButtonClick(e:MouseEvent):void {
switch (e.target) {
case upButton:
dispatchEvent(new CharacterEvent(CharacterEvent.UP));
break;
case downButton:
dispatchEvent(new CharacterEvent(CharacterEvent.DOWN));
break;
case growButton:
dispatchEvent(new CharacterEvent(CharacterEvent.GROW));
break;
case shrinkButton:
dispatchEvent(new CharacterEvent(CharacterEvent.SHRINK));
break;
case rotateButton:
dispatchEvent(new CharacterEvent(CharacterEvent.ROTATE));
break;
case vanishButton:
dispatchEvent(new CharacterEvent(CharacterEvent.VANISH));
break;
default:
break;
}
}
}
}
Note that there's zero layout code. This code is dependent on a custom Event Class. I'm going to write that Event Class so that it always bubbles. That way, it can be dispatched anywhere on the display list and received at the top level:
package control {
class CharacterEvent extends Event {
public static var UP:String = 'characterUp';
public static var DOWN:String = 'characterDown';
public static var GROW:String = 'characterGrow';
public static var SHRINK:String = 'characterShrink';
public static var ROTATE:String = 'characterRotate';
public static var VANISH:String = 'characterVanish';
public function CharacterEvent(type:String) {
super(type, true, true);//always bubbles
}
public function clone():Event {
return new CharacterEvent(type);
}
}
}
Now, if you want to manually handle instantiation of the Symbol that has view.NavBar as its base class, it will look like this:
package {
public var navBar:NavBar;
class GameWorld {
public function GameWorld() {
try {
var navClass:Class = getDefinitionByName('NavBarSymbol') as Class;
} catch (e:Error) {
trace('You need to have a Library symbol called NavBarSymbol');
}
if (navClass) {
navBar = new navClass() as NavBar;
//unnecessary layout code here
//Note that this is NOT the responsibility of the child Class!
addChild(navBar);
}
//instantiate character
...
//by listening to the whole Document, you can add other things that
//dispatch Character events on the display list, like a keyboard listener
addEventListener(CharacterEvent.UP, moveCharacterUp);
//listeners for the rest of the character events...
}
public function moveCharacterUp(e:CharacterEvent):void {
//character move logic
}
//other handlers
}
}
Personally, I'd just add the navBar to the stage, and then there's no need to manage it at all (not even reference it with a variable), simply add the event listeners for the various character events.
The root of your problem doesn't seem to be the character code. However, I'm going to give you a few "best practice" pointers about it.
The convention in AS3 is for Class members (properties and methods) to be camel case starting with a lower case letter. So, characterPos() and characterMove().
Your Class already contains character in the name, so really these should just be pos() and move() (though there's no need now to shorten position()).
The only thing your child Classes are doing with their references to the parent are adding themselves. They don't need and shouldn't have a reference to the parent for this purpose. It is the parent's responsibility to add the Children (or the responsibility of the Flash Player if you use the stage).
That said, you could give your Character a reference to the parent Class typed as IEventDispatcher and allow the Character to listen to this channel. This concept is called an event bus.
Note that the reason that so many people do what you're doing is that Adobe failed to document how to properly use OOP with the timeline. Unfortunately, by the time a few of us started documenting that around late 2009/early 2010, the damage was done and everyone assumed that if you wanted to write good code you had to pretend the timeline and stage didn't exist.
I know I've covered a lot of ground, and probably most of what I said directly contradicts what you thought you knew, so please don't hesitate to ask any questions you might have.

AS3 - Access variable in root from class

How to access a variable in the root timeline from within a class? There is a variable called myblocks in the root timeline I need to read the value from.
This is the related class part:
package myclasses
{
public final class Bcoder extends EventDispatcher
{
private function getBlocks():void
{
for (var i:int = _getNumBlocks; i--; ){
// how to get the myblocks value from here?
}}
This is from the root timeline: (ActionScript stands in a keyframe)
import myclasses.Bcoder;
var myblocks:Number=20
I think you might want to look at this: http://www.kirupa.com/forum/showthread.php?349086-AS3-Question-Accessing-a-main-timeline-variable-from-a-class
Furthermore, I'd like to mention that you should use either timeline coding or use a document class, with preference to the latter because this option is the strength of AS3 and will make your code much more structured.
This is complete nonsense and really bad practice. You should avoid this manner of coding!!!
This is really not OOP and make me think about bad AS1 /2 and 3 combined!!!
However this is possible if you have no class defined in Document properties as main Class.
ex :
in a foler "com", the class ObjectOnStage.as :
package com {
import flash.display.Stage;
import flash.display.Sprite;
import flash.events.Event;
public class ObjectOnStage extends Sprite{
public function ObjectOnStage() {
this.addEventListener(Event.ADDED_TO_STAGE,onAddedToStage,false,0,false);
this.addEventListener(Event.ACTIVATED,onActivate,false,0,false);
}
public function onAddedToStage(e:Event):void{
// will output null for both
trace("\n added " + e.target + "\n");
trace(e.target.parent["nonSense"]);
trace(e.target.parent["nonsense"]);
}
public function onActivate(e:Event):void{
// will output the values.
trace("\n added " + e.target + "\n");
trace(e.target.parent["nonSense"]);
trace(e.target.parent["nonsense"]);
}
}
}
On frame 1 of the Timeline :
import com.ObjectOnStage;
var nonSense:int = 1;
var nonsense:String = "This is a nonsense";
var oos:ObjectOnStage = new ObjectOnStage();
this.addChild(oos);
You'd better change the whole script!
Adobe should remove the possibility to write script on the Timeline since the export settings are set to AS3 and the strict mode should be always set to strict mode ON.
Also private constructors will be welcome in order to permit an usage of
MyClass.getInstance();
This will pemit something like:
package com {
public class MyMainObject {
private static var instanceOfMainObject;
private function MyMainObject(args:Vector.<String>){
// or MyMainObject(...args)
trace("new Instance of MyMainObject created with " + args.toString());
}
public static function main(args:Vector.<String>):void{
instanceOfMainObject = MyMainObject.getInstance(args);
trace("arguments.length = " + args.length);
for(var i:int = 0 ; i < args.length ; i++){
trace( i + " = " + args[i]);
}
}
public static function getInstance(args:Vector.<String>):MyMainObject{
var instance:MyMainObject = new MyMainObject(args);
return instance;
}
}
}
Now, this code throws an Error:
1153: A constructor can only be declared public.
Perhaps this will be the case in AS4 ???
If I understand it trough your comment you must pass the DisplayObjectContainer where your variables are declared to the class as argument.
Example :
in MyClass.as
package com {
import flash.display.DisplayObjectContainer;
import flash.events.EventDispatcher;
public class MyClass extends EventDispatcher{
public function MyClass(doc:DisplayObjectContainer) {
trace(doc["nonSense"]);
trace(doc["nonsense"]);
// but this is again not OOP even if you use the "class" KEYWORD.
}
}
}
on the timeline :
var nonSense:int = 1;
var nonsense:String = "This is a nonsense";
var mclss:MyClass = new MyClass(this);
Concerning EventDispatcher you can also read my answer about EventDispatcher here

Trying to delete child instance in array. Lack CS training. Totally stuck

I posted yesterday about how to communicate to one class from another that I wanted to delete an instance of it, and I got the dispatcher working today. However, I think I've painted myself into a corner. Even though the dispatcher is working, I A:feel like it's running through too many functions on the way to actually deleting the object, and B: still can't manage to get it to actually delete. I don't have any formal CS training, so it's one of those situations where my mind is going in circles and I can't "see" what I'm doing wrong. I figure if I post my classes here, at the very least people can have a chuckle at my amateur code, and if I'm lucky, some kind soul will point out what I'm doing wrong. So here goes:
Background.as:
//Background class. Singleton? Sets up/maintains the application.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class Background extends flash.display.MovieClip {
private var slate:MovieClip;
private var slateBounds:Rectangle = new Rectangle(100,-260,0,280);
private var _toolbox:MovieClip;
private var _elementArray:Array = new Array();
public function Background() {
//attach movieclips to stage
slate = new mc_slate();
slate.x = 100;
slate.y = 20;
addChild(slate);
_toolbox = new Toolbox();
_toolbox.x = 750;
_toolbox.y = 20;
addChild(_toolbox);
//set draggables
//slate.addEventListener(MouseEvent.MOUSE_DOWN, dragSlate);
//slate.addEventListener(MouseEvent.MOUSE_UP, releaseSlate);
slate.addEventListener(MouseEvent.MOUSE_UP, dropNewElement);
}
private function dragSlate(event:MouseEvent) {
slate.startDrag(false, slateBounds);
}
private function releaseSlate(event:MouseEvent) {
slate.stopDrag();
}
private function dropNewElement(event:MouseEvent) {
var _elementType:String = _toolbox.currentTool;
var _x:Number = event.target.x;
var _y:Number = event.target.y;
var _newElement:MovieClip;
var _latestIndex:Number;
//case switch to choose element based on _elementType
//add new element to stage
_newElement = new PageElement(_elementType, event.localX, event.localY);
_latestIndex = _elementArray.push(_newElement);
_newElement.addEventListener("closeWindow", deleteElement);
slate.addChild(_newElement);
}
private function deleteElement(event:Event) {
trace("trying to remove element.");
slate.event.target.removeChild(_elementArray[0]);
}
}
}
Toolbox.as:
//Toolbox class.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class Toolbox extends flash.display.MovieClip {
private var _toolboxback:MovieClip;
private var _tool01:MovieClip;
private var _tool02:MovieClip;
private var _tool03:MovieClip;
private var _tool04:MovieClip;
private var _tool05:MovieClip;
private var _currentTool:String = 'none';
public function Toolbox() {
_toolboxback = new ToolboxBack();
_toolboxback.x = 0;
_toolboxback.y = 0;
_toolboxback.alpha = .5;
addChild(_toolboxback);
_tool01 = new TextTool();
_tool01.x = 10;
_tool01.y = 10;
addChild(_tool01);
//_tool01.addEventListener(MouseEvent.MOUSE_DOWN, dragTool);
_tool01.addEventListener(MouseEvent.MOUSE_UP, switchTool);
_tool02 = new ImageTool();
_tool02.x = 10;
_tool02.y = 54;
addChild(_tool02);
_tool02.addEventListener(MouseEvent.MOUSE_UP, switchTool);
}
private function dragTool(event:MouseEvent) {
event.target.startDrag(false);
}
private function releaseTool(event:MouseEvent) {
event.target.stopDrag();
}
private function switchTool(event:MouseEvent) {
_currentTool = event.target.toolname;
//trace(_currentTool);
}
public function get currentTool():String{
return _currentTool;
}
}
}
Tool.as (any class with "Tool" at the end of it simply extends this class and adds a name)
//Tool class.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class Tool extends flash.display.MovieClip {
private var _toolname:String;
public function Tool(toolname) {
_toolname = toolname;
}
public function get toolname():String{
return _toolname;
}
}
}
PageElement.as:
//Page element class.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
public class PageElement extends flash.display.MovieClip {
private var _elementname:String;
private var _elementback:MovieClip;
private var _elementmenu:MovieClip;
private var _title:TextField;
private var _formatter:TextFormat = new TextFormat();
public function PageElement(elementname, x, y) {
_elementname = elementname;
_elementback = new ElementBack();
_elementback.x = x;
_elementback.y = y;
_elementback.alpha = .5;
_elementback.addEventListener(MouseEvent.MOUSE_DOWN, dontBubble);
_elementback.addEventListener(MouseEvent.MOUSE_UP, dontBubble);
_elementmenu = new ElementMenu();
_elementmenu.x = x + _elementback.width - 5;
_elementmenu.y = y - 5;
_elementmenu.addEventListener(MouseEvent.MOUSE_OVER, showElementMenu);
_elementmenu.addEventListener(MouseEvent.MOUSE_OUT, retractElementMenu);
_elementmenu.addEventListener(MouseEvent.MOUSE_DOWN, dragElement);
_elementmenu.addEventListener(MouseEvent.MOUSE_UP, releaseElement);
_formatter.font = "Helvetica";
_formatter.size = 10;
_title = new TextField();
_title.text = elementname;
_title.x = x;
_title.y = y;
_title.textColor = 0xffffff;
_title.setTextFormat(_formatter);
addChild(_title);
addChild(_elementback);
addChild(_elementmenu);
}
public function get elementname():String{
return _elementname;
}
public function set elementTitle(newTitle) {
}
public function showElementMenu(event:MouseEvent) {
_elementmenu.expandMenu();
}
public function retractElementMenu(event:MouseEvent) {
_elementmenu.retractMenu();
}
public function hideElementMenu() {
_elementmenu.alpha = 0;
}
private function dragElement(event:MouseEvent) {
event.target.parent.parent.startDrag(false);
event.stopPropagation();
}
private function releaseElement(event:MouseEvent) {
event.target.parent.parent.stopDrag();
event.stopPropagation();
}
private function dontBubble(event:MouseEvent) {
event.stopPropagation();
}
}
}
DeleteBack.as:
//Element menu back class.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class DeleteBack extends flash.display.MovieClip {
public function DeleteBack() {
}
public function closeElement(event:MouseEvent) {
dispatchEvent(new Event("closeWindow", true));
trace("event dispatched.");
}
}
}
ElementMenu.as:
//Element menu class.
package pc_mockup {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import caurina.transitions.Tweener;
public class ElementMenu extends flash.display.MovieClip {
private var _elementmenuback:MovieClip;
private var _deletebutton:MovieClip;
public function ElementMenu() {
_elementmenuback = new ElementMenuBack();
_elementmenuback.x = 0;
_elementmenuback.y = 0;
_elementmenuback.width = 100;
_elementmenuback.height = 5;
_elementmenuback.alpha = .5;
addChild(_elementmenuback);
_deletebutton = new DeleteBack();
_deletebutton.x = -5;
_deletebutton.y = 10;
_deletebutton.width = 10;
_deletebutton.height = 10;
_deletebutton.alpha = .2;
_deletebutton.visible = false;
addChild(_deletebutton);
_deletebutton.addEventListener(MouseEvent.MOUSE_DOWN, closeElement);
}
public function expandMenu() {
Tweener.addTween(_elementmenuback, {height:30, time:.2, transition:"easeOutBack"});
_deletebutton.visible = true;
}
public function retractMenu() {
Tweener.addTween(_elementmenuback, {height:5, time:.1, transition:"easeInBack"});
_deletebutton.visible = false;
}
public function closeElement(event:MouseEvent) {
//check that the user really wants to close the element before sending the destroy signal
//perform any closing animations
//this.parent.destroy();
_deletebutton.closeElement(event);
}
}
}
That's it for the meaningful classes. Anything else is either an empty class that's only in there to help make the library object accessible to ActionScript, or a trivial extension of something else.
The code puts a new element onto the stage, gives it a cool little dropdown menu that makes it draggable and has a delete button on it, and should link that button to a function that closes the element.
I've got everything but the closing.
General code criticism also very welcome. Like I said, I have no training, I've been figuring this stuff out for myself, and feedback of any kind from people who know what they're doing is valuable.
Thanks!
SS
PS. In response to Daniel's comment, here are the steps the code takes:
The Background class puts everything on the stage and creates the toolbox.
The toolbox creates the tools, which are like Photoshop's tools. You click on them to select an element you want to add to the stage, then you click inside the "slate" to drop a new instance of that object on top of it. The background creates the instance and saves it in an array of all instances created at runtime.
The new element makes its own dropdown menu, which is the draggable portion of the element and holds the delete button. This menu places an eventListener on the delete button.
When the delete button is clicked, the eventListener placed on it by its parent class calls an event dispatcher inside the delete button class itself.
This dispatched event is caught by the background class (I figured the best class to remove the element is the same class that made it, right?) and triggers the actual code to remove the element.
This code, "deleteElement," is where I'm stuck. I have all the instances in an array, but the event has gone through so many intermediary classes, the MouseEvent, and thus, I suspect, the MouseEvent target, has fallen by the wayside. So the only way to know which element to delete is to find its array index. I have no idea how this would work. Any ideas?
let's do this a bit at a time...
slate.event.target.removeChild(_elementArray[0]); in background.as
why are you using slate.event?
you are passing an event object to the function, but looks like you're using a different event's target, which I don't know where it's coming from or why it's not giving you an error.
it should just be event.target, which should give you the PageElement(formerly known as _newElement)
what I don't know also is why you are removing a child from it which is _elementArra[0] - which really is another PageElement and likely itself if you only have one.
so it looks to me that there are a bunch of things that should have thrown errors. What are you using to compile your code? What about debugger? are you using any?
If you look at your previous question, I added some code there about how to get the parent. So I adjusted it a bit
function deleteElement($e:MouseEvent):void{
var parentMC:MovieClip = $e.target.parent;
parentMC.removechild($e.target);
}
however the problem is that you're not passing a MouseEvent but a blank event
dispatchEvent(new Event("closeWindow", true)); in DeleteBack.as
so this will not pass anything under target, and you can't get it. (target is read only, so new Event(etc) will always have a null target. So essentially that's a bit of a lost cause.
you could set an onject in your singleton and pass which mc is to be deleted, and then the deleteElement would just grab that object. The other option is to look into the signals class which will let you do some better/more efficient event handling.
finally (sort of, there's more but for now) I'd say look into using CASAlib, in particular, use CasaMovieClip instead of MovieClip for extending, as it will delete your movie clips better. If you have a lot of event listeners and you don't clear them properly, they'll end up staying in memory even after you delete them.
of course looking into other frameworks like RobotLegs is a good idea too, it gets you into better practices.
GL
Edit ...
frameworks/micro-architectures:
http://www.robotlegs.org/
http://swizframework.org/
http://puremvc.org/
and many more
I think the important thing is to not get stuck on a framework (though I mention the word often). And the best framework is the framework that is best for you, and to me that means offering a good communication backbone for the app and staying out of the way.
My setup for writing code is this:
FlashDevelop with the Flex Compiler. FlashDevelop is for PC only so if you're on Mac you might want to consider other options like flex. FlashDevelop and the Flex compiler(the compiler only) are both free so you can't go wrong, and once you start using it you won't want to go back to coding in Flash - guaranteed!!
debugging:
Trace is the simplest form of debugging, and it can be quite difficult to understand the problem.
You can use the flash debugger by pressing Ctrl-Shift-Enter to compile and run. You will need to set the break points ahead though.
FlashDevelop has a debugger that works just like the Flash and Flex debuggers and I use it quite often.
But my favorite debug tool has to be de monster debugger
it takes a bit to more to implement, and you need to add some code, but it found issues for me that I couldn't get to using the default debugger only. Definitely worth a look.