views:

56

answers:

2

Hi! A good friend recommended this site to me, it looks really useful! I'm a bit of a shameless noob at actionscript and after 3 days of tutorials and advice I've hit a brick wall.

I've managed to get a sensor attached to an arduino talking to flash using something called AS3glue. it works, when i set up a trace("leaf") for the contition that the sensor reads 0, i get a printout of the word "leaf". however i want the program to make a graphic appear on the screen when this condition is met, not just trace something.

I'm trying to get the program to generate a library object called "Enemy" on the screen at a random position each time the conditions are met. It's called enemy because I was following a game tutorial...actually it's a drawing of a leaf.

Here's the bit of the code which is causing me problems:

var army:Array; var enemy:Enemy;
function AvoiderGame() { army = new Array(); var newEnemy = new Enemy( 100, 100 ); army.push( newEnemy ); addChild( newEnemy ); }

function timerEvent(event:Event):void {

if (a.getAnalogData(0) ==0 && a.getAnalogData(0) != this.lastposition){

          trace("leaf");
          var randomX:Number = (Math.random() * 200) + 100;
          var randomY:Number = (Math.random() * 150) + 50;
          var newEnemy = new Enemy( randomX, randomY);
          army.push( newEnemy );
          addChild( newEnemy );
       } else if (a.getAnalogData(0) == 0) {   //don't trace anything } >else {

//don't trace anything } this.lastposition = a.getAnalogData(0); //afterwards, set the position to be the new lastposition and repeat.

}

I've imported "import flash.display.MovieClip;"

and the code for the Enemy class looks like this:

package { import flash.display.MovieClip; public class Enemy extends MovieClip { public function Enemy( startX:Number, startY:Number ) { x = startX; y = startY; } } }

Here's my error. I've tried googling, it seems like a pretty general error:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at as3glue_program_fla::MainTimeline/timerEvent() at flash.utils::Timer/_timerDispatch() at flash.utils::Timer/tick()

I've made sure that the "Enemy" object is exported for AS3.

I'm going for something like this when it's programmed in AS2:

leafCounter = 0; //set the counter to 0 counter.swapDepths(1000); //puts the counter on top of pretty much anything, unless you make more than 1000 leaves! counter.textbox.text = 0; //shows "0" in the text box in the "counter" movie clip

this.onMouseDown = function() { //triggers when the mouse is clicked this.attachMovie("Leaf","Leaf"+leafCounter,leafCounter,{_x:Math.random()*Stage.width,_y:Math.random()*Stage.height,_rotation:Math.random()*360}); //adds a leaf to rthe stage with a random position and random rotation leafCounter++; //adds 1 to the leaf counter counter.textbox.text = leafCounter; //shows that number in the text box }

I'm sure it must be a simple error, I can get the logic working when it just traces something on the screen but i can't get it to generate an "enemy"

Any help or hints would be really useful! I know this is a bit of a ham-fisted job of altering existing code.

A: 

ok, the error you are throwing means something does not exist when you think it should. At the moment the best candidate is looking to be lastposition, though you might have instanced it earlier. Comment out each global var til it works. You seem to be checking that a.input is equal to 0 but not lastposition, then you later set lastposition as a.input (which must be 0) check that too.

shortstick
Hi, thanks for your help! When i comment everthing out except the trace("leaf"); in the "if" statement it works perfectly. Every time the sensor is pressed, "leaf" is traced. it only gives errors when I include the part meant to add a new "enemy" onto the screen. It doesn't. I'm not sure what's wrong with it...
Katsideswide
well as far as i can see the creation of the "Enemy" and adding it to the stage is fine (did you import Enemy too?). try commenting single variables until something works, not just blocks.
shortstick
Ah ha! Thanks so much for your advice, I took out the line: army.push( newEnemy ); and it works perfectly! No idea why though.
Katsideswide
A: 

For some reason, taking out the line army.push( newEnemy ); makes this work perfectly! :D

Katsideswide
in which case you have not initialised the array 'army' are you sure that you have run AvoiderGame() before you have started the timer?
shortstick
I'm not certain, I used bits taken from a tutorial. I don't need thins to appear at a set time, so maybe it doesn't matter for this application?
Katsideswide