As3 Error: Error #1009: Cannot access a property or method of a null object reference - actionscript-3

I seem to be having an error with my inventory system.
This is my class:
package
{
import flash.display.*;
public class InventoryDemo extends MovieClip
{
var inventory:Inventory;
public function InventoryDemo()
{
inventory = new Inventory(this);
inventory.makeInventoryItems([d1,d2]);
}
}
}
I have already placed d1 and d2 objects in a second keyframe.
And this is the child class:
package
{
import flash.display.*;
import flash.events.*;
public class Inventory
{
var itemsInInventory:Array;
var inventorySprite:Sprite;
public function Inventory(parentMC:MovieClip)
{
itemsInInventory = new Array ;
inventorySprite = new Sprite ;
inventorySprite.x = 50;
inventorySprite.y = 360;
parentMC.addChild(inventorySprite);
}
function makeInventoryItems(arrayOfItems:Array)
{
for (var i:int = 0; i < arrayOfItems.length; i++)
{
arrayOfItems[i].addEventListener(MouseEvent.CLICK,getItem);
arrayOfItems[i].buttonMode = true;
}
}
function getItem(e:Event)
{
var item:MovieClip = MovieClip(e.currentTarget);
itemsInInventory.push(item);
inventorySprite.addChild(item);
item.x = itemsInInventory.length - 1 * 40;
item.y = 0;
item.removeEventListener(MouseEvent.CLICK,getItem);
item.addEventListener(MouseEvent.CLICK,useItem);
}
function useItem(e:Event)
{
var item:MovieClip = MovieClip(e.currentTarget);
trace(("Use Item:" + item.name));
}
}
}
The code works when I try it in a black project with only d1 and d2 in the stage. Can any one help me solve this?

Your error suggests that d1 and d2 are not created at the time that you call
inventory.makeInventoryItems([d1,d2]);
You suggest that you have created d1 and d2 in the second keyframe, but if InventoryDemo is your DocumentClass, then it will run before you reach the second keyframe.
So, if you want to use d1 and d2 you either need to move them to the first frame, or you need to not try and use them until the second frame of the Movie has occurred.
If you want to keep d1 and d2 on the second frame, then you need to take the call out of the constructor and into a new function that you call on the second frame:
public function InventoryDemo()
{
//Do nothing here
//inventory = new Inventory(this);
//inventory.makeInventoryItems([d1,d2]);
}
public function initialiseInventory():void
{
//Initialise you inventory here.
inventory = new Inventory(this);
inventory.makeInventoryItems([d1,d2]);
}
Then on the 2nd frame of your timeline call the function:
initialiseInventory();
stop();

Related

Removing an Object when it hits another object AS3

