Actionsctipt: Creating a Movie Clips inside a class - actionscript-3

I'm trying to draw an movie clip object from within a class and directly output it onto the screen. Needless to say, I'm fairly new to actionscript. This is my class:
class Class.player {
function create() {
_root.createEmptyMovieClip("ship", _root.getNextHighestDepth());
ship._x = 200;
ship._y = 390;
ship.beginFill(0xFF00AA);
ship.moveTo(-20, -12);
ship.lineTo(20, -12);
ship.lineTo(20, 12);
ship.lineTo(-20, 12);
ship.lineTo(-20, -12);
ship.endFill();
}
}
And this is the code I'm using to import, call, and execute it.
import Class.player;
var Player:player = new player();
Player.create();
There's no error message, and it works fine if I place the create function into the main script, but nothing happens if I call it from within the class.
Any advice would be appreciated. Thanks!

I would recommend reading this post to help you: http://brianchau.wordpress.com/2008/01/15/targeting-movieclip-from-an-as3-class-file/
The main code from the website is :
package
{
import flash.display.MovieClip;
public class Test extends MovieClip
{
public static var blackbox_mc:MovieClip = new BlackBox();
public function Test()
{
this.stage.addChild(blackbox_mc);
var myTest:Control = new Control();
}
}
}
Create the Control.as class file with the following code:
package
{
public class Control
{
public function Control()
{
Test.blackbox_mc.y = 100;
}
}
}
The trick is to create a static variable so that it can be referenced from the Control.as file.

Related

How do I run separate ActionScript 3 class code in a Flash project?

I'm trying to create some sort of game prototype using ActionScript 3.
However, I have run into something that could be called a Catch-22.
package Main{
public class Main extends MovieClip
{
public function Main()
{
//gameLoop()
StartGame()
}
public function StartGame()
{
stage.addEventListener(Event.ENTER_FRAME, gameLoop)
}
var spr = new Symbol6();
public function gameLoop(e:Event):void
{
addChild(Symbol6)
trace("owo")
}
//new Bullet(0,0,5,Math.PI,0,5,0,0)
}
}
The code above outputs:
1037: Packages cannot be nested.
And if I were to remove the package Main block, it would output
1114: The public attribute can only be used inside a package.
How am I supposed to solve this?

AS3 - Call a method between classes

I'm almost entirely new to OOP, and I just don't understand how to call a method in one class from another. I'm trying to call a method in the Main.as from a custom class, but it always comes up as the "Call to possibly undefined method" error, and I don't know a way around it.
Main.as code:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public var titleScreen:TitleScreen = new TitleScreen();
public var feedMeShibe:FeedMeShibe = new FeedMeShibe; //These are exported
public var milkbone:MilkboneItem = new MilkboneItem; //actionscript symbols
public var openMouthArea:OpenMouthArea = new OpenMouthArea; //in the library
public function Main() {
titleScreen.x = 350;
titleScreen.y = 250;
addChild(titleScreen);
}
public function addShibe(shibeX:Number, shibeY:Number, shibeSize:Number):void {
feedMeShibe.x = shibeX;
feedMeShibe.y = shibeY;
feedMeShibe.width = feedMeShibe.width*shibeSize;
feedMeShibe.height = feedMeShibe.height*shibeSize;
addChild(feedMeShibe);
}
public function addMilkbone(milkboneX:Number, milkboneY:Number, milkboneSize:Number):void {
milkbone.x = milkboneX;
milkbone.y = milkboneY;
milkbone.width = milkbone.width*milkboneSize;
milkbone.height = milkbone.height*milkboneSize;
addChild(milkbone);
}
public function addOpenMouthArea(OMAX:Number, OMAY:Number, OMAW:Number, OMAH:Number):void {
openMouthArea.x = OMAX;
openMouthArea.y = OMAY;
openMouthArea.width = OMAW;
openMouthArea.height = OMAH;
addChild(openMouthArea);
}
}
}
TitleScreen.as code:
package {
import flash.display.MovieClip;
import flash.events.Event;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
public class TitleScreen extends MovieClip {
public function TitleScreen() {
createListeners();
}
private function createListeners():void {
this.addEventListener(Event.ENTER_FRAME, stopFrame);
}
public function stopFrame(e:Event):void {
if (this.currentFrame == 510){
this.removeEventListener(Event.ENTER_FRAME, stopFrame);
this.stop();
addShibe(100, 100, 0.5); //How do I
addMilkbone(50, 50, 0.6); //access the Main class
addOpenMouthArea(200, 200, 1, 1); //with these?
}
}
}
}
I don't normally ask questions online, but I'm at the end of my rope here. I'm completely stumped, and browsing tutorials and previous questions online is only further confusing me. Any help would be greatly appreciated.
The most simple way to do that is to have one class to reference another class. Let's say you have a switch and a bulb. The switch can tell the bulb to turn lights on and off.
Here's a minimalistic example of how it could look like:
var bulbInstance:Bulb = new Bulb();
var switchInstance:LightSwitch = new LightSwitch();
switchInstance.bulbReference = bulbInstance;
now you have a bulb referenced in your switch class. Inside from it you can call public methods and access public variables of the Bulb class.
Don't forget to make a public variable bulbReference in your LightSwitch class.
There are many ways to form dependencies between elements of your code to break strong dependencies and make your code: scalable, reusable and maintainable. To learn about it look for Design Patterns. There is a great book written about this topic: http://shop.oreilly.com/product/9780596528461.do
But there's a lot of material on the topic free on the Internet.
If I'm not mistaken all you have to do is
Main.addShibe(100, 100, 0.5); //How do I
Main.addMilkbone(50, 50, 0.6); //access the Main class
Main.addOpenMouthArea(200, 200, 1, 1); //with these?

