Trying to move from one actionscript page to another using a button - actionscript-3

The page it is on is CarSelect.as, the aButton should take it to a page called AClass.as however it doesnt load it.
public static var aButton:MovieClip = new AButton();
addChild (aButton);
aButton.x = 65;
aButton.y = 154;
aButton.addEventListener(MouseEvent.MOUSE_DOWN, openAClass);
function openAClass(e:MouseEvent):void
{
Mercedes.AClass.visible = true;
}
//pulling in car actionscript pages
public var aClass:MovieClip = new AClass();
public var bClass:MovieClip = new BClass();
public var cClass:MovieClip = new CClass();

Related

Error 1119 in Actionscript 3 (as3) Access of possibly undefined property text through a reference with static type money_txt

This is in the main class
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.events.Event;
public class main extends MovieClip {
public var scene = 0;
public var _money = 0;
public var gain = 1;
public var clicks = 0;
public function main() {
addEventListener(Event.ENTER_FRAME, loop);
mainbtn.addEventListener(MouseEvent.CLICK, handler);
playbtn.addEventListener(MouseEvent.CLICK, playHandler);
}
var mainbtn:button = new button();
var playbtn:playbutton = new playbutton();
var playtxt:playtext = new playtext();
var cash:money_txt = new money_txt();
var scene0:MovieClip = new MovieClip();
var scene1:MovieClip = new MovieClip();
public function loop(e:Event):void {
if(scene == 0) {
addChild(scene0)
scene0.addChild(playbtn);
playbtn.x = 300;
playbtn.y = 200;
scene0.addChild(playtxt);
playtxt.x = 300;
playtxt.y = 100;
} else {
scene0.removeChild(playbtn);
scene0.removeChild(playtxt);
}
if(scene == 1) {
addChild(scene1);
scene1.addChild(mainbtn);
mainbtn.x = 300;
mainbtn.y = 200;
scene1.addChild(cash);
cash.text = 'Money: ' + _money.toString();
} else {
scene1.removeChild(mainbtn);
}
}
public function playclickHandler(e:MovieClip) {
scene = 1;
}
public function handler(e:MouseEvent):void {
_money += gain;
clicks++;
trace('yep');
}
public function playHandler(e:MouseEvent):void {
scene = 1;
}
}
}
And This is where the error would be
C:\Users\Slime\Desktop\Art-ish\game\main.as, Line 47, Column 10 1119: Access of possibly undefined property text through a reference with static type money_txt.
Thanks for helping if you can!
these should be defined as public
public var mainbtn:button = new button();
public var playbtn:playbutton = new playbutton();
public var playtxt:playtext = new playtext();
public var cash:money_txt = new money_txt();
public var scene0:MovieClip = new MovieClip();
public var scene1:MovieClip = new MovieClip();
also it is hard to tell if money_txt, playtext, playbutton and button are classes or MovieClip instances. Convention dictates that Classes should start with a capital letter and instances with lower.
update
The issue is that if button and playbutton are buttons and playtext and money_txt are MovieClips, you should instantiate them as such.
for example if you have
public var mainbtn:button = new button();
but there is no class with name of button, mainbtn will be null. What you may need to do is
public var mainbtn:Button;
public var cash:MovieClip;
and as a part of your main or some other function, assign the instances
mainbtn = this['button'];
cash = this['money_txt'];
you can check if this worked by checking trace(cash);, which will return null if the assignment did not work.
I should stress again though, it is hard to to know what exactly is going wrong without knowing what your setup is. I'm assuming money_txt and the other classes you are defining are not actually classes with their own linkage IDs, but buttons and movieclips inside the MovieClip or stage you are putting this code in.

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.

Adding a dynamically created DataGrid as child or element does not work