Beginner here. I have a symbol on the timeline with an instance name of 'island', so basically I want to remove the cells that hits the 'island'
if (cell.hitTestObject (island)) {
if(stage.contains(cell))
removeChild (cell);
}
I tried this one under the moveCell function but it only removes one cell instead of every cell that hits the island. Thanks everyone!
Here's my code so far:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Main extends MovieClip {
public var cell:Cell;
public var group:Array;
public var gameTimer:Timer;
public function Main() {
cell = new Cell (400, -15);
addChild (cell);
group = new Array();
var newCell = new Cell (100, -15);
group.push ( newCell);
addChild(newCell);
gameTimer = new Timer (25);
gameTimer.addEventListener(TimerEvent.TIMER,moveCell);
gameTimer.start();
}
public function moveCell (timerEvent:TimerEvent):void {
if (Math.random() < 0.01) {
var randomX:Number = Math.random() * 700;
var newCell:Cell = new Cell (randomX, -15);
group.push (newCell);
addChild(newCell);
}
for each(var i:MovieClip in group) {
if (i.hitTestObject(island)) {
i.visible = false;
//i.parent.removeChild(i);
var score:int = 0;
score ++;
scoreOutPut.text = score.toString();
}
}
}
}
}`
You got the "Cannot access a property or method of a null object reference" because you've removed the Cell object from the DisplayObjectContainer (its parent) but not from the group array, so in the next iteration of your for loop, that object didn't exist anymore and that error will be fired.
To avoid that you can do like this :
for(var i:int = 0; i < group.length; i++)
{
var cell:Cell = Cell(group[i]);
if (cell.hitTestObject(island))
{
cell.parent.removeChild(cell);
group.splice(i, 1);
score++;
}
}
For the score, it should be a global property for all your class to get updated every time.
Also, for your code to be more organised and clearer, it's better to put every task in a single method.
For example, for creating cells, you can use a createCell() method :
// 0 is the default value of __x and -15 is the default one of __y
private function createCell(__x:Number = 0, __y:Number = -15): void
{
var cell:Cell = new Cell(__x, __y);
group.push(cell);
addChild(cell);
}
Then you can use it in any place in your code, for example, for your two first cells that you create in the constructor :
public function Main()
{
// ..
createCell(400);
createCell(100);
// ...
}
Or inside the moveCell() method :
if (Math.random() < 0.01)
{
var randomX:Number = Math.random() * 700;
createCell(randomX);
}
Also, if you don't really need that a property or a method to be public, don't put it as public.
...
Hope that can help.

Incorrect number of arguments. Expected 2

I'm making a game where you are a soccer goalie and the ball(brazuca) keeps on falling down and you have to collect it, but I keep getting the incorrect number of arguments error. I'm fairly new to coding and I know that the error means something unexpected happened in the brackets where the error happened but I can't figure out how to fix it. It would be great if anyone can help me with this issue.
class Gamescreen2
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.*;
import flash.ui.Mouse;
import flash.media.SoundChannel;
public class Gamescreen2 extends MovieClip
{
public var goalie:GoaliePlay;
var gameMusic:GameMusic;
var gameChannel:SoundChannel;
var speed:Number;
//public var army:Array;
public var army2:Array;
//public var gameTimer:Timer;
public var gameTimer2:Timer;
public static var tick:Number;
public static var score:Number;
public function Gamescreen2()
{
goalie = new GoaliePlay();
addChild (goalie);
gameMusic = new GameMusic;
gameChannel = gameMusic.play(0,999);
score = 0;
//army = new Array();
army2 = new Array();
//gameTimer = new Timer(25);
gameTimer2 = new Timer(25);
//gameTimer.addEventListener(TimerEvent.TIMER, onTick);
gameTimer2.addEventListener(TimerEvent.TIMER, onTick);
gameTimer2.start();
tick = 0;
}
public function onTick(timerEvent:TimerEvent):void
{
tick ++;
if (Math.random() < 0.05)
{
var randomX:Number = Math.random() * 550;
var goalieB= new GoalieB(0,0);
army2.push (goalieB);
addChild (goalieB);
}
for each (var goalieB:GoalieB in army2)
{
goalieB.moveDownABit();
if(goalieB.y == 420)
{
score--
trace(score);
this.scoreTxttwo.text = score.toString();
}
if (goalie.collisionArea.hitTestObject(goalieB))
{
goalieB.x == 550;
score += 10;
}
if (score < 1)
{
score = 0;
}
}
}
}
}
class BrazucaPlay
package
{
import flash.display.MovieClip;
public class BrazucaPlay extends MovieClip
{
var speed:Number;
public function BrazucaPlay(startX:Number, startY:Number)
{
x = startX;
y = startY;
speed = Math.random();
}
public function moveDownABit():void
{
//two speed enemies
if (speed >= .25)
{
y = y + 3;
}
}
}
}
Might be the constructor of your GameMusic class.
gameMusic = new GameMusic;
You should have brackets like so:
gameMusic = new GameMusic();
Same with this line:
var newBrazuca = new BrazucaPlay;
Should be:
var newBrazuca = new BrazucaPlay();
If, after adding brackets () you still receive the error, then you should check your custom classes BrazucaPlay and GoaliePlay and make sure their constructors aren't expecting parameters. Also check this function: brazuca.moveDownABitB().
The constructor is the function that is named after the class and is what first runs when instantiate an object. So you do var newBrazuca = new BrazucaPlay(); there is a constructor function in the BrazucaPlay class that would look something like this:
public function BrazucaPlay(){
//some code.
}
If that function actually looked something like this:
public function BrazucaPlay(myParameter:String){ }
Then that would throw the error you're getting because it's expecting you to pass a parameter to it (in this case a string like new BrazucaPlay("blah blah blah"))
EDIT
Now that you've posted more code, the cause is quite clear. The constructor of your BrazucaPlay class is expecting two arguments (a starting x/y position). So when you instantiate a new BrazucaPlay instance, you are required to pass it those two parameters:
var newBrazuca = new BrazucaPlay(0,0);//you need to pass two numbers (starting x and y)
If you don't want to do this, you can change the code to make those parameter OPTIONAL.
//this makes the parameters default to 0 if no value is passed in
public function BrazucaPlay(startX:Number = 0, startY:Number = 0)
{
x = startX;
y = startY;
speed = Math.random();
}
Welcome, user. Okay, a couple of things...
First, just so you know, we need the entire error, as CyanAngel said. It helps us prevent digging through the entire code. (For more tips about getting started, go to stackoverflow.com/help)
Second, this is what your error message means: You are passing the wrong number of arguments (values) to a function. The function requires exactly two, and you're passing more or less than you should.
This is why we need the error: it has the line number of the error, letting us/you narrow in on it directly.

AS3: Error 1009: Null reference

I am making a game in ActionScript 3. I have a Menu Class with a method that renders a Menu.
I make an instance of Menu in my Main class, and then call the method. When I debug the application I get a null reference error. This is the code of the menu class:
package
{
import flash.display.MovieClip;
import menucomponents.*;
public class Menu extends MovieClip
{
public function Menu()
{
super();
}
public function initMenuComponents():void{
var arrMenuButtons:Array = new Array();
var btnPlay:MovieClip = new Play();
var btnOptions:MovieClip = new Options();
var btnLikeOnFacebbook:MovieClip = new LikeOnFacebook();
var btnShareOnFacebook:MovieClip = new ShareOnFacebook()
arrMenuButtons.push(btnPlay);
arrMenuButtons.push(btnOptions);
arrMenuButtons.push(btnLikeOnFacebbook);
arrMenuButtons.push(btnShareOnFacebook);
var i:int = 0;
for each(var item in arrMenuButtons){
item.x = (stage.stageWidth / 2) - (item.width / 2);
item.y = 100 + i*50;
item.buttonMode = true;
i++;
}
}
}
}
Thanks in advance.
Your issue is likely that stage is not populated yet when your for loop runs. Try the following:
public class Main extends MovieClip {
public function Main() {
super();
var menu:Menu = new Menu();
//it's good practice - as sometimes stage actually isn't populated yet when your main constructor runs - to check, though in FlashPro i've never actually encountered this (flex/flashBuilder I have)
if(stage){
addedToStage(null);
}else{
//stage isn't ready, lets wait until it is
this.addEventListener(Event.ADDED_TO_STAGE,addedToStage);
}
}
private function addedToStage(e:Event):void {
menu.addEventListener(Event.ADDED_TO_STAGE,menuAdded); //this will ensure that stage is available in the menu instance when menuAdded is called.
stage.addChild(menu);
}
private function menuAdded(e:Event):void {
menu.initMenuComponents();
}
}

How can I call my as. class file from another frame?

I have a code set up in an external as. class file this class file allows for a couple mouse features such as color transformations using swatches. Now the code works well but it only seems to work in frame 1. I want to move all my artwork from frame 1 into frame 4 while loading the as. class file to accompany my features in frame 4. So basically I am just trying to move everything from frame 1 to frame 4. HOW IS THIS DONE ? I tried going to publish settings and changing the eport classes option to frame 4 but no success. Heres my code which is in an external as. file.
package code
{
/*****************************************
* DrawingSample4 :
* Demonstrates dynamic color transformations.
* -------------------
* See 4_color.fla
****************************************/
import fl.events.SliderEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.SimpleButton;
import flash.display.MovieClip;
import flash.geom.ColorTransform;
public class Drawing5 extends MovieClip
{
//*************************
// Properties:
public var activeSwatch:MovieClip;
// Color offsets
public var c1:Number = 0; // R
public var c2:Number = 51; // G
public var c3:Number = 51; // B
//*************************
// Constructor:
public function Drawing5()
{
// Respond to mouse events
swatch1_btn.addEventListener(MouseEvent.CLICK,swatchHandler,false,0,false);
swatch2_btn.addEventListener(MouseEvent.CLICK,swatchHandler,false,0,false);
swatch3_btn.addEventListener(MouseEvent.CLICK,swatchHandler,false,0,false);
swatch4_btn.addEventListener(MouseEvent.CLICK,swatchHandler,false,0,false);
swatch5_btn.addEventListener(MouseEvent.CLICK,swatchHandler,false,0,false);
swatch6_btn.addEventListener(MouseEvent.CLICK,swatchHandler,false,0,false);
apply_btn.addEventListener(MouseEvent.CLICK,clickHandler,false,0,true);
previewBox_btn.addEventListener(MouseEvent.MOUSE_DOWN,dragPressHandler);
// Respond to drag events
red_slider.addEventListener(SliderEvent.THUMB_DRAG,sliderHandler);
red_slider.addEventListener(Event.CHANGE,sliderHandler);
green_slider.addEventListener(SliderEvent.THUMB_DRAG,sliderHandler);
green_slider.addEventListener(Event.CHANGE,sliderHandler);
blue_slider.addEventListener(SliderEvent.THUMB_DRAG,sliderHandler);
blue_slider.addEventListener(Event.CHANGE,sliderHandler);
// Respond to textfield events
hexc1_txt.addEventListener(Event.CHANGE,changeHexHandler);
hexc2_txt.addEventListener(Event.CHANGE,changeHexHandler);
hexc3_txt.addEventListener(Event.CHANGE,changeHexHandler);
c1_txt.addEventListener(Event.CHANGE,changeRGBHandler);
c2_txt.addEventListener(Event.CHANGE,changeRGBHandler);
c3_txt.addEventListener(Event.CHANGE,changeRGBHandler);
// Draw a frame later
addEventListener(Event.ENTER_FRAME,draw);
}
//*************************
// Initialization:
protected function draw(event:Event):void
{
// Set color transformations
swatch1_btn.chipfill.transform.colorTransform = new ColorTransform(0,0,0,1,127,0,97);
swatch2_btn.chipfill.transform.colorTransform = new ColorTransform(0,0,0,1,130,0,0);
swatch3_btn.chipfill.transform.colorTransform = new ColorTransform(0,0,0,1,10,106,0);
swatch4_btn.chipfill.transform.colorTransform = new ColorTransform(0,0,0,1,0,51,51);
swatch5_btn.chipfill.transform.colorTransform = new ColorTransform(0,0,0,1,0,51,102)
swatch6_btn.chipfill.transform.colorTransform = new ColorTransform(0,0,0,1,127,127,127);
previewBox_btn.transform.colorTransform = new ColorTransform(0,0,0,1,c1,c2,c3);
// Handle listeners...
removeEventListener(Event.ENTER_FRAME,draw);
}
//*************************
// Event Handling:
protected function dragPressHandler( event:MouseEvent ):void
{
activeSwatch = new Swatch();
activeSwatch.transform.colorTransform = new ColorTransform(0,0,0,1,c1,c2,c3);
activeSwatch.addEventListener(MouseEvent.MOUSE_UP,dragReleaseHandler);
activeSwatch.startDrag(true);
addChild(activeSwatch);
}
protected function dragReleaseHandler( event:MouseEvent ):void
{
if( activeSwatch.hitTestObject(car) ){
car.transform.colorTransform = new ColorTransform(0,0,0,1,c1,c2,c3);
}
activeSwatch.stopDrag();
activeSwatch.removeEventListener(MouseEvent.MOUSE_UP,dragReleaseHandler);
removeChild(activeSwatch);
}
protected function clickHandler(event:MouseEvent):void
{
car.transform.colorTransform = new ColorTransform(0,0,0,1,c1,c2,c3);
}
protected function changeRGBHandler(event:Event):void
{
c1 = Number(c1_txt.text);
c2 = Number(c2_txt.text);
c3 = Number(c3_txt.text);
if(!(c1>=0)){
c1 = 0;
}
if(!(c2>=0)){
c2 = 0;
}
if(!(c3>=0)){
c3 = 0;
}
hexc1_txt.text = c1.toString(16);
hexc2_txt.text = c2.toString(16);
hexc3_txt.text = c3.toString(16);
updateSliders();
}
protected function changeHexHandler(event:Event):void
{
c1 = parseInt("0x"+hexc1_txt.text);
c2 = parseInt("0x"+hexc2_txt.text);
c3 = parseInt("0x"+hexc3_txt.text);
if(!(c1>=0)){
c1 = 0;
}
if(!(c2>=0)){
c2 = 0;
}
if(!(c3>=0)){
c3 = 0;
}
c1_txt.text = c1.toString();
c2_txt.text = c2.toString();
c3_txt.text = c3.toString();
updateSliders();
}
protected function sliderHandler( event:SliderEvent ):void
{
switch( event.target )
{
case red_slider:
c1 = red_slider.value;
break;
case green_slider:
c2 = green_slider.value;
break;
case blue_slider:
c3 = blue_slider.value;
break;
}
updateFields();
}
protected function swatchHandler(event:MouseEvent):void
{
switch( event.target.parent )
{
case swatch1_btn:
c1 = 127;
c2 = 0;
c3 = 97;
break;
case swatch2_btn:
c1 = 130;
c2 = 0;
c3 = 0;
break;
case swatch3_btn:
c1 = 10;
c2 = 106;
c3 = 0;
break;
case swatch4_btn:
c1 = 0;
c2 = 51;
c3 = 51;
break;
case swatch5_btn:
c1 = 0;
c2 = 51;
c3 = 102;
break;
case swatch6_btn:
c1 = 127;
c2 = 127;
c3 = 127;
break;
}
updateFields();
updateSliders();
}
//*************************
// Utils:
protected function updateFields():void
{
hexc1_txt.text = c1.toString(16);
hexc2_txt.text = c2.toString(16);
hexc3_txt.text = c3.toString(16);
c1_txt.text = c1.toString();
c2_txt.text = c2.toString();
c3_txt.text = c3.toString();
previewBox_btn.transform.colorTransform = new ColorTransform(0,0,0,1,c1,c2,c3);
}
protected function updateSliders():void
{
red_slider.value = c1;
green_slider.value = c2;
blue_slider.value = c3;
previewBox_btn.transform.colorTransform = new ColorTransform(0,0,0,1,c1,c2,c3);
}
}
}
the issue is when you load/call this class it is looking for objects on the stage and if they are not there it will throw errors. So you shouldn't call it until it is on the same frame as the items it is looking for. So if all of your items it is looking for are on frame 4, that is where you should call it.
So go to frame 4 and add this code:
var myDrawings5 = new Drawing5();
Another option is moving the code currently in the constructor function into a new public function called init(). Then you can load the class whenever you want but you don't call the init() until the items are on the stage.
public function Drawing5(){
// now emtpy
}
public function init(){
// move code from Drawing5() here
}
Then load it and call it where needed
var myDrawings5 = new Drawing5();
myDrawings5.init();
I think your problem is that you were using Drawing5 as your document class before. Now you want to run it stand alone in frame 4. To do this, you will need to give the Drawing5 class a reference to your swfs stage so it knows where to find all of those objects now.
var d5:Drawing5 = new Drawing5(this);
then add a property to Drawing5 class called _container (or something)
private var _container:MovieClip;
then in your Drawing 5 constructor, change it to accommodate the new var and set the container
public function Drawing5(container:MovieClip){
_container = container;
}
Then in Drawing5, change ALL calls that reference an object like a list or button to include the container in the path like this:
// Respond to mouse events
_container.swatch1_btn.addEventListener(MouseEvent.CLICK,swatchHandler,false,0,false);
I hope I understood your problem correctly and this helps.

AS3 Object Array not adding to stage

So I am new to AS3 and AS in general. I do know Java to a degree and PHP very well.
I am trying to learn by writing an app that creates a hex map programatically. I am using a library for hexagons which I will include below however, here is my issue.
When I run a for loop for say, a 10 count, and I then create a shape, add the shape and draw the shape, it has no issue. When in the same loop if I push a shape to an array, then add that array element, then draw it, I get nothing.
package
{
import flash.display.MovieClip;
import flash.display.Shape;
import flash.text.TextField;
public class Main extends MovieClip
{
private var radius:Number = 30;
private var sides:Number = 6;
private var myrotation:Number = 0;
private var lineColor:Number = 0x000000;
private var lineThickness:Number = 1;
private var l:TextField = new TextField();
private var f:Array = new Array(20);
public function Main()
{
l.text = "test";
addChild(l);
//WORKS should be red
for(var j:int = 0; j<stage.stageWidth/(radius*2);j++)
{
var t:Polygon = new Polygon();
addChild(t);
t.drawPolygon(radius,sides,2*radius*j,radius*2,0xFF0000,lineThickness,myrotation);
}
//DOES NOT WORK (note the *3 is so it is lower down) should be blue
for(var j:int = 0; j<stage.stageWidth/(radius*2);j++)
{
f.push(new Polygon());
addChild(f[j]);
f[j].drawPolygon(radius,sides,2*radius*j,radius*3,0x0000FF,lineThickness,myrotation);
}
}
}
}
The polygon class I am using is below. I grabbed it off the internets somewhere.
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Polygon extends MovieClip
{
//PROPERTIES
private var points:Array;
private var id:int;
private var ratio:Number;
private var top:Number;
private var fade_value:int;
//CONSTRUCTOR
public function Polygon()
{
addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
stage.frameRate=31;
}
//METHODS
public function drawPolygon(radius:int,segments:int,centerX:Number,centerY:Number,tint:uint,line:int,rotating:Number):void
{
id=0;
points=new Array();
ratio=360/segments;
top=centerY-radius;
for(var i:int=rotating;i<=360+rotating;i+=ratio)
{
var xx:Number=centerX+Math.sin(radians(i))*radius;
var yy:Number=top+(radius-Math.cos(radians(i))*radius);
points[id]=new Array(xx,yy);
if(id>=1)
{
drawing(points[id-1][0],points[id-1][1],points[id][0],points[id][1],tint,line);
}
id++;
}
id=0;
}
private function radians(n:Number):Number
{
return(Math.PI/180*n);
}
private function drawing(startX:Number,startY:Number,stopX:Number,stopY:Number,tint:Number,line:int):void
{
graphics.lineStyle(line,tint,1);
graphics.moveTo(startX,startY);
graphics.lineTo(stopX,stopY);
}
public function fadeOut():void
{
fade_value=0;
addEventListener(Event.ENTER_FRAME,fade);
}
public function fadeIn():void
{
fade_value=1;
addEventListener(Event.ENTER_FRAME,fade);
}
//PRIVATE
private function fade(evt:Event):void
{
var da:Number=fade_value-alpha;
var aa:Number=da*.1;
alpha+=aa;
if(Math.abs(da)<=.05)
{
alpha=fade_value;
removeEventListener(Event.ENTER_FRAME,fade);
if(fade_value==0)
dispatchEvent(new Event('fadeOutDone'));
else
dispatchEvent(new Event('fadeInDone'));
}
}
}
}
----EDIT----
the code is now as follows with the same issues occurring. I've tried adding a vector and i tried casting the array element.
package
{
import flash.display.MovieClip;
import flash.display.Shape;
import flash.text.TextField;
public class Main extends MovieClip
{
private var radius:Number = 30;
private var sides:Number = 6;
private var myrotation:Number = 0;
private var lineColor:Number = 0x000000;
private var lineThickness:Number = 1;
private var l:TextField = new TextField();
private var f:Array = new Array(20);
private var v:Vector.<Polygon> = new Vector.<Polygon>(10);
public function Main()
{
l.text = "test";
addChild(l);
//WORKS should be red
for(var i:int = 0; i<stage.stageWidth/(radius*2);i++)
{
var t:Polygon = new Polygon();
addChild(t);
t.drawPolygon(radius,sides,2*radius*i,radius*2,0xFF0000,lineThickness,myrotation);
}
//DOES NOT WORK (note the *3 is so it is lower down) should be blue
for(var j:int = 0; j<stage.stageWidth/(radius*2);j++)
{
f.push(new Polygon());
addChild(f[j]);
Polygon(f[j]).drawPolygon(radius,sides,2*radius*j,radius*3,0x0000FF,lineThickness,myrotation);
}
for(var k:int = 0; k<stage.stageWidth/(radius*2);k++)
{
v.push(new Polygon());
addChild(v[k]);
v[k].drawPolygon(radius,sides,2*radius*k,radius*3.5,0x00FF00,lineThickness,myrotation);
}
}
}
}
compile notes...
Loading configuration file C:\Program Files (x86)\Adobe\Adobe Flash Builder 4.6\
sdks\4.6.0\frameworks\flex-config.xml
Initial setup: 64ms
start loading swcs 14ms Running Total: 78ms
Loaded 30 SWCs: 484ms
precompile: 841ms
C:\Users\jmasiello.place\Documents\flash\Main.as: Warning: This compila
tion unit did not have a factoryClass specified in Frame metadata to load the co
nfigured runtime shared libraries. To compile without runtime shared libraries e
ither set the -static-link-runtime-shared-libraries option to true or remove the
-runtime-shared-libraries option.
Files: 145 Time: 961ms
Linking... 25ms
Optimizing... 26ms
SWF Encoding... 11ms
C:\Users\jmasiello.place\Documents\flash\Main.swf (1809 bytes)
postcompile: 65ms
Total time: 1595ms
Peak memory usage: 47 MB (Heap: 29, Non-Heap: 18)
WWhen you do
private var f:Array = new Array(20);
You create an array with 20 elements in implements
Then later on you are pushing an element into the same array.
f.push(new Polygon());
By pushing an element you are adding to the last element
Which makes the array have the first 20 elements null and the 21st element be a Polygon
You can even verify it by doing.
for(var j:int = 0; j<stage.stageWidth/(radius*2);j++){
f.push(new Polygon());
trace(f)
}
So in your code you are pushing into the 21st element and then accessing the array via
addChild(f[j]);
Which j = 0 and the element in 0 is still null
Then best way to fix it would be to
private var f:Array = new Array();
Are you getting any errors? You probably need to cast to Polygon in order for the compiler to accept the drawPolygon method.
for(var j:int = 0; j<stage.stageWidth/(radius*2);j++) {
f.push(new Polygon());
addChild(f[j]);
Polygon(f[j]).drawPolygon(radius,sides,2*radius*j,radius*3,0x0000FF,lineThickness,myrotation;
}
To skip the casting you can use a Vector instead of an Array as then the compiler will know that it handles Polygons exclusively.