How to improve this AS3 code structure to be more effective?

I have made an AS3 code to be a function. But I think my code is too lengthy. Could you help to improve it? Thank you!
I created test.fla first and added 5 grey block(external pictures from PSD) to stage. My function is to display different pictures when hovering mouse on corresponding grey block.
I converted those 5 grey blocks to Movie Clip and set instance name as sp1, sp2, sp3, sp4 and sp5. Then I created a document class, test.as and set 5 EventListener.
sp1.addEventListener(MouseEvent.MOUSE_OVER,clickmouse1);
sp2.addEventListener(MouseEvent.MOUSE_OVER,clickmouse2);
sp3.addEventListener(MouseEvent.MOUSE_OVER,clickmouse3);
sp4.addEventListener(MouseEvent.MOUSE_OVER,clickmouse4);
sp5.addEventListener(MouseEvent.MOUSE_OVER,clickmouse5);
So my first question is can I have any method to combine those 5 EventListener to be one? Because in my mind, so many EventListener will cost much more resource of PC.
My second question is I set 5 target pictures as 5 class.
In test.as I created code below:
public class EuroCup extends Sprite{
var arr:Array=new Array();
var Res1:Result609=new Result609();
var Res2:Result610=new Result610();
var Res3:Result611=new Result611();
var Res4:Result612=new Result612();
var Res5:Result613=new Result613();
var i:int=0;
public function EuroCup() {
arr[1]=Res1;
arr[2]=Res2;
arr[3]=Res3;
arr[4]=Res4;
arr[5]=Res5;
}
}
I think that is too lengthy. Is there any way to simplify it?
Here is the test.fla and test.as:Download
Whatever, thank u guys!
Restructuring:
public class EuroCup extends Sprite {
private var arr:Array;
public function EuroCup() {
arr = [ new Result609(), new Result610(),
new Result611(), new Result612(), new Result613()
];
}
}
Then use results as arr[0], arr[1] and so on. Also, if you have several sprites to listen clicks on, with similar listeners, you can connect all such sprites to single listeners and use event.target to distinguish them, where event is MouseEvent. Or place them into container and create one listener to that container - again, event.target will tell what sprite is clicked.
And yet two things - every time you see new Array(), replace it with [] - its faster and shorter. And place all code into constructor, not class body - it will be compiled to be executed faster.
You can/should use a Dictionary for associations between the grey rects and the images to display.
package {
public class EuroCup {
private var _children:Array, _current:Sprite, _map:Dictionary;
public function EuroCup() {
super();
initialize();
}
protected function initialize():void {
_children = [];
_map = new Dictonary();
// i don't know the image's symbol name.
// _map[_children[_children.length] = new Result609()] = new SYMBOL_NAME();
for each(var child:Sprite in _children) {
child.addEventListener(MouseEvent.CLICK, click_handler);
}
}
private function click_handler(event:MouseEvent):void
{
if (_current) {
_current.visible = false; // or use fading, etc
}
_current = _map[event.currentTarget] as Sprite;
if (_current) {
_current.visible = true; // or use fading, etc
}
}
}
}
One option for simplifying the code would be to associate the sp and Res instances with each other by identity, using a Dictionary. That allows you to avoid the work of tracking array indices, which is half of the reason you have separate event handler methods. Once the instances are associated by identity, then you can use the currentTarget property of a dispatched event to determine which element in the Dictionary you want to show on the stage.
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.utils.Dictionary;
public class test extends Sprite
{
var dict:Dictionary = new Dictionary();
var visibleResult:MovieClip;
public function test()
{
dict[sp1]=new Result609();
dict[sp2]=new Result610();
dict[sp3]=new Result611();
dict[sp4]=new Result612();
dict[sp5]=new Result613();
sp1.addEventListener(MouseEvent.MOUSE_OVER,clickmouse);
sp2.addEventListener(MouseEvent.MOUSE_OVER,clickmouse);
sp3.addEventListener(MouseEvent.MOUSE_OVER,clickmouse);
sp4.addEventListener(MouseEvent.MOUSE_OVER,clickmouse);
sp5.addEventListener(MouseEvent.MOUSE_OVER,clickmouse);
}
private function clickmouse(evt:MouseEvent):void
{
if(visibleResult)
{
removeChild(visibleResult);
}
var Res:MovieClip = dict[evt.currentTarget] as MovieClip;
addChild(Res);
Res.x=300;
Res.y=400;
visibleResult=Res;
}
}
}
If you expect to have more than 5 sp instances in the application, then you could use a loop to assign the event listeners. But for less than 10 instances, you probably don't gain much from a loop.
I would go for a more simple version; add only one event listener and use Event.target to determine on which item is clicked, using a switch-statement.
This is helpful if the buttons should do different things.
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Test extends Sprite
{
public var sp1:Sprite;
public var sp2:Sprite;
public var sp3:Sprite;
public function Test()
{
this.addEventListener(MouseEvent.MOUSE_OVER, handleClick);
}
private function handleClick(event:MouseEvent):void
{
trace("Clicked on: " + event.target)
switch (event.target)
{
case this.sp1:
{
// do something here
break;
}
case this.sp2:
{
// do something here
break;
}
case this.sp3:
{
// do something here
break;
}
default
{
trace("No handler defined for: " + event.target)
}
}
}
}
}
However, you can also make smart use of it's type. Let's say all you buttons extend a custom class called CustomButton, and they all need to do the same (like call a function), but with a parameter based on it's id.
This is helpful if the buttons should basically do the same thing.
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Test extends Sprite
{
public function Test()
{
this.addEventListener(MouseEvent.MOUSE_OVER, handleClick);
}
private function handleClick(event:MouseEvent):void
{
if (event.target is CustomButton)
{
var button:CustomButton = event.target as CustomButton; // you're now sure it's a CustomButton
this.showById(button.id); // let's say CustomButton has a public var 'id'
}
}
private function showById(id:int):void
{
// do something
}
}
}
Hope that helps.
Tip: Always start your class+filename with a capital. Variables start with capitals. This is very common in the actionscript world.