I am trying to develop an application in AS3. What I am really trying to achieve is to have only one datagrid and having it show, you say, three different set of datas. (the real count will be changing dynamically, and it does not matter as the problem is not relevant with this) Yes, it has to be only one datagrid because of you know, I need a compact interface.
The class "Sonuc" has three properties which are string versions of inputs from constructor. A typical "Sonuc" object is something like this.
var sonuc1:Sonuc = new Sonuc(1,1,false);
//sonuc1.num = "1"
//sonuc1.type = "1"
//sonuc1.isTrue = "No"
The reason that I have informed you about "Sonuc" class is that I wanted you to know that class was not something too complicated. And x.mxml is the test mxml where I only load the class for testing purposes.
This is what I have coded so far
public class ResultInterface extends UIComponent
{
private const desiredWidth:int = 250;
private const desiredHeight:int = 150;
private const sonuc1:Sonuc = new Sonuc(1,1,false);
public var tablo:DataGrid = new DataGrid();
public var kolonArray:Array = new Array ();
public var sonucArray:Array = new Array ();
public var currentIndex:int = new int ();
public var prevButon:Button = new Button();
public var nextButon:Button = new Button();
public function ResultInterface():void
{
currentIndex = 0;
super();
tablo = new DataGrid();
width=desiredWidth+40;
height=desiredHeight+60;
this.tablo.width = desiredWidth;
this.tablo.height = desiredHeight;
this.tablo.x = 20;
this.tablo.y = 40;
prevButon.x = 10;
prevButon.y = genislik/2 - 10;
prevButon.width =
prevButon.height = 10;
nextButon.x = genislik +20;
nextButon.y = genislik/2 -10;
nextButon.width =
nextButon.height = 10;
var referansColl:ArrayCollection = new ArrayCollection();
sonucArray.push(referansColl);
tablo.dataProvider = sonucArray[currentIndex];
var sampleCol:DataGridColumn = new DataGridColumn();
sampleCol.dataField = "num";
sampleCol.headerText = "Number";
var sampleCol2:DataGridColumn = new DataGridColumn();
sampleCol2.dataField = "type";
sampleCol2.headerText = "Type";
var sampleCol3:DataGridColumn = new DataGridColumn();
sampleCol3.dataField = "isTrue";
sampleCol3.headerText = "Is it true?";
kolonArray.push(sampleCol,sampleCol2,sampleCol3);
tablo.columns = kolonArray;
this.addElement(tablo); //**** this is the problematic line
this.addChild(oncekiButon);
this.addChild(sonrakiButon);
}
public function getNewSonuc(incoming:Sonuc):void
{
sonucArray[currentIndex].addItem(incoming);
}
public function newTablo():void
{
var newTablo:ArrayCollection = new ArrayCollection();
currentIndex = sonucArray.push(newTablo);
}
public function prev(evt:Event):void //I haven't written event listeners yet
{
if(currentIndex > 0)
currentIndex--;
nextButon.enabled = true;
if(currentIndex == 0)
prevButon.enabled = false;
}
public function birSonrakine(evt:Event):void
{
if(currentIndex < sonucArray.length)
currentIndex++;
prevButon.enabled = true;
if(currentIndex == sonucArray.length)
nextButon.enabled = false;
}
in this version, I get a syntax error "call to a possibly undefined method addElement"
I also tried having the base class as "Sprite" and "Canvas"
when I used addChild instead of addElement, then I get runtime error "addChild is not available to this class"
when I just commented the problematic line out, everything was loaded perfectly but the datagrid itself.
Note that error occurs before sending in some data (Sonuc) to datagrid.
and when I tried with canvas and with addelement, I get "Cannot access a property or method of a null object reference" with some weird functions and classes and packages.
1009: Cannot access a property or method of a null object reference.
at mx.styles::StyleProtoChain$/initProtoChainForUIComponentStyleName()[E:\dev\4.y\frameworks\projects\framework\src\mx\styles\StyleProtoChain.as:358]
at mx.styles::StyleProtoChain$/initProtoChain()[E:\dev\4.y\frameworks\projects\framework\src\mx\styles\StyleProtoChain.as:171]
at mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::initProtoChain()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:10926]
at mx.core::UIComponent/regenerateStyleCache()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:10989]
at mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::addingChild()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:7465]
at mx.core::UIComponent/addChild()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:7162]
at mx.controls.listClasses::ListBase/createChildren()[E:\dev\4.y\frameworks\projects\mx\src\mx\controls\listClasses\ListBase.as:3772]
at mx.controls::DataGrid/createChildren()[E:\dev\4.y\frameworks\projects\mx\src\mx\controls\DataGrid.as:1143]
at mx.core::UIComponent/initialize()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:7634]
at mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::childAdded()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:7495]
at mx.core::Container/http://www.adobe.com/2006/flex/mx/internal::childAdded()[E:\dev\4.y\frameworks\projects\mx\src\mx\core\Container.as:3974]
at mx.core::Container/addChildAt()[E:\dev\4.y\frameworks\projects\mx\src\mx\core\Container.as:2618]
at mx.core::Container/addChild()[E:\dev\4.y\frameworks\projects\mx\src\mx\core\Container.as:2534]
at mx.core::Container/addElement()[E:\dev\4.y\frameworks\projects\mx\src\mx\core\Container.as:2981]
at genel.siniflar::ResultInterfaceArayuz()[C:\Users\Ege\Adobe Flash Builder\brainswift2\src\genel\siniflar\ResultInterface.as:95]
at x()[C:\Users\Ege\Adobe Flash Builder\brainswift2\src\x.mxml:27]
at _x_mx_managers_SystemManager/create()[_x_mx_managers_SystemManager.as:54]
at mx.managers.systemClasses::ChildManager/initializeTopLevelWindow()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\systemClasses\ChildManager.as:311]
at mx.managers::SystemManager/initializeTopLevelWindow()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\SystemManager.as:3057]
at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::kickOff()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\SystemManager.as:2843]
at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::preloader_completeHandler()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\SystemManager.as:2723]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.preloaders::Preloader/timerHandler()[E:\dev\4.y\frameworks\projects\framework\src\mx\preloaders\Preloader.as:542]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
I really need your help folks, please answer as soon as possible.
Well, I tried calling "addElement" from outside of the constructor, and it worked. However I don't know what did exactly cause this error and I know that my solution is not a legitimate one. And I would like to learn proper solution to this problem.
changes to resultInterface.as
// this.addElement(tablo); **** this is the problematic line, we have commented it out
this.addChild(oncekiButon);
this.addChild(sonrakiButon);
changes to x.mxml
public function onCreationComplete(evt:Event):void
{
showResult.addElement(showResult.tablo);
addElement(showResult);
}

