Error 1009 cannot access a property----again - actionscript-3

Hello I'm stuck on this one which is trying to show a game level on the game and getting an output error 1009. I'm doing something wrong but can't figure. Here is the code:
if ( levelNumber ==1) {
var level:Number = 1;
showLevel.text=level.toString();
showLevel.text = String ("Level: " +level);
if ( levelNumber ==2) {
var level:Number = 2;
showLevel.text=level.toString();
showLevel.text = String("Level: " +level);
var numBombs:Number = 4;
}else if( levelNumber ==3 ) {
var level:Number = 3;
showLevel.text=level.toString();
showLevel.text = String ("Level: " +level);
The debugger says it's the line with showLevel.text=level.toString(); which I have set as a Number ie private var level:Number =0. I hope this is sufficient code to let you know what's going on here. It's also coming up with a duplicate variable definition - compiler error on var level:Number =2 and the next one that =3.Thanking you in advance for any assistance. Cheers.

Error #1009 is indeed a null reference error. I see your level is declared and has a right value.
Maybe the point is that your showLevel object (I guess it's a label) is not declared yet.
The problem can be that you use:
private var showLevel:mx.controls.Label;
instead of:
private var showLevel:mx.controls.Label = new mx.controls.Label();
otherwise if you make your showLevel in a viewStack you must be sure that your content is created yet. Therefor you can use:
<mx:ViewStack id="yourView" creationPolicy="all">

Related

Implicit Coercion of a value of a type of text.flash:TextField to an unrelated Int

Okay, so I'm trying to create a mobile flash game (I'm mostly an animator, a storyteller) and I'm having trouble with the input text for the player name. I've often read helpful tips on this site so I signed up to get some help. The code I use to load and save data is saveDataObject, But as far as I know input text has to be used with package code. I tried to convert it to function var, but then these errors occur. I am unsure how to use the Package class code, and everything I've read on it has been confusing. I am pretty much self taught for everything I know about code though tutorials and forums, so if it isn't explained in a way I can understand I wont be able to do it...
Here's the section of code if I wasn't clear(error lines separate):
var playerName:int;
init(); // this line goes directly beneath the variables
function f_1(init):void
{ // call once to set everything up
saveDataObject = SharedObject.getLocal("DataBattle/character/name"); // give the save data a location
-
playerName = txtPlayer;
-
function addName(e:TouchEvent):void
{
var myTextBox1:TextField = new TextField();
var txtPlayer:TextField = new TextField();
var myText:String = "Cyan";
function CaptureUserInput()
{
captureText();
}
function captureText():void
{
myTextBox1.type = TextFieldType.INPUT;
myTextBox1.background = true;
addChild(myTextBox1);
myTextBox1.text = myText;
myTextBox1.addEventListener(TextEvent.TEXT_INPUT, textInputCapture);
}
function textInputCapture(event:TextEvent):void
{
var str:String = myTextBox1.text;
createOutputBox(str);
}
function createOutputBox(str:String):void
{
txtPlayer.background = false;
txtPlayer.x = 200;
addChild(txtPlayer);
txtPlayer.text = str;
}
-
if(saveDataObject.data.characterName = playerName == null)
-
{ // checks if there is save data
trace("No Player data yet."); // if there isn't any data on the computer...
saveDataObject.data.characterName = playerName; // ...set the savedScore to 0
}
else
{
trace("Player data found."); // if we did find data...
loadData1(); // ...load the data
}
function loadData1():void
{
playerName = saveDataObject.data.characterName; // set the current score to the saved score
trace("Data Loaded!");
}
}
}
function saveData(e:TouchEvent):void
{
saveDataObject.data.characterName = playerName; // set the saved score to the current score
trace("Data Saved!");
saveDataObject.flush(); // immediately save to the local drive
trace(saveDataObject.size); // this will show the size of the save file, in bytes
}
Would be nice to have some more details about this error. Like which line is actually throwing this error.
However even without it there are few things that looks weird right away.
var playerName:int;
// And few lines below
playerName = txtPlayer;
// Few more lines below
var txtPlayer:TextField = new TextField();
Looks like you're declaring playerName as int so you want to store numeric values, but then txtPlayer is a TextField. So by playerName = txtPlayer; you're trying to store TextField as Int value, which is not gonna work.
I can also see
if(saveDataObject.data.characterName = playerName == null)
and it's like :O
I'm not even sure how this part is being compiled. You want to do playerName == null check (which would return true/false) and then assign it to saveDataObject.data.characterName ?

Strange object property issue in flash

I've struck a rather odd problem in my flash game. I get a "Error #1009: Cannot access a property or method of a null object reference." error in the following block of code:
public function createHero()
{
hero = new Object();
hero.mc = gamelevel.hero;
hero.dx = 0.0;
hero.dy = 0.0;
hero.inAir = false;
hero.direction = 1;
hero.animstate = "stand";
hero.walkAnimation = new Array(2, 3, 4, 5, 6, 7, 8);
hero.animstep = 2;
hero.jump = false;
hero.moveLeft = false;
hero.moveRight = false;
hero.jumpSpeed = 0.8;
hero.walkSpeed = 0.15;
hero.width = 64.0;
hero.height = 96.0;
hero.startX = hero.mc.x;
hero.startY = hero.mc.y;
hero.profile = "technology";
hero.ability = "robot";
hero.activePhotoFrame = false;
}
Using the debugger I've narrowed the error down to "hero.mc = gamelevel.hero;". I tried figuring out if hero.mc or gamelevel.hero was the culprit by using a test object.
testObject = gamelevel; //worked perfectly fine
testObject = gamelevel.hero; //gave the error
testObject.mc = gamelevel; //also gave the error
testObject.mc = gamelevel.hero; //gave the same error as well
I don't understand what the problem is.
For clarification "gamelevel" is the instance name of a movieclip containing another movieclip with the instance name "hero"
The problem isn't limited to the hero object. If I comment it out I get the error with any other object assigning gamelevel.something to a mc property.
What makes it stranger is that this worked perfectly fine before.
If any other information is required, please don't hesitate to ask.
gotta be on stage before you can access stage instances.
this.addEventListener(Event.ADDED_TO_STAGE,createHero)
Turned out one of the images was corrupted...

AS3 - Yet another TypeError: Error #1009 Cannot acces

I am creating a "Space Invaders" game in AS3. Got everything working now, but keep ketting a #1009 error and i'm looking at it for quite a time now and i just don't see the problem. Some extra eyes could help out I think!
This is what the debugger says:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at testbestandkopie_fla::MainTimeline/winGame()[testbestandkopie_fla.MainTimeline::frame1:352]
at testbestandkopie_fla::MainTimeline/enemyHitTest()[testbestandkopie_fla.MainTimeline::frame1:338]
at testbestandkopie_fla::MainTimeline/onTick()[testbestandkopie_fla.MainTimeline::frame1:117]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
Below I will paste the code that has to do with the error:
1: Creating the AS Linkage names:
//Sound FX-------------------------------------------------------------------------
var startFX:Sound = new startGameSound();
var laserFX:Sound = new laserSound();
var explosionFX:Sound = new explosionSound();
var musicFX:Sound = new backgroundMusic();
var loseFX:Sound = new endGameSound();
var winFX:Sound = new winGameSound();
var SfxTransform = new SoundTransform();
var myChannelMisc:SoundChannel = new SoundChannel();
var myChannelMusic:SoundChannel = new SoundChannel();
var myChannelWin:SoundChannel = new SoundChannel();
var myChannelLose:SoundChannel = new SoundChannel();
//---------------------------------------------------------------------------------
2: [Line 117] The AS line 117 is the highlighted one:
//Handlers Functions---------------------------------------------------------------
function onTick(e:TimerEvent) { //A continuous run of some functions below.
moveCharacter();
moveEnemyField();
playerCollisionDetectWall();
enemyCollisionDetectWall();
enemyCollisionDetectWallBottom();
moveLasers();
enemyHitTest(); <---- line 117
}
3: [Line 338] The function enemyHitTest(); where it starts the winGame(); function:
function enemyHitTest() {
//For each of the three enemys
for (var i:int = 0; i < enemyArray.length; i++) {
//the each of the six lasers
for (var j:int = 0; j < 6; j++) {
//don't consider lasers that aren't in play:
if (laserArray[j].y > SpelerMC.y) continue;
if (enemyArray[i].visible && enemyArray[i].hitTestObject(laserArray[j])) {
score += 10;
myChannelMisc = explosionFX.play();
SfxTransform.volume = 0.3;
myChannelMisc.soundTransform = SfxTransform;
scoreTxt.text = score.toString();
trace("Invader nummer " + i + " neergeschoten!");
enemyArray[i].visible = false;
//Now we remove the laser when hitting.
laserArray[j].x = j * 70 + 100;
laserArray[j].y = 895;
}
else if(score == 660) {
//If you reach a score of 660 (66 enemy's x 10 = 660) you win the game.
winGame(); <---- Line 338
}
}
}
}
4: [Line 352] Where the winGame(); function run's after getting 660 points at the enemyHit.
function winGame() {
winScreen.visible = true;
gameTimer.stop();
//Stop the music.
myChannelMusic.stop();
//Start the "You Win" music.
myChannelWin = winFX.play();
SfxTransform.volume = 0.02;
myChannelWin.soundTransform = SfxTransform; <---- line 352
}
So as you can see, it run's through these functions. I've already checked if something is wrong with my file in the Library, but the AS Linkage name is exactly the same as the var I defined above. Maybe a few extra eyes can see what's going wrong here, and explain me why..
Thanks in advance!
As per the livedocs :
winFX.play() method may return null if you have no sound card or if you run out of available sound channels. The maximum number of sound channels available at once is 32.
Check if either of the issues above is applicable to you....
As Mark said, the class winGameSound is the culprit here. The call winFX.play() returns a null, not a sound channel. So you can not apply a sound transform to the null object.
The only info one can currently get is that the class inherits Sound class & returns null with play() call.

Reference Shared Object AS3

I am trying to reference a shared object (one that saves data), but whenever I try to, I get a crash.
This code works fine:
var var1:Object = { value:1 };
var varRef:Object = var1;
if(var1.value == 1) {
varRef.value = 50;
}
trace(varRef.value); // outputs 50;
trace(var1.value); // outputs 50;
But when I try to use shared objects, it doesn't work.
import flash.net.SharedObject;
var iapso:SharedObject = SharedObject.getLocal("purchases");
var varRef:Object = iapso.data.testing;
varRef = 90
trace ("The shared value is " + iapso.data.testing);
trace ("This should mirror it" + varRef);
If you're able to figure out the issue, please post a fixed version.
Thanks.
You must come from a programming background where pointers can be dereferenced.
In this case, varRef is not varRef = &iapso. Setting its value does not change the value of iapso.data.testing;
Initially, you set varRef as a pointer to iapso.data.testing:
var varRef:Object = iapso.data.testing;
Then, you immediately change it to a constant value object literal 90:
varRef = 90;
This does not set the value of testing to 90 - this changes the value of varRef.
You could set varRef to iapso.data, then set testing as in:
var varRef:Object = iapso.data;
varRef.testing = 90;
Then, the following would produced expected results:
trace(iapso.data.testing); // 90
trace(varRef.testing); // 90
setting varRef will not update the value in iapso.data - numbers are copied, not referenced.
var bar:Object = { data:1 };
var foo:Object = bar.data;
trace(foo); //"1"
bar.data = 2;
trace(foo); //"1"
If you really want to use a reference, use data instead, eg:
import flash.net.SharedObject;
var iapso:SharedObject = SharedObject.getLocal("purchases");
var varRef:Object = iapso.data;
varRef.testing = 90
trace ("The shared value is " + iapso.data.testing);
trace ("This should mirror it" + varRef.testing);

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.