Organizing Long Scripts In Separate Files?

In an attempt to organize my code, I'm trying to split up my (lengthy) main controller class into separate files, but my new files must still have access to the variables and functions of the main controller class.
I'm trying to cut and paste code from my controller class into a new class/file, allowing the controller class to call the new class, and allowing the new class to have access to the controller class's properties and function.
Assuming I'm not totally bludgeoning appropriate design patterns, below is my unsuccessful attempt at accomplishing this task:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Test extends Sprite
{
public var myString:String;
public function Test()
{
if (stage)
init(null);
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(evt:Event):void
{
if (hasEventListener(Event.ADDED_TO_STAGE))
removeEventListener(Event.ADDED_TO_STAGE, init);
/////////////// MOVE COMMENTED CODE TO NEW FILE ///////////////////////
//
// //Assign The String A Value
// myString = "Hello world";
//
// //Draw A Blue Square
// var sq:Sprite = new Sprite();
// sq.graphics.beginFill(0x0000FF);
// sq.graphics.drawRect(10, 10, 100, 100);
// sq.graphics.endFill();
// super.addChild(sq);
//
// //Call Tracer Function
// tracer();
//
//////////////////////////////////////////////////////////////////////
//Call pasted method in NewFile.as
NewFile.myNewFunction(); // <- this doesn't work
}
public function tracer():void
{
trace(myString);
}
}
}
new file doesn't have access to the Controller class - doesn't work. how can i write the new file so that it does have access to the properties, functions, stage, etc. of the Controller class, as if its code was never removed and is still in its original place.
package
{
public class NewFile
{
public static function myNewFuntion():void
{
//Assign The String A Value
myString = "Hello world";
//Draw A Blue Square
var sq:Sprite = new Sprite();
sq.graphics.beginFill(0x0000FF);
sq.graphics.drawRect(10, 10, 100, 100);
sq.graphics.endFill();
super.addChild(sq);
//Call Tracer Function
tracer();
}
}
}
public class MainClass extends Sprite
{
private var subClass:SubClass;
public function MainClass
{
var controller:Controller = new Controller();
subClass = new SubClass(controller);
addChild( subClass );
}
private function init():void
{
subClass.doWhatever();
}
}
public class Controller
{
public function doThis():void
{
}
public function doThat():void
{
trace("controller do that...");
}
public function doSomethingElse():void
{
}
}
public class Subclass extends Sprite
{
private var controller:Controller;
public function Subclass(controller:Controller)
{
this.controller = controller;
trace( "new Subclass instance!" );
}
public function doWhatever():void
{
controller.doThat();
}
}
This code
//Call Tracer Function
tracer();
is not going to work, since tracer is not a static method.
This one :
super.addChild(sq);
won't work either, since NewFile doesn't inherit any class ; super() calls the homonyme method of the mother class in an INHERITANCE relation. What you should do there is more probably a COMPOSITION or AGGREGATION relation : newFile IS NOT a Controller, but Controller HAS a newFile.
It's difficult to know exactly what is wrong if you do not give us an error message.
On the design aspect, I have to agree with PatrickS. You might want to check the composite pattern, which could be what you need there.

