Actionscript 3: Adding buttons/textfields to an instance of a class - actionscript-3

This absolutely does not work. The for loop simply spam tracing of one of the objects in the dictionary. After like 10 seconds i get this error message:
Error: Error #1502: A script has executed for longer than the default timeout period of 15 seconds.
Does anyone know why?
package
{
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.display.Sprite;
public class Main extends MovieClip
{
public static var bg_width = 980;
public static var bg_height = 541;
public var S_Norway:Dictionary = new Dictionary();
public var S_Australia:Dictionary = new Dictionary();
public var S_China:Dictionary = new Dictionary();
public var S_South_Africa:Dictionary = new Dictionary();
public var S_Brazil:Dictionary = new Dictionary();
public var S_USA:Dictionary = new Dictionary();
public var S_France:Dictionary = new Dictionary();
// ------------------------------------------------------
public static var airportDict:Dictionary = new Dictionary();
public function Main()
{
S_USA["x"] = -299;
S_USA["y"] = -114;
S_USA["city"] = "New York";
S_Norway["x"] = -60;
S_Norway["y"] = -183;
S_Norway["city"] = "Oslo";
S_South_Africa["x"] = -26;
S_South_Africa["y"] = 146;
S_South_Africa["city"] = "Cape Town";
S_Brazil["x"] = -210;
S_Brazil["y"] = 73;
S_Brazil["city"] = "Rio de Janeiro";
S_France["x"] = -79;
S_France["y"] = -135;
S_France["city"] = "Paris";
S_China["x"] = 239;
S_China["y"] = -98;
S_China["city"] = "Beijing";
S_Australia["x"] = 307;
S_Australia["y"] = 153;
S_Australia["city"] = "Sydney";
airportDict["USA"] = S_USA;
airportDict["Norway"] = S_Norway;
airportDict["South Africa"] = S_South_Africa;
airportDict["Brazil"] = S_Brazil;
airportDict["France"] = S_France;
airportDict["China"] = S_China;
airportDict["Australia"] = S_Australia;
for (var k:Object in airportDict)
{
var value = airportDict[k];
var key = k;
trace(key);
var airport:flyplass = new flyplass(key,airportDict[key]["bynavn"]);
airport.coordinates(airportDict[key]["x"], airportDict[key]["y"]);
this.addChild(airport);
}
Running script in debug mode (CTRL - SHIFT - Enter) produced the following output.
Attempting to launch and connect to Player using URL C:\Users\eivmey\Documents\practice\3.0.swf
[SWF] C:\Users\eivmey\Documents\It 2\fly\3.0.swf - 475976 bytes after decompression
USA
Australia
* Infinite spam of Australia follows
____________________________--
EDIT: Solved it
Ok, the problem was in the flyplass class.
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.SimpleButton;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFormat;
public class flyplass extends Main()
{
// variables
public function flyplass(input1, input2)
{
// code
}
When changing extends Main() to extends MovieClip it suddenly worked. Do you know why?

Related

Flash-Adding Symbol to Stage in AS3

I am trying to add a symbol from my library to the stage. For some reason, when I run the code, no objects are appearing on the stage but I am not getting any errors either. I can't provide all of the code because its VERY long, but I'll provide enough for anyone reading to get a good idea of what I'm trying to do.
Basically, the main as3 file starts like this:
package {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.media.*;
import flash.net.*;
public class NewFrogMod extends MovieClip
{
var oneHit:Boolean;
var twoHit:Boolean;
var threeHit:Boolean;
var fourHit:Boolean;
var fiveHit:Boolean;
var sixHit:Boolean;
var sevenHit:Boolean;
var eightHit:Boolean;
var nineHit:Boolean;
var tenHit:Boolean;
var elevenHit:Boolean;
var twelveHit:Boolean;
var thirteenHit:Boolean;
var fourteenHit:Boolean;
var score:uint;
var frog1:Frog;
var truck1:Truck;
var truck2:Truck;
var truck3:Truck;
var car1:Car;
var car2:Car;
var log1:Logs;
var log2:Logs;
var log3:Logs;
var log4:Logs;
var turtle1:Turtles;
var turtle2:Turtles;
var z1:Zfrog;
var z2:Zfrog;
var z3:Zfrog;
var z4:Zfrog;
var z5:Zfrog;
public function NewFrogMod()
{
var score = 0;
frog1 = new Frog();
truck1 = new Truck();
truck2= new Truck();
truck3 = new Truck();
car1 = new Car();
car2 = new Car();
log1 = new Logs();
log2 = new Logs();
log3 = new Logs();
log4 = new Logs();
turtle1 = new Turtles();
turtle2 = new Turtles();
z1 = new Zfrog();
z2 = new Zfrog();
z3 = new Zfrog();
z4 = new Zfrog();
z5 = new Zfrog();
addChild(frog1);
addChild(truck1);
addChild(truck2);
addChild(car1);
addChild(car2);
addChild(log1);
addChild(log2);
addChild(log3);
addChild(turtle1);
addChild(turtle2);
addChild(z1);
addChild(z2);
addChild(z3);
addChild(z4);
addChild(z5);
frog1.x = 238;
frog1.y = 373;
truck1.x = 0;
truck1.y = 252;
truck2.x = 205;
truck2.y = 252;
car1.x = 82;
car1.y = 175;
car2.x = 363;
car2.y = 175;
log1.x = 22;
log1.y = 51;
log2.x = 355;
log2.y = 51;
log3.x = 43;
log3.y = 102;
log4.x = 292;
log4.y = 102;
turtle1.x = 241;
turtle1.y = 81;
turtle2.x = 508;
turtle2.y = 125;
z1.x = 200;
z1.y = 250;
z2.x = 300;
z2.y = 350;
z3.x = 100;
z3.y = 150;
z4.x = 100;
z4.y = 250;
z5.x = 200;
z5.y = 150;
Then there's a bunch more after that....
Now, to give you an idea of how I'm setting up the objects, here is the code for some of them:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Car extends NewFrogMod{
public function Car()
{
addEventListener(Event.ENTER_FRAME, Enter4);
}
function Enter4(event:Event):void
{
this.x += 3;
}
}
}
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Truck extends NewFrogMod{
public function Truck()
{
addEventListener(Event.ENTER_FRAME, Enter5);
}
function Enter5(event:Event):void {
if(this.hitTestObject(frog1))
{
health1.width -= 5;
}
}
}
}
I've tried extending MovieClip with these files as well and that doesn't work either. Any hep is GREATLY aqppreciated. Thank you!
Ensure that the parent class NewFrogMod which adds the movieclip, has itself been added to the stage.
Also ensure that each library symbol, eg. Truck, has been linked to its class:
Right-click the symbol in the library and choose Properties from the menu.
Check the box 'Export for ActionScript'.
In the Class field, enter the full class path of your Truck class (simply 'Truck' in your case). The base field class can be ignored as your Truck class already extends MovieClip.
Alternatively, specify NewFrogMod as the document class of the main timeline:
Left-click an empty part of the stage, go to the Properties inspector and enter NewFrogMod into the Class field.
i think it may be that you added the objects before you specified the location. try moving the addChild functions below the x and y coordinates.

ActionScript 3 Apparently I cannot access movieclip

I've created a zoom function but when I try to scale the bg_image nothing happends. It is like I cannot access it's properties. Does anyone know why? :)
Ty!
Main class
package
{
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.text.TextField;
import fl.controls.Button;
import fl.controls.List;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
import flash.display.Shape;
import fl.transitions.Fly;
import fl.motion.MatrixTransformer;
import flash.events.KeyboardEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.ui.Mouse;
public class Main extends Sprite
{
// Zoom
public static var scale:Number = 1;
public var spImage:Sprite;
public var mat:Matrix;
public var mcIn:MovieClip;
public var mcOut:MovieClip;
public var boardWidth:int = 980;
public var boardHeight:int = 661;
public var boardMask:Shape;
public var externalCenter:Point;
public var internalCenter:Point;
public const scaleFactor:Number = 0.8;
public var minScale:Number = 0.25;
public var maxScale:Number = 10.0;
// ------------------------
// Grafikk
public var bg_image:Sprite;
//-------------------------------
//-----------------------------------------------
public var routeArray:Array;
public var startList:List = new List();
public var sluttList:List = new List();
public var S_Norway:Dictionary = new Dictionary();
public var S_Australia:Dictionary = new Dictionary();
public var S_China:Dictionary = new Dictionary();
public var S_South_Africa:Dictionary = new Dictionary();
public var S_Brazil:Dictionary = new Dictionary();
public var S_USA:Dictionary = new Dictionary();
public var S_France:Dictionary = new Dictionary();
// ------------------------------------------------------
public static var airportDict:Dictionary = new Dictionary();
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, init);
// ---------------------------------
}
public function init(e:Event):void
{
bg_image = new Image(0, 0);
this.addChild(bg_image);
bg_image.addEventListener(MouseEvent.CLICK, mouseCoordinates);
removeEventListener(Event.ADDED_TO_STAGE, init);
// Zoom
this.graphics.beginFill(0xB6DCF4);
this.graphics.drawRect(0,0,boardWidth,boardHeight);
this.graphics.endFill();
spImage = new Sprite();
this.addChild(spImage);
boardMask = new Shape();
boardMask.graphics.beginFill(0xDDDDDD);
boardMask.graphics.drawRect(0,0,boardWidth,boardHeight);
boardMask.graphics.endFill();
boardMask.x = 0;
boardMask.y = 0;
this.addChild(boardMask);
spImage.mask = boardMask;
minScale = boardWidth / bg_image.width;
mcIn = new InCursorClip();
mcOut = new OutCursorClip();
bg_image.addChild(mcIn);
bg_image.addChild(mcOut);
bg_image.scaleX = minScale;
bg_image.scaleY = minScale;
spImage.addChild(bg_image);
spImage.addChild(mcIn);
spImage.addChild(mcOut);
spImage.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
spImage.addEventListener(MouseEvent.CLICK, zoom);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
S_USA["x"] = 180.7;
S_USA["y"] = 149.9;
S_USA["bynavn"] = "New York";
S_Norway["x"] = 423.7;
S_Norway["y"] = 76.4;
S_Norway["bynavn"] = "Oslo";
S_South_Africa["x"] = -26;
S_South_Africa["y"] = 146;
S_South_Africa["bynavn"] = "Cape Town";
S_Brazil["x"] = 226;
S_Brazil["y"] = 431.95;
S_Brazil["bynavn"] = "Rio de Janeiro";
S_France["x"] = 459.1;
S_France["y"] = 403.9;
S_France["bynavn"] = "Paris";
S_China["x"] = 716.2;
S_China["y"] = 143.3;
S_China["bynavn"] = "Beijing";
S_Australia["x"] = 809.35;
S_Australia["y"] = 414.95;
S_Australia["bynavn"] = "Sydney";
// ----------------------------------------------------
airportDict["USA"] = S_USA;
airportDict["Norway"] = S_Norway;
airportDict["South Africa"] = S_South_Africa;
airportDict["Brazil"] = S_Brazil;
airportDict["France"] = S_France;
airportDict["China"] = S_China;
airportDict["Australia"] = S_Australia;
for (var k:Object in airportDict)
{
var value = airportDict[k];
var key = k;
startList.addItem({label:key, data:key});
sluttList.addItem({label:key, data:key});
var airport:Airport = new Airport(key,airportDict[key]["bynavn"]);
airport.koordinater(airportDict[key]["x"], airportDict[key]["y"]);
bg_image.addChild(airport);
}
// --------------------------------------------
// --------------------------------------------
// -----------------------------------------------
}
private function startDragging(mev:MouseEvent):void
{
spImage.startDrag();
}
private function stopDragging(mev:MouseEvent):void
{
spImage.stopDrag();
}
private function zoom(mev:MouseEvent):void
{
if ((!mev.shiftKey)&&(!mev.ctrlKey))
{
return;
}
if ((mev.shiftKey)&&(mev.ctrlKey))
{
return;
}
externalCenter = new Point(spImage.mouseX,spImage.mouseY);
internalCenter = new Point(bg_image.mouseX,bg_image.mouseY);
if (mev.shiftKey)
{
bg_image.scaleX = Math.max(scaleFactor*bg_image.scaleX, minScale);
bg_image.scaleY = Math.max(scaleFactor*bg_image.scaleY, minScale);
}
if (mev.ctrlKey)
{
trace("Minscale: ", maxScale)
trace("Returned: ", 1/scaleFactor*bg_image.scaleY)
bg_image.scaleX = Math.min(1/scaleFactor*bg_image.scaleX, maxScale);
bg_image.scaleY = Math.min(1/scaleFactor*bg_image.scaleY, maxScale);
}
mat = this.transform.matrix.clone();
MatrixTransformer.matchInternalPointWithExternal(mat,internalCenter,externalCenter);
bg_image.transform.matrix = mat;
}
private function keyHandler(ke:KeyboardEvent):void
{
mcIn.x = spImage.mouseX;
mcIn.y = spImage.mouseY;
mcOut.x = spImage.mouseX;
mcOut.y = spImage.mouseY;
mcIn.visible = ke.ctrlKey;
mcOut.visible = ke.shiftKey;
if (ke.ctrlKey || ke.shiftKey)
{
Mouse.hide();
}
else
{
Mouse.show();
}
}
private function reise(evt:MouseEvent):void
{
var new_flight:Flight = new Flight(airportDict[startList.selectedItem.label]["x"],airportDict[startList.selectedItem.label]["y"],airportDict[sluttList.selectedItem.label]["x"],airportDict[sluttList.selectedItem.label]["y"]);
bg_image.addChild(new_flight);
}
private function mouseCoordinates(event: MouseEvent):void
{
// these are the x and y relative to the object
var localMouseX:Number = bg_image.mouseX;
var localMouseY:Number = bg_image.mouseY;
trace("Local coordinates: ", localMouseX, localMouseY);
// these are the x and y relative to the whole stage
var stageMouseX:Number = event.stageX;
var stageMouseY:Number = event.stageY;
trace("Global coordinates: ", stageMouseX, stageMouseY);
}
}
}
Image class:
package {
import flash.display.Sprite;
import flash.display.MovieClip;
public class Image extends Sprite
{
public function Image(y_:Number, x_:Number)
{
this.y = y_
this.x = x_
}
}
}
You don't have minScale declared, neither maxScale.
bg_image should be declared as Sprite, otherwise Compiler Error: Implicit coercion...
`
public var bg_image:Sprite;
As your image is very small, you may want to add your listener to the stage, and not the image, it would be very dificult to click in such a small image.
Also, here is your example working with MOUSE_WHEEL, instead of Click + Ctrl / Shift
stage.addEventListener(MouseEvent.MOUSE_WHEEL, zoom);
private function zoom(mev:MouseEvent):void
{
mev.delta > 0 ?
bg_image.scaleX = bg_image.scaleY = Math.max(scaleFactor * bg_image.scaleX, minScale) :
bg_image.scaleX = bg_image.scaleY = Math.min(1/scaleFactor * bg_image.scaleX, maxScale) ;
}

Actionscript 3: Create dictionary inside class?

I'm trying to transform my actionscript 3 code from the timeline, which I previously used, to packages.
I need to define some dictionaries in the beginning of the code.
But when running the following code inside my class, actionscript returns an error.
public var S_USA:Dictionary = new Dictionary();
S_USA["x"] = -299;
S_USA["y"] = -114;
S_USA["bynavn"] = "New York";
This is the error: "1120: Access of undefined property S_USA.
EDIT: Posting the entire code:
package
{
import fl.motion.MatrixTransformer;
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.display.Shape;
import fl.transitions.Fly;
import fl.motion.MatrixTransformer;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.ui.Mouse;
import flash.text.TextField;
import flash.display.SimpleButton;
import fl.controls.List;
public class Main extends MovieClip
{
public static var bg_width = 980;
public static var bg_height = 541;
public var spImage:Sprite;
public var mat:Matrix;
public var mcIn:MovieClip;
public var mcOut:MovieClip;
public var externalCenter:Point;
public var internalCenter:Point;
public var scaleFactor:Number = 0.8;
public var minScale:Number = 0.25;
public var maxScale:Number = 10.0;
/*public static var startList:List;
public static var sluttList:List;*/
public var bynavntxt:TextField;
public var intervall:TextField;
public var create_route:SimpleButton;
public var confirm_button:SimpleButton;
public var beregn_tid:SimpleButton;
public static var S_Norway:Dictionary = new Dictionary();
S_Norway["x"] = -60;
S_Norway["y"] = -183;
S_Norway["bynavn"] = "Oslo";
public static var S_Australia:Dictionary = new Dictionary();
S_Australia["x"] = 307;
S_Australia["y"] = 153;
S_Australia["bynavn"] = "Sydney";
public static var S_China:Dictionary = new Dictionary();
S_China["x"] = 239;
S_China["y"] = -98;
S_China["bynavn"] = "Beijing";
public static var S_South_Africa:Dictionary = new Dictionary();
S_South_Africa["x"] = -26;
S_South_Africa["y"] = 146;
S_South_Africa["bynavn"] = "Cape Town";
public static var S_Brazil:Dictionary = new Dictionary();
S_Brazil["x"] = -210;
S_Brazil["y"] = 73;
S_Brazil["bynavn"] = "Rio de Janeiro";
public static var S_USA:Dictionary = new Dictionary();
S_USA["x"] = -299;
S_USA["y"] = -114;
S_USA["bynavn"] = "New York";
public static var S_France:Dictionary = new Dictionary();
S_France["x"] = -79;
S_France["y"] = -135;
S_France["bynavn"] = "Paris";
// ------------------------------------------------------
public static var Flyplasser:Dictionary = new Dictionary();
Flyplasser["USA"] = S_USA;
Flyplasser["Norway"] = S_Norway;
Flyplasser["South Africa"] = S_South_Africa;
Flyplasser["Brazil"] = S_Brazil;
Flyplasser["France"] = S_France;
Flyplasser["China"] = S_China;
Flyplasser["Australia"] = S_Australia;
public function Main()
{
// ------------------------------------
startList:List = new List();
sluttList:List = new List();
bynavntxt:TextField = new TextField() ;
intervall:TextField = new TextField() ;
create_route:SimpleButton = new SimpleButton() ;
confirm_button:SimpleButton = new SimpleButton() ;
beregn_tid:SimpleButton = new SimpleButton() ;
this.addChild(startList);
this.addChild(sluttList);
this.addChild(bynavntxt);
this.addChild(intervall);
this.addChild(create_route);
this.addChild(confirm_button);
this.addChild(beregn_tid);
// -----------------------------------------
// We use the ctrl and shift keys to display the two different cursors that were created on the stage.
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
this.addEventListener(MouseEvent.CLICK, mouseCoordinates);
trace("Main spawned");
// --------------------
this.width = bg_width;
this.height = bg_height;
// --------------------
for (var k:Object in Flyplasser)
{
var value = Flyplasser[k];
var key = k;
trace(key);
startList.addItem({label:key, data:key});
sluttList.addItem({label:key, data:key});
var airport:flyplass = new flyplass(key,Flyplasser[key]["bynavn"]);
airport.koordinater(Flyplasser[key]["x"], Flyplasser[key]["y"]);
this.addChild(airport);
}
var mcOut = new OutCursorClip();
this.addChild(mcOut);
var mcIn = new InCursorClip();
this.addChild(mcIn);
startList = new List();
this.addChild(startList)
sluttList = new List();
this.addChild(sluttList)
bynavntxt = new TextField;
this.addChild(bynavntxt)
intervall = new TextField;
this.addChild(intervall)
create_route = new SimpleButton;
this.addChild(create_route)
confirm_button = new SimpleButton;
this.addChild(confirm_button)
beregn_tid = new SimpleButton;
this.addChild(beregn_tid)
Are you setting this data inside Constructor or function?
EDIT:
Hm, where to start...
1) You know that the trick with static is not gonna work if you'll create more than 1 instance of class? In this case if you have only 1 Main class it's gonna work, but omg... Imho it's not nice to store data in static vars...
2) If you already declared:
public var bynavntxt:TextField;
public var intervall:TextField;
later just do
bynavntxt = new TextField() ;
intervall = new TextField() ;
no need for type in there.
3) Later in Main you have something like:
var mcOut = new OutCursorClip();
this.addChild(mcOut);
var mcIn = new InCursorClip();
this.addChild(mcIn);
Why are you declaring new variables with same name but without Type?
Waaaaaay up you have:
public var mcIn:MovieClip;
public var mcOut:MovieClip;
so those variables are already declared. By declaring them once again you are creating local ones. Beware!! The ones from the top would be null after that.
1) Fill the dictionary inside the Constructor (I seen by comments you have done this already).
2) you have already assigned type to bynavntxt with public var bynavntxt:TextField; you should not be doing it again with bynavntxt:TextField = new TextField(); just bynavntxt= new TextField(); will do.
This is the same with all other variables on similar type.
3) Make sure this class is going to be your "Document Class"/"Compiled Class" otherwise you will not have access to the 'stage' variable yet. To make sure you will have access to the stage variable you can always do:
/* Inside Constructor */
addEventListener(Event.ADDED_TO_STAGE, _onAddedToStage);
/* Outside Constructor */
private function _onAddedToStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, _onAddedToStage);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
}
Again, if this is going to be the compiled class, this can be skipped.

Error 1046:Type not found or was not a compile-time constant:Listing2

I am trying to add a movieclip that is generated through a loop from SearchVectorTest class file, and put that into sresultnologin class file. Then i will make that movieclip clickable, and when clicked, goes to next class,display a new stage, pull the information from that specific stored in that movieclip, and pull the rest of the information from the database and display them.
I am having an error at the moment, Type not found. I can't seem to add the Movieclip(box) as a button. on this line var newListing:Listing2 = Bolder.getChildAt(0);
sresultnologin.as
package com.clark
{
import com.clark.SearchVectorTest;
import flash.display.*;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Stage;
import fl.controls.Button;
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class sresultnologin extends MovieClip {
public var s1:Searchreult = new Searchreult ();
public function sresultnologin(){
// init
addEventListener(Event.ADDED_TO_STAGE, onadded);
function onadded (event:Event):void{
s1.x=-10;
s1.y=10;
addChild(s1);
}
var s3:SearchVectorTest= new SearchVectorTest(new Vector.<searchVO1>);
addChild (s3);
s1.SRhome.addEventListener(MouseEvent.CLICK, fl_ClickToGoTol);
s1.ARsearch.addEventListener(MouseEvent.CLICK, fl_ClickToGosearch);
if( s3.listings.length > 0 )
{
// get the first listing in the listing array
var newListing:Listing8 = s3.listings[0];
newListing.addEventListener(MouseEvent.CLICK, gotoscener);
}
else
{
}
}
// private methods
private function gotoscener(event:MouseEvent):void
{
var s9:Listingdetail = new Listingdetail ();
removeChild(s1);
addChild(s9);
}
private function fl_ClickToGoTol(event:MouseEvent):void
{
var s9:Account = new Account ();
removeChild(s1);
addChild(s9);
}
private function fl_ClickToGosearch(event:MouseEvent):void
{
var s9:searchVO1 = new searchVO1 ();
removeChild(s1);
addChild(s9);
}
}
}
SearchVectorTest.as
package com.clark
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class SearchVectorTest extends MovieClip
{
public var listings:Vector.<Listing8>;
public function SearchVectorTest(test:Vector.<searchVO1>)
{
for (var j:int = 0; j < test.length; j++)
{
trace(test[j].nobed);
trace(test[j].zip);
trace(test[j].Location);
trace(test[j].price);
}
var len:int = test ? test.length : 0;
// create your vector for your listings with the len size
listings = new Vector.<Listing8>(len, true);
var currentY:int = 200;
for (var k:int = 0; k < test.length; k++)
{
var Bolder:Listing8 = new Listing8();
Bolder.x=20;
var bf:TextField = new TextField();
var bf1:TextField = new TextField();
var bf2:TextField = new TextField();
var bf3:TextField = new TextField();
bf3.width = 100;
bf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);
bf.width = 100;
bf.autoSize = TextFieldAutoSize.CENTER;
bf1.width = 100;
bf1.autoSize = TextFieldAutoSize.CENTER;
bf2.autoSize = TextFieldAutoSize.CENTER;
bf3.autoSize = TextFieldAutoSize.CENTER;
bf3.width = 100;
bf1.y= bf.height+5;
bf.text = test[k].nobed;
bf1.text = test[k].zip;
bf2.text = test[k].Location;
bf3.text = test[k].price;
bf.x = (Bolder.height-bf.height)*.2
Bolder.addChild(bf);
Bolder.addChild(bf1);
Bolder.addChild(bf2);
Bolder.addChild(bf3);
Bolder.properties = test[k].nobed;
Bolder.properties = test[k].zip;
// position the object based on the accumulating variable.
Bolder.y = currentY;
addChild(Bolder);
Bolder.mouseChildren = false; // ignore children mouseEvents
Bolder.mouseEnabled = true; // enable mouse on the object - normally set to true by default
Bolder.useHandCursor = true; // add hand cursor on mouse over
Bolder.buttonMode = true;
listings[k] = Bolder;
currentY += Bolder.height + 10;
}
}
}
}
SearchVO1
package com.clark
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Stage;
import fl.controls.Button;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.net.URLVariables;
import flash.utils.*;
import flash.sampler.NewObjectSample;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
public class searchVO1 extends MovieClip {
private var Arvariables:URLVariables;
private var SrSend:URLRequest;
private var SaLoader:URLLoader;
public var nobed:String;
public var zip:String;
public var Location:String;
public var price:String;
public var callMethod:Function;
public var s1:searchpage = new searchpage ();
public function searchVO1():void{
addEventListener(Event.ADDED_TO_STAGE, onadded); // init
function onadded (event:Event):void{
s1.x=-10;
s1.y=10;
addChild(s1);
s1.account.addEventListener(MouseEvent.CLICK, fl_ClickToGoToaccount);
s1.searchs.addEventListener(MouseEvent.CLICK, ValidateAndsearch);
s1.Au.addEventListener(MouseEvent.CLICK, fl_ClickToGoToautomatch);
s1.setting.addEventListener(MouseEvent.CLICK, fl_ClickToGoToss);
s1.Shome.addEventListener(MouseEvent.CLICK, fl_ClickToGoTohom);
// Build the varSend variable
SrSend = new URLRequest("http://localhost/search.php");
SrSend.method = URLRequestMethod.POST;
Arvariables = new URLVariables;
SrSend.data = Arvariables;
SaLoader = new URLLoader();
SaLoader.dataFormat = URLLoaderDataFormat.TEXT;
SaLoader.addEventListener(Event.COMPLETE,Asandler);
// private methods
function Asandler(event:Event):void{
// retrieve data from php call
var resultString :String = event.target.data;
// parse result string as json object and cast it to array
var resultArray :Array = JSON.parse( resultString ) as Array;
// get the length of the result set
var len:int = resultArray.length;
// create vector of SearchVO
var searchVOs:Vector.<searchVO1> = new Vector.<searchVO1>();
// loop the result array
var i:int;
for(i=0; i<len; i++ )
{
searchVOs[i] = new searchVO1();
searchVOs[i].nobed = resultArray[i].nobed;
searchVOs[i].zip = resultArray[i].zip;
searchVOs[i].Location = resultArray[i].Location;
searchVOs[i].price = resultArray[i].price;
}
// call a function to create your boxes
// or maybe create your SearchVector class and pass it your search vector
var mySearchVector:SearchVectorTest = new SearchVectorTest(searchVOs);
addChild(mySearchVector);
}
}
}
public function searchVOs( nobed:String, zip:String, location:String, price:String )
{
this.nobed = nobed;
this.zip = zip;
this.Location = Location;
this.price = price;
}
public function ValidateAndsearch (event:MouseEvent):void {
// validate fields
Arvariables.nobed = s1.nobed.text;
Arvariables.zip = s1.zip.text;
Arvariables.Location = s1.Location.text;
Arvariables.price = s1.price.text;
SaLoader.load(SrSend);
var s7:sresultnologin = new sresultnologin()
removeChild(s1);
addChild(s7);
}
// close else condition for error handling
// close validate and send function
private function fl_ClickToGoToautomatch(event:MouseEvent):void
{
var s7:Auto = new Auto ();
removeChild(s1);
addChild(s7);
}
private function fl_ClickToGoToss(event:MouseEvent):void
{
//
}
private function fl_ClickToGoToaccount(event:MouseEvent):void
{
var s7:Account = new Account ();
removeChild(s1);
addChild(s7);
}
private function fl_ClickToGoTohom(event:MouseEvent):void
{
var s7:homepage = new homepage ();
removeChild(s1);
addChild(s7);
}
}
}
i think you have to remove these lines:
public var Bolder:MovieClip;
public var Listing2:MovieClip;
and try to get your Listing2 object like this:
var newListing:Listing2 = s3.getChildAt(0);
but if you want to access your listings in the SearchVectorTest class it's better to keep a public reference of them like this (maybe in an array or a vector):
SearchVectortest class:
public class SearchVectorTest extends MovieClip
{
/** the array of listings **/
public var listings:Vector.<Listing2>;
public function SearchVectorTest(test:Vector.<searchVO1>)
{
// put the array length in a variable -> if test = null make len = 0
var len:int = test ? test.length : 0;
// create your vector for your listings with the len size
listings = new Vector.<Listing2>(len, true);
// ...
for( var k:int = 0; k < len; k++ )
{
var bolder:Listing2 = new Listing2();
bolder.mouseChildren = false; // ignore children mouseEvents
bolder.mouseEnabled = true; // enable mouse on the object - normally set to true by default
bolder.useHandCursor = true; // add hand cursor on mouse over
bolder.buttonMode = true; // this must be set to true to have hand cursor
// add your linsting to the vector
listings[k] = bolder;
//...
}
// ...
}
}
after that you could get your listing like this:
sresultnologin.as
public class sresultnologin extends MovieClip
{
public var s1 :Searchreult = new Searchreult ();
//public var Bolder :MovieClip; // never used -> useless
//public var Listing2 :MovieClip; // never used -> useless
public function sresultnologin()
{
// init
addEventListener(Event.ADDED_TO_STAGE, onadded);
function onadded (event:Event):void
{
s1.x=-10;
s1.y=10;
addChild(s1);
}
var s3:SearchVectorTest= new SearchVectorTest(new Vector.<searchVO1>);
addChild (s3);
s1.SRhome.addEventListener(MouseEvent.CLICK, fl_ClickToGoTol);
s1.ARsearch.addEventListener(MouseEvent.CLICK, fl_ClickToGosearch);
// verify if there are enties in the array
if( s3.listings.length > 0 )
{
// get the first listing in the listing array
var newListing:Listing2 = s3.listings[0];
newListing.addEventListener(MouseEvent.CLICK, goto);
}
else
{
// do something if there are no results
}
}
//...
}
hope that helps ;)

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.