AS3 Multidimensional Vector of Colors Themes - actionscript-3

Goal: Programatically choose a random color theme using Vectors.
Old Strategy: Multidimensional Array. This was working fine.
Desired Strategy: I would like to switch this to a multidimensional Vector. It's for practice and mastery as much as anything else.
Error: 1120: Access of undefined property
I am attempting to use Vectors like this:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.*;
import classes.calculations.GeoMath;
import classes.graphics.*;
import classes.ui.*;
public class Document extends MovieClip {
var colorThemes:Vector.<Vector.<uint>> = new Vector.<Vector.<uint>>(3);
colorThemes[0] = new Vector.<uint>(5); // 1120: Access of undefined property colorThemes.
colorThemes[1] = new Vector.<uint>(5); // 1120: Access of undefined property colorThemes.
colorThemes[2] = new Vector.<uint>(5); // 1120: Access of undefined property colorThemes.
colorThemes[0][0] = 0xb26002; // 1120: Access of undefined property colorThemes.
colorThemes[0][1] = 0xff9720; // 1120: Access of undefined property colorThemes.
colorThemes[0][2] = 0xff8b07; // 1120: Access of undefined property colorThemes.
colorThemes[0][3] = 0x007eb2; // 1120: Access of undefined property colorThemes.
colorThemes[0][4] = 0x07b6ff; // 1120: Access of undefined property colorThemes.
colorThemes[1][0] = 0xdc45ff; // 1120: Access of undefined property colorThemes.
...
It would be great if I could do this with a Vector literal, but I won't crud up my question with my wrong attempt at that, too.
Then, I attempt to access the Vector from the same Document class which is inside of a function newAsteroid which is inside another function timerFunction, like this:
function timerFunction(e: TimerEvent): void {
// some more code
function newAsteroid(): void {
var idx2: int = Math.floor(Math.random() * colorThemes.length);
for (var i: int = 0; i <= 10000; i++) {
var idx: int = Math.floor(Math.random() * colorThemes[idx2].length);
asteroid = new Asteroid();
asteroid.x = (Math.random() * (stage.stageWidth - 200) + 100);
asteroid.y = (Math.random() * (stage.stageHeight - 100) + 50);
asteroid.graphics.lineStyle();
//var thiscolorVector: Array = [colorThemes[idx2][idx], colorThemes[idx2][idx]];
//var thisGradientArray: Array = [1, -20];
//var thisRatiosArray: Array = [0, 250];
//asteroid.graphics.beginGradientFill("radial", thiscolorVector, thisGradientArray, thisRatiosArray);
asteroid.graphics.beginFill(colorThemes[idx2][idx], dim);
asteroid.graphics.drawRect(-1, -1, 2, 2);
addChild(asteroid);
asteroids.push(asteroid);
}
}
...
Anyone see how I can get rid of these errors and make a multidimensional Vector of color themes?
NOTE:
I really don't think the problem lies in the way I've constructed the Vector. I just tried it in another .fla file and it worked perfectly. So the problem must be in how I'm trying to access it or where I'm trying to access it from?

You're writing code inside of a class, so you need to do it inside of some method. One option is following:
public class Document extends MovieClip {
var colorThemes:Vector.<Vector.<uint>> = initColorThemes();
public function initColorThemes():Vector.<Vector.<uint>> {
var output:Vector.<Vector.<uint>> = new Vector.<Vector.<uint>>(3);
output[0] = new Vector.<uint>(5);
output[1] = new Vector.<uint>(5);
output[2] = new Vector.<uint>(5);
output[0][0] = 0xb26002;
return output;
}

The property is declared in the main class (mine is public class Document extends MovieClip {) and then it has to be filled in inside of a function just as was demonstrated in the answer marked as "accepted". I thought I'd post this because Petr Hrehorovsky's example doesn't quite work for me for some reason. Finally it all clicked when I re-read this about 1120 errors.
package {
public class Document extends MovieClip {
// the property has to go here! But not the values!
var colorThemes:Vector.<Vector.<uint>> = new Vector.<Vector.<uint>>(3);
public function Document() {
// declare the "internal" vectors
colorThemes[0] = new Vector.<uint>(5); // now I can make the "internal" vectors without an 1120 error
colorThemes[1] = new Vector.<uint>(5); // because colorThemes has been declared
colorThemes[2] = new Vector.<uint>(5); // as a property of Document class.
colorThemes[0][0] = 0xb26002; // These also work now because colorThemes has been declared
colorThemes[0][1] = 0xff9720;
colorThemes[0][2] = 0xff8b07;
colorThemes[0][3] = 0x007eb2;
colorThemes[0][4] = 0x07b6ff;
colorThemes[1][0] = 0xdc45ff;
colorThemes[1][1] = 0x733fe8;
colorThemes[1][2] = 0x23a8e8;
colorThemes[1][3] = 0x26ffd9;
colorThemes[1][4] = 0x3359ff;
colorThemes[2][0] = 0x2eccc9;
colorThemes[2][1] = 0x3d9998;
colorThemes[2][2] = 0x00ff6b;
colorThemes[2][3] = 0xff3b6c;
colorThemes[2][4] = 0xcc14a0;

Related

TypeError: Error #1009: Cannot access a property or method of a null object reference. at ColoringScreen/freezeColor()

hi I am once again asking for pointers on what I did wrong here the code works fine but when I test to play it the output message has this:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at ColoringScreen/freezeColor()
here's the code
public class ColoringScreen extends MovieClip {
var bitmapDt:BitmapData = new BitmapData (176.8, 176.8);
var ct:ColorTransform = new ColorTransform();
var hexaColor:*;
var myObject:Object;
public function ColoringScreen() {
// constructor code
chBackBtn.addEventListener(MouseEvent.CLICK, backtoMainScreen)
bitmapDt.draw(ColorPickerMc.spectrum);
ColorPickerMc.spectrum.addEventListener(MouseEvent.CLICK, freezeColor);
addEvents(oneMc, twoMc, threeMc, fourMc, fiveMc, sixMc, sevenMc, eightMc, nineMc, tenMc, elevenMc, twelveMc, thirteenMc, fourteenMc, fifteenMc, sixteenMc, seventeenMc, eighteenMc, ninteenMc, twentyMc, twentyoneMc, twentytwoMc, twentythreeMc, twentyfourMc, twentyfiveMc);
}
private function addEvents(...objects){
for(var i:Number=0; i<objects.lenght; i++){
objects[i].addEventListener(MouseEvent.CLICK, selectObject);
}
}
private function selectObject(e:MouseEvent){
myObject = e.target;
ColorPickerMc.spectrum.addEventListener(MouseEvent.MOUSE_MOVE, updateColor);
}
private function backtoMainScreen(e:MouseEvent) {
MovieClip(stage.getChildAt(0)).gotoAndStop("Intro");
}
private function updateColor(e:MouseEvent){
hexaColor = "0x"+bitmapDt.getPixel(ColorPickerMc.spectrum.mouseX, ColorPickerMc.spectrum.mouseY).toString(16);
ct.color = hexaColor;
myObject.transform.colorTransform = ct;
}
private function freezeColor(e:MouseEvent){
myObject.transform.colorTransform = ct;
ColorPickerMc.spectrum.removeEventListener(MouseEvent.MOUSE_MOVE, updateColor);
}
}
I know it's been asked a million times but I'm still scratching my head as to what property or method or what instance am I lacking cuz I checked it and all seem to be in place I named my instances and movie clip symbols all of it are in the same keyframe why does my freezeColor not work anymore? any help would be much appreciated

as3 1119: Access of possibly undefined property getters/setters

It would be amazing if someone could expand on the current answer, Thanks.
full error
Line 22 1119: Access of possibly undefined property CharacterX through a reference with static type flash.display:DisplayObject.
I'm trying to set a variable for the object shark, that is already defined in the object character
First time using setters in flash, so I might not be doing this right.
code I'm using to set the variable I tried to comment out the stuff I thought was irrelevant to this issue, not actually commented out in real code.
var character:Character;
//var bullet:Bullet=null;
//var bullets:Array = new Array();
//var enemies:Array = new Array();
//character=new Character(bullets);
addChild(character);
var shark:Shark=new Shark();
addChild(shark);
//var enemy:Enemy=null;
////var i:int;
//for (i=0; i<10; i++) {
//enemy = new Enemy(Math.random()*stage.stageWidth, Math.random()*stage.stageHeight);
//addChild(enemy);
// enemies.push(enemy);
//}
//stage.addEventListener(Event.ENTER_FRAME, colTest);
//function colTest(e:Event ):void {
// if(character.hitTestObject(turtle)){
// character.gotoAndStop("Turtle");
// }
//}
shark.setT(character.x, character.y)
class in which I'm attempting to define a variable using the function above.
package
{
import flash.display.*;
import flash.events.*;
public class Shark extends MovieClip
{
var CharacterX:Number = 0;
var CharacterY:Number = 0;
public function Shark()
{
this.x = 300;
this.y = 200;
addEventListener(Event.ENTER_FRAME,playGame);
}
public function setT(characterx:Number,charactery:Number){
CharacterX = characterx - this.x;
CharacterY = charactery - this.y;
}
function playGame(event:Event):void
{
var ease:int = 20;
var speed:int = 10;
var targetX:int = root.CharacterX - this.x;
var targetY:int = root.CharacterY - this.y;
var rotation = Math.atan2(targetY,targetX) * 180 / Math.PI;
cut code off here, didn't want to make a code dump can get you anything that might be relevant just ask.
Here is a pastebin of all of the code if it might help,
Shark class:
Actions on Frame 1:
Character class
Let me start by saying I can't spot the exact problem here, but I have some ideas. Your error 1999 says that something of the type display object is trying to change your variable. This happens a lot when you use parent.myMethod() because parent is typed as display object. You would fix this by typecasting like (parent as MovieClip).myMethod
In your case I don't see the exact source of this problem. But you could try using this.characterX in your setT function

1120: Access of undefined property shuffledArray

Please can you help me out I am new to as3 and I am trying to create a shuffled deck using the Fisher-Yates Algorithm. When I run the code with ctrl-enter it compiles with no errors but when I try to output it with trace(); it comes back with:
Scene 1, Layer 'actions', Frame 1, Line 6 1120: Access of undefined property shuffledArray.
Like I said I am new to this and it will be me doing something very stupid but all the same i'm stuck.
Here is the code
package src.CardDeck
{
public class CardDeck
{
public var allCards:Array = [];
public var cardNames:Array;
public var cardValues:Array;
public var gameType:String;
public var drawnCards:uint = 0;
public function CardDeck(game:String)
{
gameType = game;
cardNames = ["Ace","Two","Three",
"Four","Five","Six",
"Seven","Eight","Nine",
"Ten","Jack","Queen","King"];
if(gameType == "texasholdem")
{
cardValues = [1,2,3,4,5,6,7,8,9,10,10,10,10];
}
makeSuit("Spade");
makeSuit("Heart");
makeSuit("Diamond");
makeSuit("Club");
}
function makeSuit(suitString:String):void
{
var card:Object;
for(var i:uint = 0; i < cardNames.length; i++)
{
card = {};
card.cardType = suitString;
card.cardName = cardNames[i];
card.cardValue = cardValues[i];
card.isDrawn = false;
allCards.push(card);
}
}
public function shuffleFisherYates():Array
{
var shuffledArray:Array = [];
var randomCardIndex: int;
do
{
randomCardIndex = Math.floor(Math.random()* allCards.length);
shuffledArray.push(allCards[randomCardIndex]); // add to mix
allCards.splice(randomCardIndex,1); // remove from deck
}while(allCards.length); // Meaning while allCards.length != 0
return shuffledArray;
}
}
}
and here is the .fla actions layer
import src.CardDeck.CardDeck;
var deck:CardDeck = new CardDeck("texasholdem");
trace(shuffledArray);
I know its probably something silly but i'm struggling.
Thanks in advance!
Paul
var deck:CardDeck = new CardDeck("texasholdem");
trace(shuffledArray);
This doesn't work because shuffledArray isn't defined there.
Try :
var deck:CardDeck = new CardDeck("texasholdem");
var array:Array = deck.shuffleFisherYates();
for(var i:int=0; i<array.length; i++)
{
trace(array[i].cardName);
trace(array[i].cardType);
trace(array[i].cardValue);
trace(array[i].isDrawn);
}
"shuffledArray" is a property inside of your CardDeck object. To access public methods and properties within it, you need to use the dot syntax:
trace(deck.shuffleFisherYates());
However, depending on what you are doing, you may not need to really be accessing the array directly, if your CardDeck object is meant to control the entire deck.

as3 - passing a library image name to function which then adds to stage

I'm using the following function to add some images from the library to the stage.
function AddImage(image_name:String):void {
if(image_count == 4) return;
// change the following line so it uses "image_name"
var defaultImage:added_1 = new added_1(100, 100);
var tmpImage:Bitmap = new Bitmap(defaultImage);
tmpImage.x = 124.5 + (108.5 * image_count);
tmpImage.y = 1511.9;
addChild(tmpImage);
image_count++;
}
What I'd like to be able to do is pass the image name as a string parameter to the function but can't seem to figure out how to do this.
Can someone help me out?
What you want to do is get the Class Definition via using getDefinitionByName so that you can create an instance, the following code is how you do that :
// you'll need to add this import to use getDefinitionByName
import flash.utils.getDefinitionByName;
function AddImage(image_name:String):void {
if(image_count == 4) return;
// this next line gets the class definition of the image_name
var imageClass:Class = getDefinitionByName(image_name) as Class;
// this is how you create an instance of that class
var defaultImage:BitmapData = new imageClass(100, 100);
var tmpImage:Bitmap = new Bitmap(defaultImage);
tmpImage.x = 124.5 + (108.5 * image_count);
tmpImage.y = 1511.9;
addChild(tmpImage);
image_count++;
}
The changes are the import :
import flash.utils.getDefinitionByName;
and these two lines :
var imageClass:Class = getDefinitionByName(image_name) as Class;
var defaultImage:BitmapData = new imageClass(100, 100);
Note ---
Also wanted to mention that in certain cases you might run into an issue if you are compiling with Flex as opposed to the Flash IDE, where you get the following error :
ReferenceError: Error #1065: Variable <YourImageClassName> is not defined.
The way to handle that situations is by declaring a variable in your class variable declarations for the compiler, so it recognizes that symbol.
So if your two images class names were image_1 and image_2, in your class declarations you would do something like :
private var forCompiler1:image_1;
private var forCompiler2:image_2;
If you have a ton of images, that might be a pain, but that's the only way I've found to get the compiler to recognize them. :/ haha

Cannot access a property or method of a null object reference Flex

I've been trying to learn flex/flash programming and am working on a project where I need to populate a spinner list in flex dynamically from a string. I have a function that separates the string using "split" and now I need to populate an array list. I have been working with this stupid big for hours now and can;t find help anywhere. I keep getting the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at views::CommonPlaces/initApp()[/Users/twing207/Documents/Adobe Flash Builder 4.6/AmericanTaxi/src/views/CommonPlaces.mxml:30]
My code is here:
<fx:Script>
<![CDATA[
import mx.collections.ArrayList;
import spark.events.IndexChangeEvent;
var Arr1:Array;
var Arr2:Array;
var Arr3:Array;
[Bindable]
public var CommonPlacesArray:ArrayList;
var CommonPlacesData:String = new String("2133664:American Taxi Dispatch, Inc:Mount Prospect:834 E RAND RD|2133665:Walmart:Mount Prospect:930 Mount Prospect Plaza|2228885:Garage:Des Plaines:1141 Lee St|2228886:Asian Island:Palatine:1202 E Dundee Rd|2229464:Kohl's:Arlington Heights:700-856 W Dundee Rd|");
var CurrentSelect:String = new String();
private function initApp():void {
Arr1 = CommonPlacesData.split("|");
var arrLength:Number = new Number(Arr1.length);
for (var i:Number = 0; i < (arrLength - 1); i++) {
CurrentSelect = new String(Arr1[i]);
Arr2 = CurrentSelect.split(":");
//THE LINE BELOW IS WHERE IT STOPS:
CommonPlacesArray.addItem(Arr2[1]);
}
}
It doesn't seem to like the "CommonPlacesArray.addItem" line. Any help or a point in the right direction would be great. Thanks in advanced!
On another note, I am also getting the error: "Access of undefined property: data" on the following:
Here in another view I set the value for data.UserCommonReturnData to a string.
function LoginLoaded (e:Event):void {
trace(e.target.data);
var ServerReturn:String;
ServerReturn = new String(e.target.data);
data.UserCommonReturnData = ServerReturn;
navigator.pushView(CommonPlaces, data);
}
and here I try to pull it back:
var CommonPlacesData:String = new String();
var CurrentSelect:String = new String();
//The next line gives the error:
CommonPlacesData = data.UserCommonReturnData;
Any idea??
You never construct CommonPlacesArray, you just declare it.
var CommonPlacesArray:ArrayList = new ArrayList();
If you check the Array List API you can also clearly see it has a constructor that accepts an array, meaning you can copy the data to it without having to iterate over it yourself.