How do I make my public static logging method write to an inputfield?

I figured out how to create a static method that is available everywhere, for example:
UtilLib.as:
package
{
public final class UtilLib
{
public static function getTimeStamp():uint
{
var now:Date = new Date();
return now.getTime();
}
}
}
I can access this everywhere by doing UtilLib.getTimeStamp() - Now, I want to create a new staic method called log(msg:String). This should log a message to a multi-line inputfield.
The problem however is that this inputfield must be created somewhere and must be accessible and visible all the time, and I don't want to pass it through the function parameters all the time as this would cause a lot of trouble (I'd have to pass it through objects aswell..).
So, how do I make a "public textfield" so my static log method can write to it?
UPDATE:
I now tried the following class, with a static constructor (I think). However, the textfield object is not showing. When I do an addChild(debugField) after creating it, it gives me error 1180.
Logger.as
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
public class Logger extends Sprite
{
public static var debugField:TextField;
/* static block */
{
trace("Logger initializing.");
debugField = new TextField();
debugField.width = 500;
debugField.height = 100;
debugField.x = 100;
debugField.y = 400;
debugField.background = true;
debugField.backgroundColor = 0xFFFFFF;
debugField.defaultTextFormat = new CustomTextFormat();
debugField.mouseWheelEnabled = true;
debugField.multiline = true;
debugField.type = TextFieldType.DYNAMIC;
}
public static function log(msg:String):void
{
if (debugField) debugField.appendText(msg);
}
}
}
I initialize it like this:
var test:Logger = new Logger();
addChild(test);
And I log a new message like this:
Logger.log("test");
Unfortunately, the textField is not showing.
Essentially you need:
somewhere to log a message which is globally accessible
the ability to update a text field whenever the log message changes
A simple solution using objects could look like this:
Example
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;
public class Example extends Sprite {
private var messageLog:TextField;
public function Example() {
createTextField();
MessageLogger.getInstance().addEventListener( MessageLogger.LOG, handleMessageLoggerUpdate );
MessageLogger.getInstance().log( "Message!" );
}
private function handleMessageLoggerUpdate( event:Event ):void {
messageLog.text = MessageLogger.getInstance().getLog();
}
private function createTextField():void {
messageLog = new TextField();
addChild( messageLog );
}
}
}
MessageLogger
package {
import flash.events.EventDispatcher;
import flash.events.Event;
public class MessageLogger extends EventDispatcher {
private static var instance:MessageLogger;
public static function getInstance():MessageLogger {
if ( !instance ) {
instance = new MessageLogger( new InstanceKey() );
}
return instance;
}
public static var LOG:String = "MessageLoader#log";
private var messageLog:String;
public function MessageLogger(key:InstanceKey) {
messageLog = "";
}
public function log( message:String ):void {
messageLog += message;
notify();
}
public function getLog():String {
return messageLog;
}
private function notify():void {
dispatchEvent( new Event( LOG ) );
}
}
}
class InstanceKey {}
The important thing here is that a message can be logged from anywhere using
MessageLogger.getInstance().log( "Your Message Here" );
and anything can be notified of when a message has been logged using
MessageLogger.getInstance().addEventListener( MessageLogger.LOG, listenerFunction );
at any point the current message log can be obtained using
MessageLogger.getInstance().getLog();
Create a new Logging class and have that class have a static constructor. Add your logging method to this class. Make the static constructor save the logging field to a private variable. Now before you call the logging method just call your static constructor with the input field you'd like to use. This will create the class, set up the input field as the destination, and now you can simply just call the log function from anywhere.
Traditionally, the way you let static methods interact with private variables is to pass them in. Pass in a pointer to your textbox.
so instead of
public static function getTimeStamp():uint { ... }
you have
public static function writeTimeStamp(messageBox):uint { ... }
The syntax might be incorrect as I'm not an AS dev but, do you see what I mean? From there, that block of code can access messageBox as if it were a local variable. Well it is.
(I renamed the method name for clarity. You could even stop it returning a variable if it doesn't need to but you'll need to change the declaration further.)
In your updated post the debug text field needs to be added to the display list somewhere. Right now it looks like it is just created and used during that log method. You add display objects to the display list by calling addChild( displayObject:DisplayObject ):Boolean; on a DisplayObjectContainer that is already a child of stage.