Best way of passing values from a button with as3 event

I develop my own way of doing this simple task. However, I'm now wondering if there is a better way to do it.
The buttons:
var menuBtnEscrit:MovieClip = new MovieClip();
var mbe:btnEscritorio = new btnEscritorio();
menuBtnEscrit.addChild(mbe);
menuBtnEscrit.val = "escrit";
menuBtnEscrit.x = 80;
menuBtnEscrit.addEventListener(MouseEvent.CLICK, novoCont);
dMenu.addChild(menuBtnEscrit);
var menuBtnPublic:MovieClip = new MovieClip();
var mbp:btnPublic = new btnPublic();
menuBtnPublic.addChild(mbp);
menuBtnPublic.val = "public";
menuBtnPublic.x = 244;
menuBtnPublic.addEventListener(MouseEvent.CLICK, novoCont);
dMenu.addChild(menuBtnPublic);
And I can keep going, or create buttons trough some algorithm, and put more properties and take advantage of a MovieClip.
The handler:
private function novoCont(e=null){
if(e!=null) selecCont = new String(e.target.parent.val);
clearDisplay(dSubMenu);
clearDisplay(dConteudo);
var func:String = "cont_"+selecCont;
this[func]();
}
As you can see, there is some common task for all buttons.
If I click the first button, it will call cont_escrit() function. This mechanics works, but it is the best practice? Is there a way of optimize it?
public class BaseButton extends Button {
private var _func:Function;
public function get handler():Function {
return _func;
}
public function set handler(value:Function):void {
_func = value;
}
}
So hero you can create mbe like this
mbe = new BaseButton ();
mbe.hanler = cont_escrit;
The event hanler would be
private function novoCont(e=null){
if(e!=null) {
var selecContent:BaseButton = e.targe as BaseButton ;
var handler:Function = selecContent.handler;
handler();
}
}
Here is an example.
var menuBtnEscrit:MovieClip = new MovieClip();
var mbe:btnEscritorio = new btnEscritorio();
menuBtnEscrit.addChild(mbe);
//menuBtnEscrit.val = "escrit";
menuBtnEscrit.func = this.cont_escrit;
menuBtnEscrit.x = 80;
menuBtnEscrit.addEventListener(MouseEvent.CLICK, novoCont);
private function novoCont(e=null){
if(e!=null) {
var mc:MovieClip = e.target as MovieClip;
var func:Function = mc.func;
func();
}
clearDisplay(dSubMenu);
clearDisplay(dConteudo);
}

AS3 object array to class

I am new and having an issue with the use of classes in as3.
I have created an array of objects in my main timeline
function badPlayer()
{
var bads:Array = new Array();
for (var i=0; i<5; i++)
{
var mc = new bman();
mc.name=["mc"+i];
bads.push(mc);
_backGround.addChild(mc);
mc.x = 100;
mc.y = 100;
trace (bads);
Baddies(_backGround.mc); //here I am trying to export mc to my class
}
}
Here is a snip-it from my class. My trace statement wont even output.
public class Baddies extends MovieClip
{
private var pistolSound:pistolShot = new pistolShot();
//private var mc = new mc();
private var _rotateSpeedMax:Number = 2;
private var _gravity:Number = .68;
private var _bulletSpeed:Number = 2;
private var _maxDistance:Number = 200;
private var _reloadSpeed:Number = 500; //milliseconds
private var _barrelLength:Number = 20;
private var _bulletSpread:Number = 5;
private var _isLoaded:Boolean = true;
private var _isFiring:Boolean = true;
private var _endX:Number;
private var _endY:Number;
private var _startX:Number;
private var _startY:Number;
private var _reloadTimer:Timer;
private var _bullets:Array = [];
private var _gun:MovieClip;
private var _enemy:MovieClip;
private var _yx:Number;
private var _yy:Number;
private var _pcos:Number;
private var _psin:Number;
private var _trueRotation:Number;
public function Baddies()
{
trace("working");
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
Basically I am trying to create several bad guys (bman) and have the same code apply to each of them. I have also tried to change the linkage name of bman to Baddies with no success.
There are a few thing that are very wrong with this code.
Baddies(_backGround.mc); //here I am trying to export mc to my class
This is a typecast, as already stated in the comments. By the way Baddies isn't a good name, because it plural. You probably want to create a new bad guy, which would be done with this line:
var baddie = new Baddies();
Now your constructor uses the stage variable. This won't work because the object isn't on the stage, therefore stage is null (it may works if you drag and drop an instance to the stage in the editor). So before using the stage you actually need to add the object to the stage:
public function Baddies() {
trace("new baddie created");
}
public function init(mc:MovieClip) {
mc.addChild(this); // display this baddie
trace("working");
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
And in the badPlayer function:
var baddie = new Baddies();
baddie.init(_backGround);