I'm trying to make lots of the same object appear randomly on the screen subject to conditions and k

Posted by Katsideswide on Stack Overflow See other posts from Stack Overflow or by Katsideswide
Published on 2010-04-18T16:38:33Z Indexed on 2010/04/18 16:43 UTC
Read the original article Hit count: 185

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.

© Stack Overflow or respective owner

Related posts about flash-cs4

Related posts about actionscript-3